案例6-1 库存管理系统

This commit is contained in:
seahi 2024-11-21 09:35:47 +08:00
parent c0479e5bba
commit c21520d8ee
5 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,4 @@
package chapter6;
public class Example04 {
}

View File

@ -0,0 +1,4 @@
package chapter6;
public class Example05 {
}

View File

@ -0,0 +1,4 @@
package chapter6;
public class Example3 {
}

View File

@ -0,0 +1,44 @@
package chapter6.demo61;
/**
* 商品类
*/
public class Goods {
private String name;
private double price;
private int num;
public Goods(String name, double price, int num) {
this.name = name;
this.price = price;
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String toString() {
return name + "\t" + price + "\t" + num;
}
}

View File

@ -0,0 +1,81 @@
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);
System.out.println("商品出库成功,出库后仓库商品如下:");
showWareHouse();
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) {
goodsList.remove(index);
}
// 使用迭代器遍历商品
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("==========================");
}
}