Goods类
package com.alibaba.demo03;
/**
* 属性
* id 商品编号
* goodsName 商品名称
* price 商品价格
* desc 商品描述
*/
public class Goods {
private int id; //商品编号
private String goodsName;// 商品名称
private double price;// 商品价格
private String desc;// 商品描述
//无参和有参构造方法
public Goods() {
}
public Goods(int id, String goodsName, int price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = desc;
}
//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;
}
//toString()方法
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
}
Counter类
package com.alibaba.demo03;
import java.util.Scanner;
public class Counter {
Goods[]goodses = new Goods[10];
int num;
public Counter(){
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public void show(){
if(this.num==0){
System.out.println("柜台没有商品..");
return;
}
for (int i = 0; i < this.goodses.length; i++) {
if (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("0.退出");
System.out.println("请输入编号:");
int key = InputUtils.getNum();
switch (key){
case 1:show();break;
case 2:add();break;
case 3:delete();break;
case 4:update();break;
case 0:System.exit(0);
}
}
}
//下架
private void delete() {
System.out.println("请输入要下架的商品id:");
int id=InputUtils.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;
}
}
}
//上架
private void add() {
if(this.num==this.goodses.length){
System.out.println("柜台已满,不能再添加商品");
return;
}
System.out.println("请输入商品id:");
int goodsId = InputUtils.getGoodsId();
for (int i = 0; i < this.goodses.length; i++) {
if(this.goodses[i]!=null && this.goodses[i].getId()==goodsId){
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 newGoods = new Goods(goodsId, name, (int) price, desc);
for (int i = 0; i < this.goodses.length; i++) {
if(this.goodses[i]==null){
this.goodses[i] = newGoods;
this.num++;
System.out.println("商品上架成功,柜台商品数量"+this.num);
return;
}
}
}
//修改
private 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 = InputUtils.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;
}
}
}
}
}
InputUtils类
package com.alibaba.demo03;
import java.util.Scanner;
public class InputUtils {
public static int getNum(){
int n = 0;
try {
n = new Scanner(System.in).nextInt();
} catch (Exception e) {
//e.printStackTrace();
System.out.println("输入不正确,请重新输入");
n = getNum();
}
if(n<0||n>4){
System.out.println("命令错误,请重新输入");
n = getNum();
}
return n;
}
public static int getGoodsId(){
int n = 0;
try {
n = new Scanner(System.in).nextInt();
} catch (Exception e) {
//e.printStackTrace();
System.out.println("输入不正确,请重新输入");
n = getGoodsId();
}
return n;
}
public static double getPrice(){
double n = 0;
try {
n = new Scanner(System.in).nextDouble();
} catch (Exception e) {
System.out.println("输入不正确,请重新输入");
n = getPrice();
}
return n;
}
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;
}
}
CounterText测试类
package com.alibaba.demo03;
public class CounterText {
public static void main(String[] args) {
Counter counter = new Counter();
counter.main();
}
}
运行结果
--------------------------
1.展示商品 2.上架商品
3.下架商品 4.调整价格
0.退出
请输入编号:
1
Goods{id=1001, goodsName='巧克力', price=25.0, desc='美味可口,恋爱必备!'}
Goods{id=1002, goodsName='卫龙辣条', price=1.0, desc='隔壁小孩馋哭了!'}
--------------------------
1.展示商品 2.上架商品
3.下架商品 4.调整价格
0.退出
请输入编号:
2
请输入商品id:
1003
请输入商品的名称:
蛋糕
请输入商品的价格:
27
请输入商品的描述:
香甜软糯
商品上架成功,柜台商品数量3
--------------------------
1.展示商品 2.上架商品
3.下架商品 4.调整价格
0.退出
请输入编号:
1
Goods{id=1001, goodsName='巧克力', price=25.0, desc='美味可口,恋爱必备!'}
Goods{id=1002, goodsName='卫龙辣条', price=1.0, desc='隔壁小孩馋哭了!'}
Goods{id=1003, goodsName='蛋糕', price=27.0, desc='香甜软糯'}
--------------------------
1.展示商品 2.上架商品
3.下架商品 4.调整价格
0.退出
请输入编号:
4
请输入要修改的商品id:
请输入要修改的商品价格:
38
修改商品价格成功!
修改之后的商品信息:Goods{id=1001, goodsName='巧克力', price=38.0, desc='美味可口,恋爱必备!'}
--------------------------
1.展示商品 2.上架商品
3.下架商品 4.调整价格
0.退出
请输入编号: