import java.util.Scanner;
//增加商品类
//此类用来录入一个商品的所有属性,并作为结果对其返回
public class Add {
public Goods add1() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品名称");
String name = scanner.next();
System.out.println("请输入商品数量");
int shuliang = scanner.nextInt();
System.out.println("请输入商品价格");
float jiage = scanner.nextFloat();
System.out.println("请输入商品产地");
String chandi = scanner.next();
System.out.println("请输入商品编号");
int bianhao = scanner.nextInt();
return new Goods(shuliang, name, chandi, jiage, bianhao);
}
}
import java.util.ArrayList;
import java.util.Scanner;
//此类为删除商品类
//获取一个需要删除的商品的主键(biuanhao),并且返回bianhao
public class Delete {
public int delete(ArrayList<Goods> goods) {
//创建打印机类
Scanner scanner = new Scanner(System.in);
//此处int 类型的bianhao表示需要删除的元素的编号
System.out.println("请输入要删除的商品编号");
int bianhao = scanner.nextInt();
for (int i = 0; i < goods.size(); i++) {
Goods goods1 = goods.get(i);
if (goods1.getBianhao() == bianhao) {
goods.remove(i);
rz rz =new rz();
rizhi rizhi = new rizhi();
String time=rizhi.rizhi1();
rz.setTime(time);
rz.setCaozuo("删除商品");
return i;
}
}
System.out.println("抱歉,没有该编号的商品");
return 0;
}
}
public class Goods {
//无参构造函数
public Goods(){}
private int shuliang;
private String name;
private String chandi;
private float jiage;
private int bianhao;
public int getShuliang() {
return shuliang;
}
public void setShuliang(int shuliang) {
this.shuliang = shuliang;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getChandi() {
return chandi;
}
public void setChandi(String chandi) {
this.chandi = chandi;
}
public double getJiage() {
return jiage;
}
public void setJiage(float jiage) {
this.jiage = jiage;
}
public int getBianhao() {
return bianhao;
}
public void setBianhao(int bianhao) {
this.bianhao = bianhao;
}
@Override
public String toString() {
return "Goods{" +
"shuliang=" + shuliang +
", name='" + name + '\'' +
", chandi='" + chandi + '\'' +
", jiage=" + jiage +
", bianhao=" + bianhao +
'}';
}
public Goods(int shuliang, String name, String chandi, float jiage, int bianhao) {
this.shuliang = shuliang;
this.name = name;
this.chandi = chandi;
this.jiage = jiage;
this.bianhao = bianhao;
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
//记录销售数量
public class inOut {
public int inOut1(ArrayList<Goods> goods) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入销售的商品编号");
int bianhao = scanner.nextInt();
System.out.println("请输入销售的数量");
int shuliang = scanner.nextInt();
rz rz = new rz();
rizhi rizhi = new rizhi();
String time=rizhi.rizhi1();
rz.setTime(time);
rz.setCaozuo("商品出货");
for (int i = 0; i < goods.size(); i++) {
if (bianhao == goods.get(i).getBianhao()) {
int n=goods.get(i).getShuliang()-shuliang;
goods.get(i).setShuliang(n);
break;
}
}
return 0;
}
}
import java.util.ArrayList;
import java.util.Scanner;
//此类提供修改功能的子功能
//返回用户输入的商品编号对应的商品在ArrayList<>容器中的索引值
public class Set {
public int Set1(ArrayList<Goods> goods) {
System.out.println("请输入需要修改商品的编号");
Scanner scanner = new Scanner(System.in);
int bianhao = scanner.nextInt();
//判断是否有这个编号的商品
for (int i = 0; i < goods.size(); i++) {
if (goods.get(i).getBianhao() == bianhao) {
return i;
}
}
System.out.println("🐼提醒您,您输入的编号有误,数据库中没有编号为:"+bianhao+"的商品");
return -1;
}
}
package 集合框架.单列.学生管理系统.商品管理系统;
import java.util.ArrayList;
import java.util.Scanner;
//该类是用来接收Set类中的Set1()函数返回的值
public class Set2 {
public void Set3(ArrayList<Goods> goods, int i) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要修改的数据");
System.out.println("1.名称");
System.out.println("2.编号");
System.out.println("3.产地");
System.out.println("4.价格");
System.out.println("5.数量");
int i1 = scanner.nextInt();
if (i1 == 1) {
System.out.println("请输入修改后名称");
goods.get(i).setName(scanner.next());
} else if (i1 == 2) {
if (goods.size() != 1) {
//创建while1变量
//初始值为0,在进入while循环后会将其改为1
//如果while循环中嵌套的for循环中的if判断为正确,代表有编号重复
//此时将while1的值再次改为0,使其仍然满足while循环
int while1 = 0;
while (while1 == 0) {
System.out.println("请输入修改后的编号");
//i2用来接收修改后的编号
int i2 = scanner.nextInt();
goods.get(i).setBianhao(i2);
while1 = 1;
//遍历集合元素
for (int i3 = 0; i3 < goods.size(); i3++) {
//判断是否编号重复,如果新编号与此商品的原来编号重复,则判断结果为false,不会将while1的值改为0
if (i2 == goods.get(i3).getBianhao() && i2 != goods.get(i).getBianhao()) {
System.out.println("编号重复无法修改,请输入唯一的编号");
while1 = 0;
break;
}
}
}
} else {
System.out.println("请输入修改后编号");
goods.get(i).setBianhao(scanner.nextInt());
}
} else if (i1 == 3) {
System.out.println("请输入修改后的产地");
goods.get(i).setChandi(scanner.next());
} else if (i1 == 4) {
System.out.println("请输入修改后的价格");
goods.get(i).setJiage(scanner.nextFloat());
} else if (i1 == 5) {
System.out.println("请输入修改后的数量");
goods.get(i).setShuliang(scanner.nextInt());
}
rz rz =new rz();
rizhi rizhi = new rizhi();
String time=rizhi.rizhi1();
rz.setTime(time);
rz.setCaozuo("修改商品");
}
}
import java.util.ArrayList;
//该类为输出所有元素的类
public class to {
public void to1(ArrayList<Goods> goods) {
System.out.println("编号 名称 数量 价格 产地");
for (int i = 0; i < goods.size(); i++) {
System.out.print(goods.get(i).getBianhao() + " ");
System.out.print(goods.get(i).getName() + " ");
System.out.print(goods.get(i).getShuliang() + " ");
System.out.print(goods.get(i).getJiage() + " ");
System.out.println(goods.get(i).getChandi() + " ");
}
rizhi rizhi = new rizhi();
rz rz = new rz();
String time = rizhi.rizhi1();
rz.setTime(time);
rz.setCaozuo("查看元素");
}
}
import java.util.ArrayList;
//库存预警
public class yujing {
public void yujing1(ArrayList<Goods> goods) {
for (int i = 0; i < goods.size(); i++) {
int n = goods.get(i).getShuliang();
if (n < 10) {
System.out.println("🐼提醒您"+goods.get(i).getName() + "数量为" + n + "建议补货");
}
}
}
}
import java.util.ArrayList;
//输入异常处理类
public class yichang {
public void yichang1(ArrayList<Goods> goods){
Add add = new Add();
rizhi rizhi = new rizhi();
rz rz = new rz();
try{goods.add(add.add1());
String time =rizhi.rizhi1();
rz.setTime(time);
rz.setCaozuo("添加商品");
}
catch(java.util.InputMismatchException e){
System.out.println("🐼提醒您价格或数量输入错误🐼已删除该商品信息,请重新输入");
yichang1(goods);
}
}
}
package 集合框架.单列.学生管理系统.商品管理系统;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//构建存储容器
ArrayList<Goods> goods = new ArrayList<>();
//构建日志存储器
ArrayList<rz> rz1 = new ArrayList<>();
Delete delete = new Delete();
to to = new to();
Add Add = new Add();
Set set = new Set();
Set2 set2 = new Set2();
yichang yichang = new yichang();
inOut inOut = new inOut();
yujing yujing = new yujing();
torz torz = new torz();
Scanner scanner = new Scanner(System.in);
// 此处xuhao用来接收demo()函数返回的demo这个方法里面的xuhao
for (; ; ) {
//进行预警
yujing.yujing1(goods);
//生成操作界面
int xuhao = demo();
if (xuhao == 1) {
yichang.yichang1(goods);
//检查是否重复编号,并删除重复编号的商品
//要判断是否为一个商品,如果仅为一个商品必然不可能重复编号
if (goods.size() != 1) {
//定义一个i1,如果下面的if判断为true就改成0,这样就可以把通过if判断的goods元素(编号重复的商品)无法通过第二个if判断
//即无法输出商品添加成功
int i1 = 1;
for (int i = 0; i < goods.size() - 1; i++) {
//判断最后一个元素,(也就是刚刚加入的商品)的属性——编号是否与其他元素的该属性相同
//为了不与最后一个元素(该商品自己本身)进行比较,循环的判定条件:i要小于集合的长度-1
//以避免与自己本身进行比较,如果不进行此操作,则集合中的编号必然重复
if (goods.get(goods.size() - 1).getBianhao() == goods.get(i).getBianhao()) {
System.out.println("🐼提醒您,您输入的编号有重复");
goods.remove(goods.size() - 1);
System.out.println("🐼已帮您删除您刚刚输入的商品");
i1 = 0;
}
// else {
// System.out.println("商品添加成功");
// break;
// }
}
if (i1 == 1) {
System.out.println("商品添加成功");
}
} else {
System.out.println("商品添加成功");
}
}
if (xuhao == 2) {
to.to1(goods);
}
if (xuhao == 3) {
//如果i=-1代表编号重复,则不调用执行修改功能的函数Set2
int i = set.Set1(goods);
if (i != -1) {
set2.Set3(goods, i);
}
}
if (xuhao == 4) {
delete.delete(goods);
}
if (xuhao == 5) {
inOut.inOut1(goods);
}
if (xuhao == 6) {
System.out.println("您确定要退出🐼商品管理系统吗?");
System.out.println("如果确实退出请输入----确认退出,不想退出请输入其他任意键");
String yes_Or_no = scanner.next();
if (yes_Or_no.equals("确认退出")) {
System.out.println("感谢您的使用,🐼商品管理系统即将退出");
System.out.println("-----退出成功----");
break;
}
}
}
}
public static int demo() {
System.out.println("===============欢迎使用🐼商品管理系统===============");
System.out.println("1.新增商品");
System.out.println("2.查询商品");
System.out.println("3.修改商品");
System.out.println("4.删除商品");
System.out.println("5.商品出货");
System.out.println("6.退出系统");
System.out.println("请选择[1--6]");
Scanner scanner = new Scanner(System.in);
//此处的变量序号为选择功能时的序号
int xuhao = scanner.nextInt();
return xuhao;
}
}