Java:实现商品的查找、添加、出库、入库

这是一个Java实现的简单商品管理系统,包括商品的查找、添加、入库和出库功能。使用HashMap存储商品信息,通过Scanner获取用户输入进行操作。商品类包含编号、名称、单价、单位和库存等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cn.kgc.goods;
/**
 * 
 * @author vip宅男
 *
 */
public class Goods {
/**
* 属性: 编号  名称  单价  单位  库存
*/

private int id;
private String name;
private double price;
private String uom;
private int balance;
/**
* 获取编号
* @return
*/

public int getId() {
return id;
}
/**
* 设置编号
* @param id
*/

public void setId(int id) {
this.id = id;
}
/**
* 获取名称
* @return
*/

public String getName() {
return name;
}
/**
* 设置名称
* @param name
*/

public void setName(String name) {
this.name = name;
}
/**
* 获取单价
* @return
*/

public double getPrice() {
return price;
}
/**
* 设置单价
* @param price
*/

public void setPrice(double price) {
this.price = price;
}
/**
* 获取单位
* @return
*/

public String getUom() {
return uom;
}
/**
* 设置单位
* @param uom
*/

public void setUom(String uom) {
this.uom = uom;
}
/**
* 获取库存
* @return
*/

public int getBalance() {
return balance;
}
/**
* 设置库存
* @param balance
*/

public void setBalance(int balance) {
this.balance = balance;
}
/**
* 无惨构造
*/

public Goods(){

}
/**
* 带惨构造:初始化变量
* @param id
* @param name
* @param price
* @param uom
* @param balance
*/

public Goods(int id, String name, double price, String uom, int balance) {
super();
this.id = id;
this.name = name;
this.price = price;
this.uom = uom;
this.balance = balance;
}

}

package cn.kgc.goods;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
 * 
 * @author vip宅男
 *
 */
public class TeetGoods {
@SuppressWarnings("rawtypes")
private static Map map=new HashMap<>();
private static Scanner scanner=new Scanner(System.in);


@SuppressWarnings("unchecked")
public static void get(){
Goods goods1 = new Goods(1001, "脉动水蜜桃", 7.0, "1.51", 50);
Goods goods2 = new Goods(1002, "桃李熟切片", 6.5, "400g", 10); 
Goods goods3 = new Goods(1003, "吉白芝麻油", 9.5, "125ml",20); 
Goods goods4 = new Goods(1004, "雀巢奶咖啡", 1.5, "13g", 200); 
Goods goods5 = new Goods(1005, "白玉黄豆芽", 2.4, "350g", 50); 
map.put(goods1.getId(), goods1);
map.put(goods2.getId(), goods2);
map.put(goods3.getId(), goods3);
map.put(goods4.getId(), goods4);
map.put(goods5.getId(), goods5);
}
/**
* 检测匹配id
*/

public static boolean check(int id){
if (!map.containsKey(id)) {
//没有匹配的id
return false;
}else {
//有匹配id
return true;
}
}
/**
* 新增商品
*/

@SuppressWarnings("unchecked")
public static void add(){
System.out.println("**************新增商品**************");
System.out.println("请输入商品编号:");
int id=scanner.nextInt();
if (TeetGoods.check(id)) {
//有匹配的id
System.out.println("对不起,此商品已经存在!");
}else {
System.out.println("请输入商品名称:");
String name=scanner.next();
System.out.println("请输入商品单价:");
double price=scanner.nextDouble();
System.out.println("请输入商品单位:");
String uom=scanner.next();
System.out.println("请输入商品库存数量:");
int balance=scanner.nextInt();
Goods goods6=new Goods(id, name, price, uom, balance);
map.put(goods6.getId(), goods6);
System.out.println("新增成功!");
}
}
/**
* 显示商品信息
*/

@SuppressWarnings("unchecked")
public static void show(){
System.out.println("**************商品显示**************");
System.out.println("商品编号\t商品名称\t商品单价\t商品单位\t商品库存");
Set<Map.Entry<Integer, Goods>> entrySet=map.entrySet();
Iterator<Map.Entry<Integer, Goods>> it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<Integer, Goods> entry=it.next();
System.out.println(entry.getKey()+"\t");
System.out.println(entry.getValue().getName()+"\t\t"+entry.getValue().getPrice()+"\t\t"+entry.getValue().getUom()+"\t\t"+entry.getValue().getBalance());
}
}
/**
* 入库
*/

@SuppressWarnings("unused")
public static void inStore(){
System.out.println("**************商品入库**************");
System.out.println("请输入商品编号:");
int id=scanner.nextInt();
for (int i = 0; i <map.size(); i++) {
if (TeetGoods.check(id)) {
//有匹配的id
System.out.println("请输入库存量:");
int count=scanner.nextInt();
int c=((Goods)map.get(id)).getBalance()+count;
((Goods)map.get(id)).setBalance(c);
break;
}else {
//没有匹配的id
System.out.println("对不起,此商品不存在!");
break;
}
}
}
/**
* 出库
*/

public void outStore(){
System.out.println("**************商品出库**************");
System.out.println("请输入商品编号:");
int id=scanner.nextInt();
for (int i = 0; i <map.size(); i++) {
if (TeetGoods.check(id)) {
//有匹配的id
System.out.println("请输入出库数量:");
int count=scanner.nextInt();
if (count>((Goods)map.get(id)).getBalance()) {
System.out.println("库存不足,出库失败");
}else{
int c=((Goods)map.get(id)).getBalance()-count;
((Goods)map.get(id)).setBalance(c);
break;
}
}else {
//没有匹配的id
System.out.println("对不起,此商品不存在!");
break;
}
}
}
}

package cn.kgc.goods;
/**
 * 
 * @author vip宅男
 *
 */
public class TestGoods {
@SuppressWarnings("static-access")
public static void main(String[] args) {
TeetGoods teet=new TeetGoods();
teet.get();
//teet.add();
//teet.inStore();
//teet.show();
teet.outStore();
teet.show();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值