Goods类
package cn.hp.zuoye;
public class Goods {
private int id;
private String goodsname;
private double price;
private String desc;
//tostring
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsname='" + goodsname + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
//有参
public Goods(int id, String goodsname, double price, String desc) {
this.id = id;
this.goodsname = goodsname;
this.price = price;
this.desc = desc;
}
// 无参
public Goods() {
}
//get set 方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGoodsname() {
return goodsname;
}
public void setGoodsname(String goodsname) {
this.goodsname = goodsname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Count类
package cn.hp.zuoye;
import cn.hp.zuoye.Goods;
import java.util.Scanner;
public class Count {
Goods[] goodses=new Goods[10];
int num;//柜台商品数量
public Count() {
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public void show(){
//商品数量为0时,输出信息柜台商品为0
if (this.num==0){
System.out.println("柜台商品为0");
return;
}
//遍历商品
for (int i = 0; i < this.goodses.length; i++) {
if (this.goodses[i]!=null){
System.out.println(this.goodses[i]);
}
}
}
//柜台操作
public void main(){
while (true){
System.out.println("请根据下面操作作出相映操作:");
System.out.println("1.展示商品 2.下架商品");
System.out.println("3.上架商品 4.修改商品价格");
System.out.println("3.退出");
int key=InputUtis.getkey();
switch (key){
case 1:show();break;
case 2:delete();break;
case 3:add();break;
case 4:update();break;
case 0:System.exit(0);
}
}
}
//下架商品
public void delete(){
System.out.println("请输入要下架的商品id:");
int id=InputUtis.getid();
for (int i = 0; i < this.goodses.length; i++) {
if (this.goodses[i]!=null&&this.goodses[i].getId()==id){
this.goodses[i]=null;
System.out.println("下架商品成功!"+"当前商品数量为"+this.num);
this.num--;
return;
}
}
}
//上架商品
public void add(){
if (this.num==this.goodses.length){
System.out.println("柜台已满!!");
return;
}
System.out.println("请输入上架商品id:");
int id=InputUtis.getid();
for (int i = 0; i < this.goodses.length; i++) {
if (this.goodses[i]!=null&&this.goodses[i].getId()==id){
System.out.println("商品id已存在,请重新输入:");
add();
return;
}
}
System.out.println("请输入上架商品名称:");
String name=new Scanner(System.in).next();
System.out.println("请输入上架商品价格:");
double price=new Scanner(System.in).nextDouble();
System.out.println("请输入上架商品描述:");
String desc=new Scanner(System.in).next();
Goods goods=new Goods(id, name, price, desc);
for (int i = 0; i < this.goodses.length; i++) {
if (this.goodses[i]!=goods){
this.goodses[i]=goods;
this.num++;
System.out.println("商品信息上架成功!!"+"商品数量为"+this.num);
return;
}
}
}
//修改商品价格
public void update(){
System.out.println("请输入要修改的商品id:");
int id=InputUtis.getid();
for (int i = 0; i < this.goodses.length; i++) {
if (this.goodses[i].getId()==id){
System.out.println(this.goodses[i]);
}
System.out.println("请输入要修改的商品价格:");
double price=InputUtis.getprice();
for (int j = 0; j < this.goodses.length; j++) {
if (this.goodses[j].getPrice()!=price){
this.goodses[j].setPrice(price);
System.out.println("修改商品价格成功!"+"\n"+"修改之后的商品信息:"+this.goodses[i]);
return;
}
}
}
}
}
InputUtis类
package cn.hp.zuoye;
import java.util.Scanner;
public class InputUtis {
public static int getkey(){
int key =0;
try {
key=new Scanner(System.in).nextInt();
} catch (Exception e) {
System.out.println("警告输入非法数字!请重新输入:");
key=getkey();
}
if (key<0||key>4){
System.out.println("警告请输入合法数字命令!");
key=getkey();
}
return key;
}
public static int getid(){
int id =0;
try {
id=new Scanner(System.in).nextInt();
} catch (Exception e) {
id=getid();
}
return id;
}
public static double getprice(){
double price =0;
try {
price=new Scanner(System.in).nextDouble();
} catch (Exception e) {
price=getprice();
}
return price;
}
}
CountTest测试类
package cn.hp.zuoye;
public class CountTest {
public static void main(String[] args) {
Count c=new Count();
c.main();
}
}
1.柜台信息运行结果
2.下架商品信息
3.上架商品
4.修改商品价格