一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java實現水果超市管理系統

java實現水果超市管理系統

2021-03-22 14:10scropio0zry Java教程

這篇文章主要為大家詳細介紹了java實現水果超市管理系統,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了java實現水果超市管理系統的具體代碼,供大家參考,具體內容如下

首先建立水果類的界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
 }
 
}

水果超市的界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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("你的輸入有誤!");
  }
 }
  
 }
}

顧客類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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());
 }
 }
}

管理員類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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;
 }
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/scropio0zry/article/details/78440253

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本一区二区高清免费不卡 | 久久中文字幕综合不卡一二区 | 暖暖 免费 高清 日本 在线1 | 精品国产一区二区在线观看 | 无套白浆 | 国产麻豆传媒在线观看 | 美国艳星lisann成人作品 | 99久久精品国产一区二区 | 白丝校花好湿好紧 | 好男人好资源在线观看免费 | 亚洲AV无码专区国产精品麻豆 | 99在线在线视频免费视频观看 | 日本漫画被黄漫免费动 | 亚洲 日韩 国产 中文视频 | 精品国产一区二区三区在线 | 国产在线成人精品 | 日韩高清一区二区三区不卡 | 国产一级大片免费看 | 含羞草传媒网站免费进入欢迎 | 丝瓜秋葵番茄绿巨人在线观看 | 王王的视频ivk | 黑人粗长大战亚洲女 | 亚洲男人天堂网站 | 美女做又爽又黄又猛 | 99视频福利 | 秋霞理论一级在线观看手机版 | 波多野结衣之双方调教在线观看 | 香蕉97超级碰碰碰免费公 | 2022国产麻豆剧果冻传媒入口 | 麻豆小视频在线观看 | 非洲黑女人性xxxx | 我的男友是消防员在线观看 | gogo人体模特啪啪季玥图片 | 精品一区二区三区视频 | 免费在线观看视频 | 婷婷激情综合五月天 | 色老头综合网 | 欧美精品一线二线大片 | 男人日女人的b | 91国内精品久久久久影院优播 | 1024日韩基地 |