This article shares the specific code of Java to implement the fruit supermarket management system for your reference. The specific content is as follows
First, create a fruit interface
public class Fruit { //Define ID private String id; //Define name private String name; //Define price private int price; //Define unit private String unit; //Define quantity 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; } //Get the price public int getMoney(){ return price * number; } }The interface of the fruit supermarket
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("Welcome to the fruit system"); System.out.println("Please enter your role: (1. Customer 2. Administrator 3. Exit)"); int choice = sc.nextInt(); switch(choice){ case 1: //Customer shopper.shop(); break; case 2: //Admin manager.manager(); break; case 3: System.exit(0); default: System.out.println("Your input is wrong!"); } } } } Customer category
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(" Welcome to the fruit system"); System.out .println("Please enter your operation:(1. View fruit 2. Purchase fruit 3. Checkout 4. Exit)"); int choice = sc.nextInt(); switch (choice) { case 1: // View fruit print(list); break; case 2: // Buy fruit buy(list); break; case 3: // Checkout checkOut(list); break; case 4: // Exit return; default: System.out.println("You entered the operation incorrectly!"); } } } // Checkout 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("Amount: " + sum+ "yuan, discounted price: "+ newSum+"yuan"); }else{ System.out.println("Amount: " + sum+"yuan"); } //After settlement, clear the quantity for (int i = 0; i < list.size(); i++) { Fruit f = list.get(i); f.setNumber(0); } } // Purchase fruit 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("Purchase more than 200 yuan and enjoy a 10% discount!"); System.out.println("Please enter the ID of the fruit you want to purchase: (If you don't want to purchase, please enter -1 to exit)"); String id = sc1.nextLine(); if ("-1".equals(id)) { System.out.println("Purchase has ended, please go to checkout"); 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("Please enter purchase" + f.getName() + "Quantity: "); int num = sc2.nextInt(); f.setNumber(num); flag = true; } } if(!flag){ System.out.println("You entered the fruit ID incorrect, please re-enter"); } } } } } // View fruit 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 fruit/t price/t unit"); 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()); } } } Administrator class
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("Please enter your operation: (1. Check fruit type 2. Add fruit type 3. Modify fruit type 4. Delete fruit type 5 Exit)"); int choice = sc.nextInt(); switch (choice) { case 1: // Check fruit type print(list); break; case 2: // Add fruit type addFruit(list); break; case 3: // Modify fruit type reverse(list); break; case 4: // Remove the fruit species remove(list); break; case 5: // Exit return; default: System.out.println("You entered the error!"); break; } } } else { return; } } public void remove(ArrayList<Fruit> list) throws IOException { Scanner sc = new Scanner(System.in); print(list); System.out.println("Please enter the fruit ID to be deleted: "); 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("Delete successfully"); return; } } System.out.println("The fruit ID to be deleted cannot be found!"); } //Modify fruit 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("Please enter the fruit ID to be modified: "); 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("Please enter the name of the fruit: "); String name = sc1.nextLine(); System.out.println("Please enter the price of the fruit: "); int price = sc2.nextInt(); System.out.println("Please enter the unit of the fruit: "); String unit = sc1.nextLine(); f.setName(name); f.setPrice(price); f.setUnit(unit); write(list); System.out.println("Modified successfully"); return; } } System.out.println("The fruit ID to be modified cannot be found!"); } //Add fruit 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("Please enter the ID of the fruit to be added: "); 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("Repeat the fruit ID name!"); return; } } System.out.println("Please enter the name of the fruit: "); String name = sc1.nextLine(); System.out.println("Please enter the price of the fruit: "); int price = sc2.nextInt(); System.out.println("Please enter the unit of the fruit: "); String unit = sc1.nextLine(); Fruit f = new Fruit(id, name, price, unit); list.add(f); write(list); System.out.println("Add success"); } //Write the newly added type 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 fruit/t price/t unit"); 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()); } } // View fruit 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(); } // Log in to the system public boolean load() throws FileNotFoundException, IOException { Scanner sc = new Scanner(System.in); System.out.println("Please enter the username: "); String username = sc.nextLine(); System.out.println("Please enter the password: "); 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("Welcome to the fruit management system: " + username); return true; } else { System.out.println("Your username or password is entered incorrectly, and you cannot enter the management system"); return false; } } }For more learning materials, please pay attention to the special topic "Management System Development".
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.