本文为大家分享了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, 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 name) { 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; } //获取价格 public 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){ System.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.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<Fruit> list = new ArrayList<Fruit>(); check(list); while (true) { System.out .println(" 欢迎光临水果系统"); System.out .println("请输入你的操作:(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<Fruit> list) { int sum = 0; for (int i = 0; i < list.size(); i++) { Fruit f = list.get(i); sum += f.getMoney(); } if(sum>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<Fruit> list) throws IOException { Scanner sc1 = 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 (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<Fruit> list) throws IOException { BufferedReader br = new BufferedReader(new FileReader("fruit.txt")); String line; while ((line = br.readLine()) != null) { String[] str = line.split(" "); Fruit f = new Fruit(str[0], str[1], Integer.parseInt(str[2]), str[3]); list.add(f); } br.close(); } public void print(ArrayList<Fruit> list) { System.out.println("ID/t水果/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()); } } }
管理员类
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.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<Fruit> list = new ArrayList<Fruit>(); check(list); System.out .println("请输入您的操作: (1.查看水果种类 2.增加水果种类 3.修改水果种类 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 void remove(ArrayList<Fruit> 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("删除成功"); return; } } System.out.println("找不到要删除的水果ID!"); } //修改水果 public void reverse(ArrayList<Fruit> 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 = list.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.println("修改成功"); return; } } System.out.println("找不到要修改的水果ID!"); } //增加水果 public void addFruit(ArrayList<Fruit> 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 = 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, price, unit); list.add(f); write(list); System.out.println("增加成功"); } //写入新加的种类 private void write(ArrayList<Fruit> 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() + " " + f.getUnit()); bw.newLine(); } bw.close(); } public void print(ArrayList<Fruit> list) { System.out.println("ID/t水果/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<Fruit> list) throws IOException { BufferedReader br = new BufferedReader(new FileReader("fruit.txt")); String line; while ((line = br.readLine()) != null) { String[] str = line.split(" "); Fruit f = new Fruit(str[0], str[1], Integer.parseInt(str[2]), str[3]); list.add(f); } br.close(); } // 登陆系统 public boolean load() throws FileNotFoundException, 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 (str[0].equals(username) && str[1].equals(password)) { System.out.println("欢迎您进入水果管理系统: " + username); return true; } else { System.out.println("你的用户名或密码输入不正确,无法进入管理系统"); return false; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VeVb武林网。
新闻热点
疑难解答
图片精选