Java2024/src/chapter6/demo61/Manager.java
2024-11-25 12:35:57 +08:00

86 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package chapter6.demo61;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Manager {
static ArrayList goodsList = new ArrayList<>();
// static ArrayList<Goods> goodsList = new ArrayList<>();
public static void main(String[] args) {
Goods meizu = new Goods("魅族22", 3999, 11);
goodsList.add(meizu);
goodsList.add(new Goods("小米15", 6899, 10));
goodsList.add(new Goods("iPhone15", 5999, 2));
while (true) {
System.out.println("欢迎使用库房管理系统");
System.out.println("1. 商品入库");
System.out.println("2. 商品显示");
System.out.println("3. 商品出库");
System.out.print("请选择要进行的操作:");
Scanner sc = new Scanner(System.in);
int select = sc.nextInt();
switch (select) {
case 1:
addGoods();
System.out.println("商品入库成功,入库后仓库商品如下:");
showWareHouse();
break;
case 2:
showWareHouse();
break;
case 3:
System.out.print("请输入要出库的商品编号:");
int id = sc.nextInt();
removeGoods(id);
break;
}
}
}
// 商品入库
static void addGoods() {
Scanner sc = new Scanner(System.in);
System.out.print("商品名称:");
String name = sc.next();
System.out.print("商品价格:");
double price = sc.nextDouble();
System.out.print("商品数量:");
int num = sc.nextInt();
// Goods g = new Goods(name, price, num);
// goodsList.add(g);
goodsList.add(new Goods(name, price, num));
}
// 商品出库从goodList中删除指定位置的元素
static void removeGoods(int index) {
if (index >= 0 && index < goodsList.size()) {
goodsList.remove(index);
System.out.println("商品出库成功,出库后仓库商品如下:");
showWareHouse();
} else {
System.out.println("商品不存在!");
}
}
// 使用迭代器遍历商品
static void showWareHouse() {
System.out.println("==========================");
Iterator iterator = goodsList.iterator();
while (iterator.hasNext()) {
Goods goods = (Goods) iterator.next(); // 类型转换Object->Goods
System.out.println(goods);
}
System.out.println("==========================");
}
}