复制代码
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271package com.itheima; import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import static java.lang.System.exit; public class Main{ static Scanner input = new Scanner(System.in); public static ArrayList<Goods> goodsList = new ArrayList<>(); public static void main(String[] args)throws IOException { int choice; Init();//初始化 while(true) { menu(); choice = input.nextInt(); switch (choice) { case 1: query(); break; case 2: addStock(); break; case 3: addGoods(); break; case 4: printAllGoods(); break; case 5: exit(0); break; default: System.out.println("选择有误!请再次选择。"); } } } //显示所有商品的相关信息 public static void printAllGoods(){ for(Goods goods: goodsList) System.out.println(goods); } //查询商品信息(连下) public static void query(){ System.out.print("请输入商品编号:"); String id = input.next(); try{ System.out.println(((Goods)queryID(id)).currentRecord()); }catch(NullPointerException ex){ System.out.println("无此编号"); exit(1); } } //看看有没有 public static Object queryID(String id){ for(Goods goods : goodsList) { if(goods.getId().equals(id)) return goods; } return null; } //增加库存 public static void addStock()throws IOException{ System.out.print("请输入进货数量:"); int number = input.nextInt(); System.out.print("请输入商品编号:"); String id = input.next(); try{ Goods good = (Goods)queryID(id); good.setStock(number + good.getStock()); GoodsRecorder.save(good);//保存到进货记录 GoodsRecorder.saveRealRecord(goodsList);//商品信息保存到进货 System.out.println(good.currentRecord()); }catch(NullPointerException ex){ System.out.println("无此编号"); exit(2); } } //初始化 public static void Init()throws IOException{ goodsList.add(new Goods("1001", 4.5,"百事可乐", 100, "张三")); goodsList.add(new Goods("1002", 4.0, "可口可乐",100, "李四")); goodsList.add(new Goods("1003", 3.8, "百事雪碧", 100,"王五")); for (Goods goods : goodsList) { //将商品信息添加到进货记录 GoodsRecorder.save(goods); } GoodsRecorder.saveRealRecord(goodsList); //将商品信息添加到进货实时记录 } //添加新商品 public static void addGoods()throws IOException{ System.out.print("请输入商品编号:"); String id = input.next(); if(((Goods)queryID(id)) != null){ System.out.println("已有此编号,不可重复"); exit(3); } System.out.print("请输入商品名称:"); String goodsname = input.next(); System.out.print("请输入购买数量:"); int amount = input.nextInt(); System.out.print("请输入单价:"); double unitPrice = input.nextDouble(); System.out.print("请输入联系人:"); String custumer = input.next(); goodsList.add(new Goods(id, unitPrice, goodsname, amount, custumer)); GoodsRecorder.save(goodsList.get(goodsList.size()-1)); GoodsRecorder.saveRealRecord(goodsList); System.out.println("商品进货成功.n"); } public static void menu(){ System.out.println("-----------商品进货信息及操作------------"); System.out.println("1.查询 2.进货 "); System.out.println("3.增加商品 4.显示所有商品信息"); System.out.println("5.退出" ); System.out.print("请选择需要进行的操作:"); } } package com.itheima; public class Goods{ private String id;//编号 private double unitPrice;//单价 private String goodsName;//商品名称 private int stock;//库存 private String custumerName;//联系人 public Goods(String id, double unitPrice, String goodsName, int stock, String custumerName) { this.id = id; this.unitPrice = unitPrice; this.goodsName = goodsName; this.stock = stock; this.custumerName = custumerName; } //返回总价(库存*单价) public double getTotalPrice(){ return stock * unitPrice; } //重写toString public String toString(){//返回商品的相关信息 return "进货记录编号:"+id+"n商品名称:"+goodsName+"n联系人:"+custumerName+"n单价:"+unitPrice+"n库存数量:"+stock+"n"; } public String currentRecord(){//当前商品 return "当前商品库存信息" + toString(); } public String getId(){ return id; } public void setId(String id){ this.id = id; } public double getUnitPrice(){ return unitPrice; } public void setUnitPrice(double unitPrice) { this.unitPrice = unitPrice; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public String getCustumerName() { return custumerName; } public void setCustumerName(String custumerName) { this.custumerName = custumerName; } } package com.itheima; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class GoodsRecorder {//整理好文件名准备进行写入 public static void save(Goods good)throws IOException { DateFormat date = new SimpleDateFormat("yyyyMMdd"); String fileName = "进货记录"+date.format(new Date()) +".cvs"; File file = new File("D:\java商品进货记录", fileName); if(file.exists()) save(good,file,true); else save(good, file, false); } //一个文件的写入 public static void save(Goods good, File file, boolean flag)throws IOException{//将商品写入文件 if(flag)//在原有的基础上进行追加 { byte[] b = ToStringBuffer(good).getBytes();//返回字符串的字节数 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true)); bufferedOutputStream.write(b, 0, b.length);//输出 bufferedOutputStream.close();//关闭 } else { StringBuffer sb = new StringBuffer();//字符串缓冲器对象 String string = "商品编号 "+"商品名称 "+"购买数量 "+"单价 "+"总价 "+"联系人n"+ToStringBuffer(good); sb.append(string); byte[] b = sb.toString().getBytes(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(b, 0, b.length); bufferedOutputStream.close(); } } public static String ToStringBuffer(Goods good)//将商品信息进行整理 { StringBuffer sb = new StringBuffer(); sb.append(good.getId()).append(" "); sb.append(good.getGoodsName()).append(" "); //缓冲器添加商品名称 sb.append(good.getStock()).append(" "); //缓冲器添加商品库存 sb.append(good.getUnitPrice()).append(" "); //缓冲器添加商品单价 sb.append(good.getTotalPrice()).append(" "); //缓冲器添加商品总价 sb.append(good.getCustumerName()).append("n");//缓冲器添加商品联系人名称 return sb.toString(); } //一个链表的写入 public static void saveRealRecord(ArrayList<Goods> list)throws IOException { DateFormat date = new SimpleDateFormat("yyyyMMdd"); String fileName = "商品进货实时记录" + date.format(new Date()) + ".cvs"; File file = new File("D:\java商品进货记录", fileName); if (file.exists()) { String string = "商品编号 " + "商品名称 " + "购买数量 " + "单价 " + "总价 " + "联系人n"; for (Goods goods : list) { //字符串string依次加入各商品的相关信息 string += ToStringBuffer(goods); } byte[] b = string.getBytes(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(b, 0, b.length); bufferedOutputStream.close(); } else { //如果文件不存在,创造文件并再次调用本方法 file.createNewFile(); saveRealRecord(list); } } }
最后
以上就是无私凉面最近收集整理的关于java基础案例7-2商城进货交易记录的全部内容,更多相关java基础案例7-2商城进货交易记录内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复