Java implements the fruit supermarket management system and java supermarket management system

Source: Internet
Author: User
Tags class manager

Java implements the fruit supermarket management system and java supermarket management system

This article shares the code for implementing the fruit supermarket management system in java for your reference. The specific content is as follows:

First, create a fruit Interface

Public class Fruit {// defines the ID private String id; // defines the name private String name; // defines the price private int price; // defines the unit private String unit; // defines the number of private int numbers; 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 ;}}

Fruit supermarket Interface

Import java. io. IOException; import java. util. writable; public class FruitTest {public static void main (String [] args) throws IOException {writable SC = new writable (System. in); Shopper shopper = new Shopper (); Manager manager = new Manager (); while (true) {System. out. println ("welcome to the fruit System"); System. out. println ("Enter your role: (1. customer 2. administrator 3. exit) "); int choice = SC. nextInt (); switch (choice) {case 1: // customer shopper . Shop (); break; case 2: // administrator manager. manager (); break; case 3: System. exit (0); default: System. out. println ("your input is incorrect! ");}}}}

Customer

Import java. io. bufferedReader; import java. io. fileReader; import java. io. IOException; import java. util. arrayList; import java. util. writable; public class Shopper {public void shop () throws IOException {writable SC = new writable (System. in); ArrayList <Fruit> list = new ArrayList <Fruit> (); check (list); while (true) {System. out. println ("welcome to the fruit System"); System. out. println ("Enter your operation: (1. view fruit 2. buy 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 (list); break; case 4: // exit return; default: System. out. println ("the operation you entered is incorrect! ") ;}}// 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, discount price:" + newSum + "Yuan");} else {System. out. println ("amount:" + sum + "Yuan") ;}// after the account is closed, the number is cleared by 0 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 {empty sc1 = new empty (System. in); required sc2 = new partition (System. in); print (list); while (true) {System. out. println ("purchase more than 200 yuan, enjoy a discount! "); System. out. println ("Enter the ID of the fruit you want to purchase: (if you do not want to buy, enter-1 to exit)"); String id = sc1.nextLine (); if ("-1 ". equals (id) {System. out. println ("the purchase has ended, please check out"); 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 the purchase" + f. getName () + "Quantity:"); int num = sc2.nextInt (); f. setNumber (num); flag = true ;}} if (! Flag) {System. out. println ("the Fruit ID you entered is incorrect. Please enter it again") ;}}}// view 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 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

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. required; public class Manager {public void manager () throws IOException {if (load () {synchronized SC = new synchronized (System. in); while (true) {ArrayList <Fruit> list = new Array List <Fruit> (); check (list); System. out. println ("Enter your operation: (1. view fruit Types 2. increase the fruit category. modify the fruit category 4. delete fruit Category 5 Exit) "); int choice = SC. nextInt (); switch (choice) {case 1: // view the fruit type print (list); break; case 2: // Add the fruit type addFruit (list); break; case 3: // modify the fruit type reverse (list); break; case 4: // Delete the fruit type remove (list); break; case 5: // exit return; default: system. out. println ("the operation you entered is incorrect! "); Break ;}}else {return ;}} public void remove (ArrayList <Fruit> list) throws IOException {transport SC = new partition (System. in); print (list); System. out. println ("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 ("deleted successfully"); return ;}} System. out. println ("the fruit I to be deleted cannot be found D! ");} // Modify the Fruit public void reverse (ArrayList <Fruit> list) throws IOException {transport sc1 = new partition (System. in); required sc2 = new partition (System. in); print (list); System. out. println ("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 ("Enter the fruit name:"); String name = sc1.nextLine (); System. out. printl N ("Enter the fruit price:"); int price = sc2.nextInt (); System. out. println ("Enter the unit of 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 public void addFruit (ArrayList <Fruit> list) throws IOException {transport sc1 = new partition (System. in); required sc2 = new partition (System. in); print (list); System. out. println ("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 ("Duplicate Fruit ID name! "); Return ;}} System. out. println ("Enter the fruit name:"); String name = sc1.nextLine (); System. out. println ("Enter the fruit price:"); int price = sc2.nextInt (); System. out. println ("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 ("added successfully");} // write the newly added category 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 on to the System public boolean load () throws FileNotFoundException, IOException {transport SC = new partition (System. in); System. out. println ("Enter the User name:"); String username = SC. nextLine (); System. out. println ("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 incorrect and cannot enter the management system"); return false ;}}}

For more information, see management system development.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.