这个购物车用了个简单的Map集合
Megan
1.package com.sxt;
2.
3.import java.util.HashMap;
4.import java.util.Map;
5.import java.util.Scanner;
6.
7.class Goods{
8. private Integer id;
9. private String name;
10. private Integer price;
11. private Integer numbers;
12. Goods(){}
13. public Integer getId() {
14. return id;
15. }
16. public void setId(Integer id) {
17. this.id = id;
18. }
19. public String getName() {
20. return name;
21. }
22. public void setName(String name) {
23. this.name = name;
24. }
25. public Integer getPrice() {
26. return price;
27. }
28. public void setPrice(Integer price) {
29. this.price = price;
30. }
31. public Integer getNumbers() {
32. return numbers;
33. }
34. public void setNumbers(Integer numbers) {
35. this.numbers = numbers;
36. }
37. public Goods(Integer id, String name, Integer price, Integer numbers) {
38. super();
39. this.id = id;
40. this.name = name;
41. this.price = price;
42. this.numbers = numbers;
43. }
44.
45.}
46.
47.public class MyGwcDemo {
48. Map<Integer,Goods> al=new HashMap<Integer,Goods网页购物车代码>();
49. //添加购物车
50. public void cun() {
51. Scanner sc=new Scanner(System.in);
52. System.out.println("请输入商品的ID");
53. Integer id = sc.nextInt();
54. Goods m=new Goods();
55. ainsKey(id)) {
56. al.get(id).setNumbers((al.get(id).getNumbers()+1));
57. }else {
58. m.setId(id);
59. System.out.println("请输入商品的名字");
60. String name = sc.next();
61. m.setName(name);
62. System.out.println("请输入商品的价格");
63. int jia = sc.nextInt();
64. m.setPrice(jia);
65. m.setNumbers(1);
66. al.put(id, m);
67. }
68.// sc.close();
69. }
70. //查看一件
71. public void cha() {
72. System.out.println("请输入要查看商品的ID");
73. Scanner sc=new Scanner(System.in);
74. Integer id = sc.nextInt();
75. System.out.println("Id\t名字\t价格\t数量");
76. System.out.(id).getId()+"\t"+al.get(id).getName()+"\t"+al.get(id).getPrice()+"\t"+al.get(id).getNumbers());
77. //sc.close();
78. }
79. //删除商品
80. public void deleate(){
81. System.out.println("请输入要删除的商品的ID");
82. Scanner sc=new Scanner(System.in);
83. Integer id = sc.nextInt();
84. al.remove(id);
85. //sc.close();
86. }
87. //查看购物车
88. public void View() {
89. for(Goods g:al.values()) {
90. System.out.println("Id\t名字\t价格\t数量");
91. System.out.Id()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getNumbers());
92. }
93. }
94. public static void main(String[] args) {
95. MyGwcDemo myg=new MyGwcDemo();
96. //Scanner sc =new Scanner(System.in);
97. Scanner sc =new Scanner(System.in);
98. do {
99. System.out.println("1.添加商品\t 2.查看商品\t 3.删除商品 \t 4.查看所有商品\t 5.退出");
100. Integer it = sc.nextInt();
101. switch(it) {
102. case 1: myg.cun();
103. break;
104. case 2:myg.cha();
105. break;
106. case 3:myg.deleate();
107. break;
108. case 4:myg.View();
109. break;
110. case 5:
111. sc.close();
112. it(0);
113. default:System.out.println("输入有误,请重新输入");
114. break;
115. }
116.
117. }while(true);
118.
119. }
120.}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论