java implements fruit supermarket management system

  • 2020-12-22 17:40:11
  • OfStack

This paper shares the specific code of java fruit supermarket management system for your reference. The specific content is as follows

First, the interface of fruit is established


public class Fruit { 
 // define ID 
 private String id; 
 // Define the name  
 private String name; 
 // Define the price  
 private int price; 
 // To define the unit  
 private String unit; 
 
 // Define the number  
 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; 
 } 
 
 // To get the price  
 public int getMoney(){ 
 return price * number; 
 } 
 
} 

Fruit supermarket interface


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. The customer  2. The administrator  3. exit )"); 
  int choice = sc.nextInt(); 
  switch(choice){ 
  case 1: 
  // The customer  
  shopper.shop(); 
  break; 
  case 2: 
  // The administrator  
  manager.manager(); 
  break; 
  case 3: 
  System.exit(0); 
  default: 
  System.out.println(" Your input is wrong !"); 
  } 
 } 
  
 } 
} 

The customer class


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. See the fruit  2. To buy fruit  3. The invoicing   4. exit )"); 
  int choice = sc.nextInt(); 
  switch (choice) { 
  case 1: 
  //  See the fruit  
  print(list); 
  break; 
  case 2: 
  //  To buy fruit  
  buy(list); 
  break; 
  case 3: 
  //  The invoicing  
  checkOut(list); 
  break; 
  case 4: 
  //  exit  
  return; 
  default: 
  System.out.println(" You are inputting something wrong !"); 
  } 
 
 } 
 
 } 
 
 // The invoicing  
 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 ,  Preferential price: "+ newSum+" yuan "); 
 }else{ 
  System.out.println(" Amount: " + sum+" yuan "); 
 } 
  
 // After the account is settled, the quantity is cleared 0 
 for (int i = 0; i < list.size(); i++) { 
  Fruit f = list.get(i); 
  f.setNumber(0); 
 } 
 } 
 
 //  To buy 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(" Buy more than 200 Yuan, enjoy 9 A discount! "); 
  System.out.println(" Please enter the fruit you want to buy ID : ( If you do not want to buy, please enter -1 exit )"); 
  String id = sc1.nextLine(); 
  if ("-1".equals(id)) { 
  System.out.println(" The purchase is finished, please go to settle the bill  "); 
  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(" The fruit that you put in ID Is not correct , Please re-enter "); 
  } 
  } 
 
 } 
 
 } 
 
 //  See the 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 The 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()); 
 } 
 } 
} 

Manager 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 the type of fruit   2. Increase the variety of fruit  3. Modify fruit types  4. Delete fruit species   5 exit )"); 
  int choice = sc.nextInt(); 
  switch (choice) { 
  case 1: 
   //  Check the type of fruit  
   print(list); 
   break; 
  case 2: 
   //  Increase the variety of fruit  
   addFruit(list); 
   break; 
  case 3: 
   //  Modify fruit types  
   reverse(list); 
   break; 
  case 4: 
   //  Delete fruit species  
   remove(list); 
   break; 
  case 5: 
   //  exit  
   return; 
  default: 
   System.out.println(" You are inputting something wrong !"); 
   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 to delete 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(" Delete the success "); 
  return; 
  } 
 } 
 System.out.println(" Unable to find fruit to delete ID!"); 
 } 
 
 // Modify the 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 to be modified 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(" Please enter the name of the fruit : "); 
  String name = sc1.nextLine(); 
  System.out.println(" Please enter the price of fruit : "); 
  int price = sc2.nextInt(); 
  System.out.println(" Please enter the unit of fruit : "); 
  String unit = sc1.nextLine(); 
   
  f.setName(name); 
  f.setPrice(price); 
  f.setUnit(unit); 
   
  write(list); 
  System.out.println(" Modify the success "); 
  return; 
  } 
 } 
 System.out.println(" No fruit to modify ID!"); 
  
  
 } 
 
 // Increase the 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 one to add fruit 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(" fruit ID A repeated !"); 
  return; 
  } 
 } 
 System.out.println(" Please enter the name of the fruit : "); 
 String name = sc1.nextLine(); 
 System.out.println(" Please enter the price of fruit : "); 
 int price = sc2.nextInt(); 
 System.out.println(" Please enter the unit of fruit : "); 
 String unit = sc1.nextLine(); 
  
 Fruit f = new Fruit(id, name, price, unit); 
 list.add(f); 
  
 write(list); 
 System.out.println(" Increase the success "); 
  
 } 
 // Write the new 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 The 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()); 
 } 
 } 
 
 //  See the 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 the system  
 public boolean load() throws FileNotFoundException, IOException { 
 Scanner sc = new Scanner(System.in); 
 
 System.out.println(" Please enter user name:  "); 
 String username = sc.nextLine(); 
 System.out.println(" Please enter your 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 not correct and you cannot access the management system "); 
  return false; 
 } 
 } 
} 

For more information, please pay attention to the topic management System Development.


Related articles: