先声明一点,是一个刚学Javase的新手,有许多不足,希望大家见谅和建议。
代码1.ShopStockManage.java
import java.util.ArrayList;
import java.util.Scanner;
/*
* 1.商品数据初始化
* 2.用户选择菜单
* 3 选择不同的功能
* 3.1 Read
* 3.2 Add
* 3.3 Delete
* 3.4 Update
*
* 所有功能用方法实现
* main调用功能
*/
public class ShopStockManage {
* 1.商品数据初始化
* 2.用户选择菜单
* 3 选择不同的功能
* 3.1 Read
* 3.2 Add
* 3.3 Delete
* 3.4 Update
*
* 所有功能用方法实现
* main调用功能
*/
public class ShopStockManage {
public static void main(String[] args) {
ArrayList<GoodsImformation> array = new ArrayList<GoodsImformation>();
initGoods(array);
while(true){
mainMenu();
int choose = chooseFunction();
switch (choose) {
case 1:
readGoogs(array);
break;
case 2:
addGoogs(array);
break;
case 3:
deleteGoods(array);
break;
case 4:
updateGoods(array);
break;
case 5:
return;
default:
System.out.println("输入的序号没有");
break;
}
}
}
ArrayList<GoodsImformation> array = new ArrayList<GoodsImformation>();
initGoods(array);
while(true){
mainMenu();
int choose = chooseFunction();
switch (choose) {
case 1:
readGoogs(array);
break;
case 2:
addGoogs(array);
break;
case 3:
deleteGoods(array);
break;
case 4:
updateGoods(array);
break;
case 5:
return;
default:
System.out.println("输入的序号没有");
break;
}
}
}
// 1.商品数据初始化
public static void initGoods(ArrayList<GoodsImformation> array){
GoodsImformation gf1 = new GoodsImformation();
gf1.ID = 1000;
gf1.name = "可乐";
gf1.price = 5.7;
GoodsImformation gf2 = new GoodsImformation();
gf2.ID = 1001;
gf2.name = "伊利";
gf2.price = 15.7;
GoodsImformation gf3 = new GoodsImformation();
gf3.ID = 1002;
gf3.name = "蒙牛";
gf3.price = 115.7;
array.add(gf1);
array.add(gf2);
array.add(gf3);
}
// 2.选择菜单,提示用户有哪些选择
public static void mainMenu(){
System.out.println("====================欢迎光临ItCast超市====================");
System.out.println("1:货物清单 2:添加货物 3:删除货物 4:修改货物 5:退出 ");
System.out.println("请输入要操作的序号");
}
// 3.实现从键盘输入
public static int chooseFunction(){
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
// 4.显示货物清单
public static void readGoogs(ArrayList<GoodsImformation> array){
System.out.println();
System.out.println("====================商品库存清单====================");
System.out.println("商品编号 商品名称 商品单价");
for(int i = 0; i< array.size(); i++){
GoodsImformation goods = array.get(i);
System.out.println(goods.ID + " " + goods.name+" "+goods.price);
}
}
// 5.添加商品信息
public static void addGoogs(ArrayList<GoodsImformation> array){
System.out.println("你选择的是添加商品信息");
Scanner sc = new Scanner(System.in);
System.out.println("输入商品的编号");
int id = sc.nextInt();
System.out.println("请输入商品名称");
String name = sc.next();
System.out.println("请输入商品的单价");
double price = sc.nextDouble();
GoodsImformation goods = new GoodsImformation();
goods.ID = id;
goods.name = name;
goods.price = price;
array.add(goods);
System.out.println("添加成功");
}
// 6.删除商品信息
public static void deleteGoods(ArrayList<GoodsImformation> array){
System.out.println("你选择的是删除商品");
System.out.println("输入要删除商品的编号");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for(int i = 0; i < array.size();i++){
GoodsImformation goods = array.get(i);
if(goods.ID == id){
array.remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("你输入的编号不存在");
}
// 7.修改商品信息
public static void updateGoods(ArrayList<GoodsImformation> array){
System.out.println("你选择的是修改功能");
System.out.println("输入要修改的商品编号");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for(int i = 0;i < array.size(); i++){
GoodsImformation goods = array.get(i);
if(goods.ID == id){
System.out.println("输入新的商品编号");
goods.ID = sc.nextInt();
System.out.println("输入商品的名字");
goods.name = sc.next();
System.out.println("输入商品的价格");
goods.price = sc.nextDouble();
System.out.println("修改成功");
return;
}
}
System.out.println("你输入的编号不存在");
}
}
public static void initGoods(ArrayList<GoodsImformation> array){
GoodsImformation gf1 = new GoodsImformation();
gf1.ID = 1000;
gf1.name = "可乐";
gf1.price = 5.7;
GoodsImformation gf2 = new GoodsImformation();
gf2.ID = 1001;
gf2.name = "伊利";
gf2.price = 15.7;
GoodsImformation gf3 = new GoodsImformation();
gf3.ID = 1002;
gf3.name = "蒙牛";
gf3.price = 115.7;
array.add(gf1);
array.add(gf2);
array.add(gf3);
}
// 2.选择菜单,提示用户有哪些选择
public static void mainMenu(){
System.out.println("====================欢迎光临ItCast超市====================");
System.out.println("1:货物清单 2:添加货物 3:删除货物 4:修改货物 5:退出 ");
System.out.println("请输入要操作的序号");
}
// 3.实现从键盘输入
public static int chooseFunction(){
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
// 4.显示货物清单
public static void readGoogs(ArrayList<GoodsImformation> array){
System.out.println();
System.out.println("====================商品库存清单====================");
System.out.println("商品编号 商品名称 商品单价");
for(int i = 0; i< array.size(); i++){
GoodsImformation goods = array.get(i);
System.out.println(goods.ID + " " + goods.name+" "+goods.price);
}
}
// 5.添加商品信息
public static void addGoogs(ArrayList<GoodsImformation> array){
System.out.println("你选择的是添加商品信息");
Scanner sc = new Scanner(System.in);
System.out.println("输入商品的编号");
int id = sc.nextInt();
System.out.println("请输入商品名称");
String name = sc.next();
System.out.println("请输入商品的单价");
double price = sc.nextDouble();
GoodsImformation goods = new GoodsImformation();
goods.ID = id;
goods.name = name;
goods.price = price;
array.add(goods);
System.out.println("添加成功");
}
// 6.删除商品信息
public static void deleteGoods(ArrayList<GoodsImformation> array){
System.out.println("你选择的是删除商品");
System.out.println("输入要删除商品的编号");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for(int i = 0; i < array.size();i++){
GoodsImformation goods = array.get(i);
if(goods.ID == id){
array.remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("你输入的编号不存在");
}
// 7.修改商品信息
public static void updateGoods(ArrayList<GoodsImformation> array){
System.out.println("你选择的是修改功能");
System.out.println("输入要修改的商品编号");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for(int i = 0;i < array.size(); i++){
GoodsImformation goods = array.get(i);
if(goods.ID == id){
System.out.println("输入新的商品编号");
goods.ID = sc.nextInt();
System.out.println("输入商品的名字");
goods.name = sc.next();
System.out.println("输入商品的价格");
goods.price = sc.nextDouble();
System.out.println("修改成功");
return;
}
}
System.out.println("你输入的编号不存在");
}
}
代码2.GoodsImformation.java
/*
* 自定义商品信息的类
* 编号
* 品名
* 价格
* 库存
* 总金额
* 商品类型,存储到集合ArrayList中
*
*/
public class GoodsImformation {
int ID;
String name;
double price;
int number;
double money;
}
* 自定义商品信息的类
* 编号
* 品名
* 价格
* 库存
* 总金额
* 商品类型,存储到集合ArrayList中
*
*/
public class GoodsImformation {
int ID;
String name;
double price;
int number;
double money;
}