1、java实现水果超市管理系统java实现水果超市管理系统这篇文章主要为大家详细介绍了java实现水果超市管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。废话不多说,直接上源码:首先建立水果类的界面:public class Fruit /定义ID private String id; /定义名称 private String name; /定义价格 private int price; /定义单位 private String unit; /定义数量 private int number; public Fruit(String id, String name, int price
2、, String unit) super(); this.id = id; this.name = name; this.price = price; this.unit = unit; public Fruit() super(); / TODO Auto-generated constructor stub public String getId() return id; public void setId(String id) this.id = id; public String getName() return name; public void setName(String nam
3、e) this.name = name; public int getPrice() return price; public void setPrice(int price) this.price = price; public String getUnit() return unit; public void setUnit(String unit) this.unit = unit; public int getNumber() return number; public void setNumber(int number) this.number = number; /获取价格 pub
4、lic int getMoney() return price * number; 水果超市的界面:import java.io.IOException; import java.util.Scanner; public class FruitTest public static void main(String args) throws IOException Scanner sc = new Scanner(System.in); Shopper shopper = new Shopper(); Manager manager = new Manager(); while(true) Sy
5、stem.out.println( 欢迎光临水果系统); System.out.println(请输入你的角色:(1.顾客 2.管理员 3.退出); int choice = sc.nextInt(); switch(choice) case 1: /顾客 shopper.shop(); break; case 2: /管理员 manager.manager(); break; case 3: System.exit(0); default: System.out.println(你的输入有误!); 顾客类:import java.io.BufferedReader; import java.
6、io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Shopper public void shop() throws IOException Scanner sc = new Scanner(System.in); ArrayList list = new ArrayList(); check(list); while (true) System.out .println( 欢迎光临水果系统); System.out .pri
7、ntln(请输入你的操作:(1.查看水果 2.购买水果 3.结账 4.退出); int choice = sc.nextInt(); switch (choice) case 1: / 查看水果 print(list); break; case 2: / 购买水果 buy(list); break; case 3: / 结账 checkOut(list); break; case 4: / 退出 return; default: System.out.println(你输入的操作有误!); /结账 private void checkOut(ArrayList list) int sum =
8、0; for (int i = 0; i 200) int newSum = (int) (sum * 0.9); System.out.println(金额: + sum+ 元, 优惠价格:+ newSum+元); else System.out.println(金额: + sum+元); /结完账后,将数量清0 for (int i = 0; i list.size(); i+) Fruit f = list.get(i); f.setNumber(0); / 购买水果 public void buy(ArrayList list) throws IOException Scanner s
9、c1 = new Scanner(System.in); Scanner sc2 = new Scanner(System.in); print(list); while (true) System.out.println(购买超过200元,享受九折优惠!); System.out.println(请输入想要购买的水果的ID:(如果不想购买,请输入-1退出); String id = sc1.nextLine(); if (-1.equals(id) System.out.println(购买已结束,请去结账 ); return; else boolean flag = false; for
10、(int i = 0; i list.size(); i+) Fruit f = list.get(i); if(f.getId().equals(id) System.out.println(请输入购买 + f.getName() + 数量: ); int num = sc2.nextInt(); f.setNumber(num); flag = true; if(!flag) System.out.println(你输入的水果ID不正确,请重新输入); / 查看水果 public void check(ArrayList list) throws IOException BufferedR
11、eader br = new BufferedReader(new FileReader(fruit.txt); String line; while (line = br.readLine() != null) String str = line.split( ); Fruit f = new Fruit(str0, str1, Integer.parseInt(str2), str3); list.add(f); br.close(); public void print(ArrayList list) System.out.println(IDt水果t价格t单位); for (int i
12、 = 0; i list.size(); i+) Fruit f = list.get(i); System.out.println(f.getId() + t + f.getName() + t + f.getPrice() + t + f.getUnit(); 管理员类:import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import j
13、ava.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Manager public void manager() throws IOException if (load() Scanner sc = new Scanner(System.in); while (true) ArrayList list = new ArrayList(); check(list); System.out .println(请输入您的操作: (1.查看水果种类 2.增加水果种类 3.修改水果种类
14、 4.删除水果种类 5退出); int choice = sc.nextInt(); switch (choice) case 1: / 查看水果种类 print(list); break; case 2: / 增加水果种类 addFruit(list); break; case 3: / 修改水果种类 reverse(list); break; case 4: / 删除水果种类 remove(list); break; case 5: / 退出 return; default: System.out.println(你输入的操作有误!); break; else return; public
15、 void remove(ArrayList list) throws IOException Scanner sc = new Scanner(System.in); print(list); System.out.println(请输入要删除的水果ID: ); String id = sc.nextLine(); for (int i = 0; i list.size(); i+) Fruit f = list.get(i); if(f.getId().equals(id) list.remove(i); write(list); System.out.println(删除成功); ret
16、urn; System.out.println(找不到要删除的水果ID!); /修改水果 public void reverse(ArrayList list) throws IOException Scanner sc1 = new Scanner(System.in); Scanner sc2 = new Scanner(System.in); print(list); System.out.println(请输入要修改的水果ID: ); String id = sc1.nextLine(); for (int i = 0; i list.size(); i+) Fruit f = lis
17、t.get(i); if(f.getId().equals(id) System.out.println(请输入水果的名称: ); String name = sc1.nextLine(); System.out.println(请输入水果的价格: ); int price = sc2.nextInt(); System.out.println(请输入水果的单位: ); String unit = sc1.nextLine(); f.setName(name); f.setPrice(price); f.setUnit(unit); write(list); System.out.printl
18、n(修改成功); return; System.out.println(找不到要修改的水果ID!); /增加水果 public void addFruit(ArrayList list) throws IOException Scanner sc1 = new Scanner(System.in); Scanner sc2 = new Scanner(System.in); print(list); System.out.println(请输入要增加水果的ID: ); String id = sc1.nextLine(); for (int i = 0; i list.size(); i+)
19、Fruit f = list.get(i); if(f.getId().equals(id) System.out.println(水果ID名重复!); return; System.out.println(请输入水果的名字: ); String name = sc1.nextLine(); System.out.println(请输入水果的价格: ); int price = sc2.nextInt(); System.out.println(请输入水果的单位: ); String unit = sc1.nextLine(); Fruit f = new Fruit(id, name, pr
20、ice, unit); list.add(f); write(list); System.out.println(增加成功); /写入新加的种类 private void write(ArrayList list) throws IOException BufferedWriter bw = new BufferedWriter(new FileWriter(fruit.txt); for (int i = 0; i list.size(); i+) Fruit f = list.get(i); bw.write(f.getId()+ + f.getName() + + f.getPrice(
21、) + + f.getUnit(); bw.newLine(); bw.close(); public void print(ArrayList list) System.out.println(IDt水果t价格t单位); for (int i = 0; i list.size(); i+) Fruit f = list.get(i); System.out.println(f.getId() + t + f.getName() + t + f.getPrice() + t + f.getUnit(); / 查看水果 public void check(ArrayList list) thro
22、ws IOException BufferedReader br = new BufferedReader(new FileReader(fruit.txt); String line; while (line = br.readLine() != null) String str = line.split( ); Fruit f = new Fruit(str0, str1, Integer.parseInt(str2), str3); list.add(f); br.close(); / 登陆系统 public boolean load() throws FileNotFoundExcep
23、tion, IOException Scanner sc = new Scanner(System.in); System.out.println(请输入用户名: ); String username = sc.nextLine(); System.out.println(请输入密码: ); String password = sc.nextLine(); BufferedReader br = new BufferedReader(new FileReader(admin.txt); String line = br.readLine(); String str = line.split(,); if (str0.equals(username) & str1.equals(password) System.out.println(欢迎您进入水果管理系统: + username); return true; else System.out.println(你的用户名或密码输入不正确,无法进入管理系统); return false; 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我哦!
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1