java實現(xiàn)水果超市管理系統(tǒng)
本文為大家分享了java實現(xiàn)水果超市管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
首先建立水果類的界面
public class Fruit {
//定義ID
private String id;
//定義名稱
private String name;
//定義價格
private int price;
//定義單位
private String unit;
//定義數(shù)量
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( " 歡迎光臨水果系統(tǒng)");
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(" 歡迎光臨水果系統(tǒng)");
System.out
.println("請輸入你的操作:(1.查看水果 2.購買水果 3.結(jié)賬 4.退出)");
int choice = sc.nextInt();
switch (choice) {
case 1:
// 查看水果
print(list);
break;
case 2:
// 購買水果
buy(list);
break;
case 3:
// 結(jié)賬
checkOut(list);
break;
case 4:
// 退出
return;
default:
System.out.println("你輸入的操作有誤!");
}
}
}
//結(jié)賬
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+ "元, 優(yōu)惠價格:"+ newSum+"元");
}else{
System.out.println("金額:" + sum+"元");
}
//結(jié)完賬后,將數(shù)量清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元,享受九折優(yōu)惠!");
System.out.println("請輸入想要購買的水果的ID:(如果不想購買,請輸入-1退出)");
String id = sc1.nextLine();
if ("-1".equals(id)) {
System.out.println("購買已結(jié)束,請去結(jié)賬 ");
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() + "數(shù)量: ");
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名重復(fù)!");
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();
}
// 登陸系統(tǒng)
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("歡迎您進入水果管理系統(tǒng): " + username);
return true;
} else {
System.out.println("你的用戶名或密碼輸入不正確,無法進入管理系統(tǒng)");
return false;
}
}
}
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實現(xiàn)多環(huán)境配置文件切換教程詳解
很多時候,我們項目在開發(fā)環(huán)境和生成環(huán)境的環(huán)境配置是不一樣的,例如,數(shù)據(jù)庫配置,這個時候就需要切換環(huán)境配置文件。本文將詳細(xì)講解SpringBoot如何切換配置文件,需要的可以參考一下2022-03-03
springboot logback如何從apollo配置中心讀取變量
這篇文章主要介紹了springboot logback如何從apollo配置中心讀取變量的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
java為什么使用BlockingQueue解決競態(tài)條件問題面試精講
這篇文章主要為大家介紹了java為什么使用BlockingQueue解決競態(tài)條件問題面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Java多態(tài)實現(xiàn)原理詳細(xì)梳理總結(jié)
這篇文章主要介紹了Java多態(tài)實現(xiàn)原理詳細(xì)梳理總結(jié),多態(tài)是繼封裝、繼承之后,面向?qū)ο蟮牡谌筇匦?,本文只總結(jié)了多態(tài)的實現(xiàn)原理,需要的朋友可以參考一下2022-06-06
解決創(chuàng)建springboot后啟動報錯:Failed?to?bind?properties?under‘spri
在Spring?Boot項目中,application.properties和application.yml是用于配置參數(shù)的兩種文件格式,properties格式簡潔但不支持層次結(jié)構(gòu),而yml格式支持層次性,可讀性更好,在yml文件中,要注意細(xì)節(jié),比如冒號后面需要空格2024-10-10

