需求:
代码实现:
复制代码
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
149import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class test { static Scanner sc = new Scanner(System.in); static List<Map<String, Object>> goods= new ArrayList<Map<String,Object>>(); public static void main(String[] args) { int[] goods_id = {9001,9002,9003}; String[] goods_name = {"商品A","商品B","商品C"}; double[] goods_p = {23.45,56.0,9,9}; initGoods(goods_id, goods_name, goods_p); System.out.println("===============欢迎光临================"); System.out.println("1:货物清单,2:添加货物,3:删除货物,4修改货物,5:退出系统"); System.out.println("================================="); while(true) { System.out.println("请输入功能编号:"); int func_code = sc.nextInt(); switch (func_code) { case 1: showAll(); break; case 2: addGoods(); break; case 3: deleteGoods(); break; case 4: updateGoods(); break; case 5: return; } } } public static void initGoods(int[] goods_id, String[] goods_name, double[] goods_p) { for(int i=0;i<goods_id.length;i++) { Map<String, Object> temp = new HashMap<String, Object>(); temp.put("id", goods_id[i]); temp.put("name", goods_name[i]); temp.put("p", goods_p[i]); goods.add(temp); } } public static void showAll() { System.out.println("货物清单:"); System.out.println("idtnametprice"); for(Map<String, Object> temp:goods) { System.out.println(temp.get("id") + "t" + temp.get("name") + "t" +temp.get("p")); } } public static void addGoods() { System.out.println("货物编号(你可以输入5,然后返回主菜单):"); int id = sc.nextInt(); if(id==5) { return; } Map<String, Object> tag = checkGoodsExist(id); if(!tag.isEmpty()) { System.out.println("货物编号已存在,请输入其他的编号。"); addGoods(); }else { System.out.println("货物名称:"); String name = sc.next(); System.out.println("货物单价:"); double p = sc.nextDouble(); Map<String, Object> temp = new HashMap<String, Object>(); temp.put("id", id); temp.put("name", name); temp.put("p", p); goods.add(temp); } } public static void deleteGoods() { System.out.println("请输入想要删除的货物编号(你可以输入5,然后返回主菜单):"); int id = sc.nextInt(); if(id==5) { return; } Map<String, Object> tag = checkGoodsExist(id); if(!tag.isEmpty()) { goods.remove(tag); }else { System.out.println("不存在的货物编号。"); deleteGoods(); } } public static void updateGoods() { System.out.println("请输入要修改的货物编号(你可以输入5,然后返回主菜单):"); int id = sc.nextInt(); if(id==5) { return; } Map<String, Object> tag = checkGoodsExist(id); if(!tag.isEmpty()) { goods.remove(tag); System.out.println("新的货物编号"); int new_id = sc.nextInt(); Map<String, Object> temp = checkGoodsExist(new_id); if(!temp.isEmpty()) { System.out.println("您输入的货物编号已经存在,请重新输入。"); updateGoods(); }else { System.out.println("新的货物名称"); String new_name = sc.next(); System.out.println("新的货物单价:"); double new_p = sc.nextDouble(); tag.put("id", new_id); tag.put("name", new_name); tag.put("p", new_p); goods.add(tag); } }else { System.out.println("不存在的货物。"); updateGoods(); } } public static Map<String, Object> checkGoodsExist(int goods_id) { Map<String, Object> tag = new HashMap<String, Object>(); for(Map<String, Object> temp:goods) { if(goods_id==(int)temp.get("id")) { tag = temp; } } return tag; } }
最后
以上就是哭泣水池最近收集整理的关于java练习题--商品管理系统的全部内容,更多相关java练习题--商品管理系统内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复