java简单的管理系统(商品)源码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42337796/article/details/81841984

public class Product{//实体

//设置属性
private int Pid;
private String name;
private double price;
private int count;

//无参构造
public  Product(){}

//有参构造
public Product(int pid, String name, double price, int count) {
    super();
    Pid = pid;
    this.name = name;
    this.price = price;
    this.count = count;
}

//重写toString方法 
@Override
public String toString() {
    System.out.println("商品编号" +  "\t商品名称"  +"\t商品价格"  + "\t库存价格");
    return Pid   +"\t"+name + "\t"+price +"\t"+count;
}

public int getCount() {
    return count;
}
public void setCount(int count) {
    this.count = count;
}
public int getPid() {
    return Pid;
}

public interface ProductDAO {//接口

//添加商品
boolean addProduct (Product product);

//显示所有商品信息
void showAllProduct ();

//通过ID查找某一件商品信息
Product finfProductById (int id);

//根据id进行入库操作
boolean inPrduct (int id , int num);

//根据id进行出库操作
boolean outPrduct (int id , int num);

//根据id删除元素
boolean deleteProduct (int id);

}

public class ProductDAOImpl implements ProductDAO{//实现类

//创建一个集合对象
ArrayList products = new ArrayList();

@Override
public boolean addProduct(Product product) {
    // TODO Auto-generated method stub
    boolean add= products.add(product);
    return add;
}

@Override
public void showAllProduct() {
    // TODO Auto-generated method stub

    for(int i = 0;i<products.size();i++){
        System.out.println(products.get(i));
    }

}

@Override
public Product finfProductById(int id) {
    // TODO Auto-generated method stub
    //遍历数据
    for(int i =0;i<products.size();i++){
        //找到对应的商品
        Product product = (Product) products.get(i);
        if(product.getPid()==id){
            return product;
        }
    }
    return null;


}

@Override
public boolean inPrduct(int id, int num) {
    // TODO Auto-generated method stub
    for(int i=0;i<products.size();i++){
        Product product = (Product) products.get(i);
        if(product.getPid()==id){
            //获取原来的数据
            int numOld = product.getCount();
            //更新数据
            int numNew = numOld + num;
            //给对象的数据进行更新
            product.setCount(numNew);


        }
    }
    return true;
}

@Override
public boolean outPrduct(int id, int num) {
    // TODO Auto-generated method stub
    for(int i=0;i<products.size();i++){
        Product product = (Product) products.get(i);
            if(product.getPid()==id){
                //获取原来的数据
                int numOld = product.getCount();
                if(numOld<num){
                    System.out.println("库存不足请重新输入:");
                }else{
                //更新数据
                int numNew = numOld - num;
                //给对象的数据进行更新
                product.setCount(numNew);
            }
        }
    }
    return true;
}

@Override
public boolean deleteProduct(int id) {
    // TODO Auto-generated method stub
    for(int i=0;i<products.size();i++){
        Product product = (Product) products.get(i);
        if(product.getPid()==id){
            products.remove(product);
            System.out.println("删除成功");

        }
    }
    return true;
}

public static void showMainMenu(){//界面

    boolean falg = true; //定义一个boolean的值让循环停下来

    Scanner input  = new Scanner(System.in); //用来接收数字
    Scanner input1 = new Scanner(System.in); //用来接收字符

    //创建并保存对象
    Product product1 = new Product();

    //创建对象调用方法
    ProductDAOImpl pdi = new ProductDAOImpl();


    while(falg){
        System.out.println("**********小商品系统*************");
        System.out.println("1:录入商品信息");
        System.out.println("2:展示所有商品信息");
        System.out.println("3:根据id查找商品");
        System.out.println("4:商品入库");
        System.out.println("5:商品出库");
        System.out.println("6:删除指定商品");
        System.out.println("7:退出系统");
        System.out.print("请输入你的选择:");

        int num = input.nextInt();
        switch(num){
        case 1:{

            System.out.print("请输入id");
            int pid = input.nextInt();
            System.out.print("请输入名称");
            String pname = input1.nextLine();
            System.out.print("请输入价格");
            double price = input.nextDouble();
            System.out.print("请输入数量");
            int count = input.nextInt();

            product1 = new Product(pid, pname, price, count);
            pdi.addProduct(product1);

        }break;
        case 2:{
            pdi.showAllProduct();
        }break;

        case 3:{
            System.out.println("请输入要查找的id");
            int num1 = input.nextInt();
            pdi.finfProductById(num1);
            System.out.println(product1);

        }break;
        case 4:{
             System.out.println("请选择您要入库的商品id:");
             int num2 = input.nextInt();
             System.out.println("请选择您要入库的数量:");
             int num3 = input.nextInt();
             pdi.inPrduct(num2, num3);

        }break;
        case 5:{
            System.out.println("请选择您要出库的商品的id:");
            int num2 = input.nextInt(); 
            System.out.println("请选择您要出库的数量");
            int num3 = input.nextInt();
            pdi.outPrduct(num2, num3);

        }break;
        case 6:{
            System.out.println("请选择您要删除的商品信息");
            int num2 = input.nextInt();
            pdi.deleteProduct(num2);
        }break;
        case 7:{
            System.out.println("欢迎下次使用");
            falg = false;

        }break;
        }

    }
}

public class ProductTest {//product测试文本
public static void main(String[] args) {
MainMenu.showMainMenu();
}

}

展开阅读全文

没有更多推荐了,返回首页