来一个水果超市的案例,控制台版的,以后做个界面出来!

自己现在的技术水平还很菜!要加油!

import java.util.ArrayList;

import java.util.Scanner;

public class GoodsMarket {
public static void main(String[] args) {
ArrayList list = new ArrayList();
init(list);
while (true) {
mainMenu();
choosFunction(list);
}
}
private static void choosFunction(ArrayList list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的选择:");
int num = sc.nextInt();
switch (num) {
case 1:
showGoodsList(list);
break;
case 2:
addGoods(list);
break;
case 3:
deletGoods(list);
break;
case 4:
setGoods(list);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("您输入的数据有误!");
break;
}
}
private static void setGoods(ArrayList list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改商品的编号:");
int num = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).number == num) {
System.out.print("请输入商品的编号:");
list.get(i).number = sc.nextInt();
System.out.print("请输入商品的名字:");
list.get(i).name = sc.next();
System.out.print("请输入商品的价格:");
list.get(i).price = sc.nextDouble();
System.out.println("商品修改成功!");
}
}
}
private static void deletGoods(ArrayList list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除商品的编号:");
int num = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).number == num) {
list.remove(i);
System.out.println("商品删除成功!");
}
}
}
private static void addGoods(ArrayList list) {
Goods g = new Goods();
Scanner sc = new Scanner(System.in);
System.out.print("请输入添加商品的编号:");
g.number = sc.nextInt();
System.out.print("请输入添加商品的名字:");
g.name = sc.next();
System.out.print("请输入添加商品的价格:");
g.price = sc.nextDouble();
list.add(g);
System.out.println("商品添加成功!");
}


private static void showGoodsList(ArrayList list) {
System.out.println("=======================商品库存清单=======================");
System.out.println("商品编号\t商品名称\t\t商品单价");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).number + "\t" + list.get(i).name+ "\t" + list.get(i).price);
}
}
private static void mainMenu() {
System.out.println("=========================欢迎光临itcast超市=========================");
System.out.println("1:查询货物  2:添加新货物 3:删除货物 4:修改货物");
System.out.println("5:退出系统");
}
private static void init(ArrayList list) {
Goods item = new Goods();
item.name = "少林寺酥饼核桃";
item.number = 9001;
item.price = 120;


Goods item2 = new Goods();
item2.name = "尚康杂粮牡丹饼";
item2.number = 9002;
item2.price = 20;


Goods item3 = new Goods();
item3.name = "新疆原产哈密瓜";
item3.number = 9003;
item3.price = 3;


list.add(item);
list.add(item2);
list.add(item3);
}
}

你可能感兴趣的:(Java基础知识)