1 package com.oracle.demo01;
2
3 import java.util.ArrayList;
4 import java.util.Scanner;
5
6 public class Shop {
7
8 public static void main(String[] args) {
9
10
11 Scanner sc = new Scanner(System.in);
12 System.out.println("-----------------用户登录----------------");
13 System.out.println("1、登陆");
14 System.out.println("2、退出");
15 int choose = sc.nextInt();
16 switch (choose) {
17 case 1:
18 record();
19 break;
20 case 2:
21 return;
22 default:
23 System.out.println("您的输入有误,请重新输入");
24 break;
25
26 }
27 }
28
29 //登录页面
30 public static void record(){
31 System.out.println("-----------------用户登录----------------");
32 System.out.println("请输入用户名:");
33 Scanner sc = new Scanner(System.in);
34 String user=sc.next();
35 System.out.println("请输入密码:");
36 String postword=sc.next();
37 if(user.equals(user)&&postword.equals("123456")){
38
39 show();
40 }else{
41 System.out.println("输入用户名或密码不正确,请重新输入:");
42 record();
43 }
44 }
45 // 主页面
46 public static void show() {
47 System.out.println("---------------欢迎光临oracle超市--------------");
48 System.out.println("1.货物清单");
49 System.out.println("2.添加货物");
50 System.out.println("3.删除货物");
51 System.out.println("4.修改货物");
52 System.out.println("5.退出");
53 System.out.println("请输入您的选择");
54 ArrayList arr=new ArrayList();
55 Goods g1=new Goods();
56 g1.gid=9001;
57 g1.gname="少林寺酥饼核桃";
58 g1.price=120;
59 Goods g2=new Goods();
60 g2.gid=9002;
61 g2.gname="尚康杂粮杜丹饼";
62 g2.price=20;
63 Goods g3=new Goods();
64 g3.gid=9003;
65 g3.gname="新疆原产哈密瓜";
66 g3.price=7;
67 arr.add(g1);
68 arr.add(g2);
69 arr.add(g3);
70 while (true) {
71
72 Scanner sc = new Scanner(System.in);
73 int choose = sc.nextInt();
74 switch (choose) {
75 case 1:// 货物清单
76 getGoods(arr);
77 break;
78 case 2:// 添加货物
79 addGoods(arr);
80 break;
81 case 3:// 删除货物
82 deleteGoods(arr);
83 break;
84 case 4:// 修改货物
85 updateGoods(arr);
86 break;
87 case 5: //退出
88 record();
89 return;
90 default:
91 System.out.println("您的输入有误,请重新输入");
92 break;
93
94 }
95 }
96 }
97
98 //货物清单
99 public static void getGoods(ArrayList list ) {
100 System.out.println("--------------------货物清单------------------");
101 System.out.println("商品编号\t商品名称\t商品价格");
102 for(int i=0;i
103 System.out.println(list.get(i).gid+
104 "\t\t"+list.get(i).gname+"\t"+list.get(i).price);
105 }
106 }
107 //添加货物
108 public static void addGoods(ArrayList list ) {
109 System.out.println("--------------------添加货物------------------");
110 System.out.println("请输入你要添加的商品编号:");
111 Scanner sc = new Scanner(System.in);
112 int gid=sc.nextInt();
113 System.out.println("请输入你要添加的商品名称:");
114 String gname=sc.next();
115 System.out.println("请输入你要添加的商品价格:");
116 double price=sc.nextDouble();
117 Goods goods=new Goods();
118 goods.gid=gid;
119 goods.gname=gname;
120 goods.price=price;
121 //将对象存入集合中
122 list.add(goods);
123 }
124 //修改货物
125 public static void updateGoods(ArrayList list ) {
126 //查询所有货物
127 getGoods(list);
128 System.out.println("请输入你要修改的商品编号:");
129 Scanner sc = new Scanner(System.in);
130 int gid=sc.nextInt();
131 System.out.println("请输入你要修改后的商品编号:");
132 String gname=sc.next();
133 System.out.println("请输入你要修改后的商品价格:");
134 double price=sc.nextDouble();
135 //寻找要修改的商品编号所对应的对象
136 for(int i=0;i
137 //从每一个对象中取出gid与用户输入的gid比较
138 //如果相同就找到了要修改的对象
139 if(list.get(i).gid==gid){
140 //修改商品名称和价格
141 list.get(i).gname=gname;
142 list.get(i).price=price;
143 }
144 }
145 }
146 //删除货物
147 public static void deleteGoods(ArrayList list ) {
148 getGoods(list);
149 System.out.println("请输入你要删除的商品编号:");
150 Scanner sc = new Scanner(System.in);
151 int gid=sc.nextInt();
152 for(int i=0;i
153 if(list.get(i).gid==gid){
154 list.remove(i);
155 }
156 }
157 }
158 }