|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
package com.itheima_02;
import java.util.ArrayList;
import java.util.Scanner;
public class ShopMain {
public static void main(String[] args) {
ArrayList<Shop> array = new ArrayList<Shop>();
init(array);
menu();
while(true) {
int choose = input();
switch(choose) {
case 1:
print_all(array);
break;
case 2:
add_shop(array);
break;
case 3:
remove_shop(array);
break;
case 4:
add_shop(array);
remove_shop(array);
break;
case 5:
return;
default:
System.out.println("输入错误");
}
}
}
//初始化
public static void init(ArrayList<Shop> array) {
Shop p = new Shop("三只松鼠",9001,90.01);
Shop p1 = new Shop("小馒头",1500,90.02);
Shop p2 = new Shop("旺仔牛奶",3001,20.45);
array.add(p);
array.add(p1);
array.add(p2);
}
//输入
public static int input() {
Scanner sc = new Scanner(System.in);
System.out.println("-----------------");
System.out.print("输入");
int choose = sc.nextInt();
return choose;
}
//货物清单
public static void print_all(ArrayList<Shop> array) {
System.out.println("============货物清单==============\n");
System.out.println("============商品库存清单==============");
System.out.println("商品编号 商品名称 商品单价");
for (int i = 0; i < array.size(); i++) {
Shop p1 = array.get(i);
System.out.println(p1.shopnumber+" "+p1.name+" "+p1.price);
}
}
//添加货物
public static void add_shop(ArrayList<Shop> array) {
System.out.println("===========添加新货物=============\n");
Scanner sc = new Scanner(System.in);
System.out.print("请输入新商品的名称:");
String new_name = sc.nextLine();
System.out.print("请输入新商品的编号:");
int number = sc.nextInt();
System.out.print("请输入新商品单价:");
double price2 = sc.nextDouble();
//创建对象
Shop p = new Shop(new_name,number,price2);
//添加到集合
array.add(p);
}
//删除货物
public static void remove_shop(ArrayList<Shop> array) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要删除的商品编号:");
int number = sc.nextInt();
//判断是否存在
if(find(number,array)) {
//删除
for(int i = 0;i < array.size();i++) {
Shop p1 = array.get(i);
if(p1.shopnumber == number) {
array.remove(p1);
}
}
}else {
System.out.println("没有这个商品!");
}
}
//判断是否存在
public static boolean find(int number,ArrayList<Shop> array) {
//遍历数组
for (int i = 0; i < array.size(); i++) {
Shop p1 = array.get(i);
if(p1.shopnumber == number) {
return true;
}
}
return false;
}
public static void menu() {
System.out.println("=============欢迎光临itcast超市=============");
System.out.println("1.货物清单 2.添加货物 3.删除货物 4.修改货物");
System.out.println("5.退出系统");
System.out.println("请你输入要操作的功能序号:");
System.out.println();
}
}
|
|