/**
*
* @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();
}
}