测试类
package 练习.商品入库系统;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static final int Enrool = 1;
public static final int Search = 2;
public static final int Delect = 3;
public static final int Exit = 4;
public static void main(String[] args) {
//创建手机类的商品集合
ArrayList<Phone> goods = new ArrayList<>();
// 用户的操作
Choice(goods);
}
// 登记商品
public static void Enroll(ArrayList<Phone> goods){
Scanner sc = new Scanner(System.in);
System.out.println("请输入商品品牌:");
Phone goods1 = new Phone();
String name = sc.next();
goods1.setName(name);
System.out.println("请输入商品价格:");
double price = sc.nextDouble();
goods1.setPrice(price);
System.out.println("请输入商品型号:");
String configuration = sc.next();
goods1.setConfiguration(configuration);
System.out.println("请输入商品库存数:");
int storage = sc.nextInt();
goods1.setStorage(storage);
goods.add(goods1);
System.out.println("添加成功");
}
// 查询商品
public static void Search(ArrayList<Phone> goods){
System.out.println("输入你要查询的商品品牌");
Scanner sc = new Scanner(System.in);
String name = sc.next();
// 判断商品是否在仓库如果在就输出,不在就提示
for (int i = 0; i < goods.size(); i++) {
if(goods.get(i).getName().equals(name)){
System.out.println("品牌:"+goods.get(i).getName()+"\t价格:"+goods.get(i).getPrice()+"元\t型号:"+goods.get(i).getConfiguration()+"\t\t剩余库存:"+goods.get(i).getStorage()+"部\t剩余库存总价值:"+goods.get(i).Sum()+"元");
} else if (i== goods.size()-1) {
System.out.println("仓库无此品牌商品");
}
}
}
// 删除商品
public static void Delete(ArrayList<Phone> goods){
Scanner sc = new Scanner(System.in);
System.out.println("输入要删除的品牌");
String name = sc.next();
// 检索要删除的品牌的所有型号
for (int i = 0; i < goods.size(); i++) {
if (goods.get(i).getName().equals(name)) {
System.out.println("要删除的品牌所有型号信息如下:");
System.out.println("品牌:" + goods.get(i).getName() + "\t价格:" + goods.get(i).getPrice() + "元\t型号:" + goods.get(i).getConfiguration() + "\t\t剩余库存:" + goods.get(i).getStorage() + "部\t剩余库存总价值:" + goods.get(i).Sum() + "元");
} else if (i == goods.size() - 1) {
System.out.println("仓库无此品牌商品");
}
}
// 删除相应的品牌的特定型号的数据
System.out.println("输入要删除的型号:");
String configuration = sc.next();
for (int i = 0; i < goods.size(); i++) {
if (goods.get(i).getConfiguration().equals(configuration)) {
goods.remove(i);
System.out.println("删除成功");
} else if (i == goods.size() - 1) {
System.out.println("仓库无此型号商品");
}
}
}
// 用户的操作
public static void Choice(ArrayList<Phone> goods){
Scanner sc = new Scanner(System.in);
// 结束标志
do {
System.out.println("请选择你的操作:1.登记商品\t2.查询商品\t3.删除商品\t4.退出");
int choice = sc.nextInt();
switch (choice) {
// 登记商品
case Enrool:
Enroll(goods);
break;
// 查询商品
case Search:
Search(goods);
break;
// 删除商品
case Delect:
Delete(goods);
break;
// 退出
case Exit:
System.exit(0);
default:
System.out.println("请输入1 2 3 4 执行操作");
}
}while(true);
}
}
手机类
package 练习.商品入库系统;
import java.math.BigDecimal;
public class Phone {
private String name;
private double price;
private String configuration;
private int storage;
private BigDecimal sum;
public Phone(){}
public Phone(String name, double price, String configuration, int storage, BigDecimal sum) {
this.name = name;
this.price = price;
this.configuration = configuration;
this.storage = storage;
this.sum = sum;
}
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 String getConfiguration() {
return configuration;
}
public void setConfiguration(String configuration) {
this.configuration = configuration;
}
public int getStorage() {
return storage;
}
public void setStorage(int storage) {
this.storage = storage;
}
public BigDecimal getSum() {
return sum;
}
public void setSum(BigDecimal sum) {
this.sum = sum;
}
public BigDecimal Sum(){
BigDecimal sum = BigDecimal.valueOf(getPrice() * getStorage());
setSum(sum.setScale(2, BigDecimal.ROUND_HALF_DOWN));
return getSum();
}
}