水果管理系統(tǒng)java版分享給大家。
主類 fruitsdemo
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
|
/** * 功能: * 1. 查看所有的水果 * 2. 添加新的水果(添加的時候判斷水果名是否有重復(fù)) * 3. 對所有的水果進行排序(價格排序、庫存排序) * 4. 刪除指定的水果 * 5. 退出系統(tǒng) * * 注意: * 1. 每種水果都必須有水果id,水果名,水果數(shù)量,水果價格 * 2. 添加水果時,要由用戶輸入水果名、數(shù)量和價格 * 3. 刪除水果時要二次確認(rèn) * * 評分依據(jù): 功能實現(xiàn)的情況,代碼規(guī)范性(命名規(guī)范、格式規(guī)范),設(shè)計的合理性 * @author yj * */ public class fruitsdemo { public static void main(string[] args) { int select = 0 ; // 主菜單功能選擇 boolean isstart = true ; // 程序運行標(biāo)志位 while (isstart) { system.out.println( "******************水果管理系統(tǒng)******************\n請輸入下列序號選擇相應(yīng)功能:\n\n 1.查看所有的水果 \t2. 添加新的水果 \n 3.對所有的水果進行排序查看(價格排序、庫存排序) \n 4.刪除水果\t5. 退出系統(tǒng)" ); select = calculation.inputisint(); switch (select) { case 1 : //1.查看所有的水果 calculation.seeallfruits(); break ; case 2 : //2. 添加新的水果 calculation.add(); break ; case 3 : //3.對所有的水果進行排序查看(價格排序、庫存排序) calculation.sort(); break ; case 4 : // 4.刪除水果 system.out.println( "請輸入你要刪除的水果" ); string index = calculation.inputisstring(); system.out.println( "二次確認(rèn)!??!請再次輸入你要刪除的水果" ); string index1 = calculation.inputisstring(); if (index.equals(index1)){ calculation.remove(index); } else { system.out.println( "兩次輸入不匹配,刪除失?。。?!" ); } break ; case 5 : //5. 退出系統(tǒng) isstart = false ; break ; default : system.out.println( "輸入錯誤,請重新輸入" ); break ; } } system.out.println( "程序已退出,歡迎使用?。。?quot; ); } } |
fruits 類
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
|
/** * 水果類 * @author yj * */ public class fruits { // 每種水果都必須有水果id,水果名,水果數(shù)量,水果價格 private int id; //id private int nums; //數(shù)量(庫存) private string name; //水果名 private double price; //水果價格 public fruits( int id, string name, int nums, double price) { super (); this .id = id; this .nums = nums; this .name = name; this .price = price; } public int getid() { return id; } public void setid( int id) { this .id = id; } public int getnums() { return nums; } public void setnums( int nums) { this .nums = nums; } public string getname() { return name; } public void setname(string name) { this .name = name; } public double getprice() { return price; } public void setprice( double price) { this .price = price; } } |
calculation 類
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
import java.util.collections; import java.util.comparator; import java.util.iterator; import java.util.linkedlist; import java.util.scanner; /** * 計算類,存放關(guān)于計算處理數(shù)據(jù)的函數(shù) * * @author yj * */ public class calculation { static linkedlist<fruits> list = new linkedlist<fruits>(); static scanner sc = new scanner(system.in); static int id = 1 ; /** * 添加水果 get() */ public static void add() { int nums; string name; double price; system.out.print( "請輸入你要添加的水果名、數(shù)量(單位:個)和價格(單位:元)\n" ); name = calculation.inputisstring(); nums = calculation.inputisint(); price = calculation.inputisdouble(); if (cals(name, nums, price)) { list.add( new fruits(id, name, nums, price)); id++; } } /** * 查看所有水果 seeallfruits() */ public static void seeallfruits() { if (list.size() == 0 ) { system.out.println( "數(shù)據(jù)為空?。?!" ); } else { iterator<fruits> it = list.iterator(); while (it.hasnext()) { fruits temp = it.next(); system.out.println( "id -> " + temp.getid() + "\t水果名稱 -> " + temp.getname() + "\t水果數(shù)量 -> " + temp.getnums() + "\t水果價格 -> " + temp.getprice()); } } } /** * 刪除水果 remove(string index) * * @param index * 你要刪除的水果名 */ public static void remove(string index) { iterator<fruits> it = list.iterator(); while (it.hasnext()) { if (index.equals(it.next().getname())) { it.remove(); system.out.println(index + "已刪除" ); } } } /** * 判斷是否重復(fù) cals(string name, int nums, double price) * * @param name * 水果名 * @param nums * 水果數(shù)量 * @param price * 水果價格 * @return */ public static boolean cals(string name, int nums, double price) { iterator<fruits> it1 = list.iterator(); while (it1.hasnext()) { fruits temp = it1.next(); if (name.equals(temp.getname())) { temp.setnums(nums + temp.getnums()); temp.setprice(price); system.out.println( "水果——" +name+ " 已存在,數(shù)量在原基礎(chǔ)上加上 " +nums+ ",價格已更新為 " +price); return false ; } } return true ; } /** * 排序輸出 sort() */ public static void sort() { system.out.println( "1.按照價格升序 2.按照庫存升序" ); int n = inputisint(); switch (n) { case 1 : collections.sort(list, new comparator<fruits>() { @override public int compare(fruits o1, fruits o2) { if (o1.getprice() > o2.getprice()) { return 1 ; } else if (o1.getprice() < o2.getprice()) { return - 1 ; } else { return 0 ; } // return (int) (o1.getprice() * 100 - o2.getprice() * 100); } }); break ; case 2 : collections.sort(list, new comparator<fruits>() { @override public int compare(fruits o1, fruits o2) { if (o1.getnums() > o2.getnums()) { return 1 ; } else if (o1.getnums() < o2.getnums()) { return - 1 ; } else { return 0 ; } // return (int) (o1.getnums() - o2.getnums()); } }); break ; default : system.out.println( "輸入指令錯誤?。?!" ); break ; } seeallfruits(); } /** * 輸入是否是int inputisint() * * @return */ public static int inputisint() { boolean isright = true ; int select = 0 ; do { try { select = sc.nextint(); isright = true ; } catch (exception e) { system.out.println( "輸入類型不匹配,請輸入一個整數(shù)(int)" ); sc.nextline(); isright = false ; } } while (!isright); return select; } /** * 輸入是否是string inputisstring() * * @return */ public static string inputisstring() { boolean isright = true ; string select = null ; do { try { select = sc.next(); isright = true ; } catch (exception e) { system.out.println( "輸入類型不匹配,請輸入一個字符串(string)" ); sc.nextline(); isright = false ; } } while (!isright); return select; } /** * 輸入是否為douuble * * @return */ public static double inputisdouble() { boolean isright = true ; double select = null ; do { try { select = sc.nextdouble(); isright = true ; } catch (exception e) { system.out.println( "輸入類型不匹配,請輸入一個小數(shù)(double)!!!" ); sc.nextline(); isright = false ; } } while (!isright); return select; } } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/qq_36868342/article/details/76522356