库存管理系统(java)

掌握List集合、Set集合以及Map集合的使用;熟悉泛型的使用;掌握Iterator迭代器和foreach循环的使用。

像商城和超市这样的地方,都需要有自己的库房,并且库房商品的库存变化有专人记录,这样才能保证商城和超市正常运转。

本例要求编写一个程序,模拟库存管理系统。该系统主要包括系统首页、商品入库、商品显示和删除商品功能。每个功能的具体要求如下:

(1)系统的首页:用于显示系统所有的操作,并且可以选择使用某一个功能。

(2)商品入库功能:首先提示是否要录入商品,根据用户输入的信息判断是否需要录入商品。如果需要录入商品,则需要用户输入商品的名称、颜色、价格和数量等信息。录入完成后,提示商品录入成功并打印所有商品。如果不需要录入商品,则返回系统首页。

(3)商品显示功能:用户选择商品显示功能后,在控制台打印仓库所有商品信息。

(4)删除商品功能:用户选择删除商品功能后,根据用户输入的商品编号删除商品,并在控制台打印删除后的所有商品。

指导:

1.每一种商品都可以存储在一个Java对象中,所以先定义一个Goods类,属性包括name,color,price,number,方法包括上述属性的set和get方法以及构造方法。

2. 在主类中定义List接口对象,用ArrayList实现类的构造方法构造此对象。调用add()方法将Goods对象加入List对象中,使用remove()方法删除已加入List队列中的Goods对象。

3. 用foreach循环方式读取List对象中的数据并显示。

代码如下

package main;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
class Goods
{
    String name;
    int num;
    String colour;
    double price;
    Goods(String name,int num,String colour,double price)
    {
        this.name=name;
        this.num=num;
        this.colour=colour;
        this.price=price;
    }
    public String getName()
    {
        return name;
    }
    public int getNum()
    {
        return num;
    }
    public String getColour()
    {
        return colour;
    }
    public double getPrice()
    {
        return price;
    }
}

public class Test {
    public static void main(String[] args) {
        ArrayList<Goods> list = new ArrayList<Goods>();
        list.add(new Goods("牛奶", 150, "白色", 58.9));
        list.add(new Goods("洗衣液", 100, "蓝色", 69.9));
        list.add(new Goods("纸", 200, "黄色", 19.9));
        list.add(new Goods("青菜", 300, "绿色", 8.9));
        list.add(new Goods("猪肉", 250, "红色", 25.9));
        String st = "yes";
        while (st.equals("yes")) {
            System.out.println("欢迎使用库房管理系统,请选择要进行的操作");
            System.out.println("1. 商品入库");
            System.out.println("2. 商品显示");
            System.out.println("3. 删除商品");
            Scanner sc = new Scanner(System.in);
            int s = sc.nextInt();
            if (s == 1) {
                System.out.println("开始录入商品信息!");
                Scanner sc1 = new Scanner(System.in);
                System.out.println("请输入商品的名称:");
                String name = sc1.next();
                System.out.println("请输入商品的数量:");
                int num = sc1.nextInt();
                System.out.println("请输入商品的颜色:");
                String colour = sc1.next();
                System.out.println("请输入商品的价格:");
                double price = sc1.nextDouble();
                list.add(new Goods(name, num, colour, price));
            } else if (s == 2) {
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    Goods goods = (Goods) it.next();
                    System.out.println(goods.getName() + " " + goods.getNum() + " " + goods.getColour() + " " + goods.getPrice());
                }
            } else if (s == 3) {
                System.out.println("请输入要删除的商品的编号");
                Scanner sc3 = new Scanner(System.in);
                int a = sc3.nextInt();
                list.remove(a);
            }
            System.out.println("输入4则退出系统");
            Scanner sc5 = new Scanner(System.in);
            int c = sc5.nextInt();
            if (c == 4)
                break;
        }
    }
}

结果截图:

 

  • 21
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值