package com.tjetc.domain;
public class Product {
public static int index;
private int pid;
private String pname;
private int price;
private int count;
public Product(int pid, String pname, int price, int count) {
super();
this.pid = pid;
this.pname = pname;
this.price = price;
this.count = count;
}
//用来显示商品信息
public void show(){
System.out.println("编号:" +"\t名称:" +"\t价格:" + "\t数量:" );
System.out.println( pid +"\t" +pname +"\t" + price +"\t"+ count);
}
public int getPid() {
return pid;
}
public int getCount() {
return count;
}
//入库时修改库存数量的set方法
public void setCount(int count) {
this.count = this.count+count;
}
//出库时修改库存数量的set方法
public void setCount2(int count) {
this.count = this.count-count;
}
public void setPid(int pid) {
this.pid = pid;
}
}
//main
package com.tjetc.test;
import java.util.Scanner;
import com.tjetc.domain.Product;
public class ProductTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 定义一个数组
Product[] product = new Product[1];
Product pro = new Product(0, null, 0, 0);
Scanner input = new Scanner(System.in); // 用来接收数
Scanner input1 = new Scanner(System.in);// 用来接收字符串
int pid;
String pname;
int price;
int count;
System.out.println("---小商品管理系统------");
while (true) {
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("请输入你的选择:");
int num = input.nextInt();
switch (num) {
case 1: {
int i;
for (i = 0; i < product.length; i++) {
System.out.print("请输入商品编号:");
pid = input.nextInt();
System.out.print("请输入商品名称:");
pname = input1.nextLine();
System.out.print("请输入商品价格:");
price = input.nextInt();
System.out.print("请输入商品数量:");
count = input.nextInt();
product[i] = new Product(pid, pname, price, count);
}
}
break;
case 2: {
for (Product pro1 : product) {
pro1.show();
}
}
break;
case 3: {
System.out.println("请输入需要入库的商品编号:");
pid = input.nextInt();
for (Product pro1 : product) {
if (pid == pro1.getPid()) {
pro1.show();
System.out.print("请输入要入库的数量:");
count = input.nextInt();
pro1.setCount(count);
pro1.show();
}
}
}
break;
case 4: {
System.out.println("请输入需要出库的商品的编号:");
pid = input.nextInt();
for (Product pro2 : product) {
if (pid == pro2.getPid()) {
while (true) {
System.out.print("请输入需要出库的数量:");
count = input.nextInt();
if (count > pro2.getCount()) {
System.out.print("出库失败库存小于您的选择");
} else {
pro2.setCount2(count);
pro2.show();
break;
}
}
}
}
}
break;
case 5: {
System.out.println("欢迎下次使用");
}
}
if (num == 5) {
break;
}
}
}
}