package com.hp.Demo3;
public class Goods {
private int id;
private String goodName;
private int price;
private String desc;
public Goods(int goodsId, String name, String price, String desc) {
}
public Goods(int id, String goodName) {
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodName='" + goodName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
public void setId(int id) {
this.id = id;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public void setPrice(int price) {
this.price = price;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public String getGoodName() {
return goodName;
}
public int getPrice() {
return price;
}
public String getDesc() {
return desc;
}
public Goods(int id, String goodName, int price, String desc) {
this.id = id;
this.goodName = goodName;
this.price = price;
this.desc = desc;
}
}
package com.hp.Demo3;
import com.sun.javafx.scene.input.InputEventUtils;
import java.util.Scanner;
public class Counter {
private static Goods[]goodses = new Goods[10];
int num=0;
public Counter(){
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public void mian() {
while (true) {
System.out.println("------------------------------");
System.out.println("-----1.展示商品 2.上架商品-----");
System.out.println("-----3.下架商品 4.调整价格-----");
System.out.println("-----0.退出 ----------------");
System.out.println("输入功能编号");
//int key = new Scanner(System.in).nextInt();
int key = inputUtils.getNUm();
switch (key) {
case 1:
show();
break;
case 2:
add();
break;
case 3:
delect();
break;
case 4:
update();
break;
case 0:
System.exit(0);//退出JVM
}
}
}
public void show(){
if (num == 00) {
System.out.println("不输出null商品");
return;
}
for (int i = 0; i < goodses.length; i++) {
if (goodses[i] != null) {
System.out.println(goodses[i]);
}
}
}
private void delect() {
System.out.println("-->请输入要下架的商品编号");
int goodsId = inputUtils.getGoodsId();
for (int i = 0; i <this.goodses.length ; i++) {
if (this.goodses !=null && this.goodses[i].getId()==goodsId) {
//找到以后,删除
this.goodses[i] = null;
//商品数量减一
this.num--;
System.out.println("-->下架成功!!当前商品数量"+this.num);
return;//跳出方法(只有找到商品才会执行)
}
}
//如果没有找到商品,就会执行到这里
System.out.println("-->警告,未找到商品");
}
private void update() {
System.out.println("请输入要修改商品的id");
int goodsId = inputUtils.getGoodsId();
for (int i = 0; i <this.goodses.length ; i++) {
if (this.goodses != null && this.goodses[i].getId() == goodsId) {
System.out.println("商品的初始价格为"+this.goodses[i].getPrice());
System.out.println("请输入价格");
int g = inputUtils.getPrice();
this.goodses[i].setPrice(g);
Goods goods=new Goods(this.goodses[i].getId(),this.goodses[i].getGoodName());
show();
return;
}
}
}
private void add() {
System.out.println("-->请输入商品id");
int goodsId = inputUtils.getGoodsId();
//丰富: id不能重复
for (int i = 0; i < this.goodses.length; i++) {
if (this.goodses != null && this.goodses[i].getId() == goodsId) {
System.out.println("-->警告,当前编号已存在:请重新输入");
add();
return;//由于重复了,后续不执行
}
}
//程序能走到这里,说明Id没有重复
System.out.println("-->提示:商品id可以使用");
System.out.println("-->请输入商品名称:");
String name = new Scanner(System.in).next();
System.out.println("-->请输入商品价格:");
String price = new Scanner(System.in).next();
System.out.println("-->请输入商品描述:");
String desc = new Scanner(System.in).next();
//把输入的信息装到Goods对像
Goods newGoods=new Goods(goodsId,name,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("");
return;
}
}
}
}
package com.hp.Demo3;
import java.util.Scanner;
public class CounterTest {
public static void main(String[] args) {
Counter counter = new Counter();
counter.mian();
}
}
package com.hp.Demo3;
import java.util.Scanner;
/**
* 提供一些输入方法
*/
public class inputUtils {
/**
* 获取我们的功能序号
*
* @return
*/
public static int getNUm() {
int n = 0;
try {
n = new Scanner(System.in).nextInt();
} catch (Exception e) {
//e.printStackTrace();
//如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
System.out.println("-->警告:输入非法数字!!,请重新输入");
n = getNUm();
}
//必须是0-4
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();
//如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
System.out.println("-->警告:输入非法数字!!,请重新输入");
n = getGoodsId();
}
if (n<0||n>100000) {
System.out.println("请重新输入");
}
return n;//返回输入的数字
}
public static int getadd() {
int n = 0;
try {
n = new Scanner(System.in).nextInt();
} catch (Exception e) {
//e.printStackTrace();
//如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
System.out.println("-->警告:输入非法数字!!,请重新输入");
n = getadd();
}
return n;//返回输入的数字
}
public static int getPrice() {
int n = 0;
try {
n = new Scanner(System.in).nextInt();
} catch (Exception e) {
//e.printStackTrace();
//如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
System.out.println("-->警告:输入非法数字!!,请重新输入");
n = getPrice();
}
return n;//返回输入的数字
}
public static int getupdate() {
int i = 0;
try {
i = new Scanner(System.in).nextInt();
} catch (Exception e) {
//e.printStackTrace();
//如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
System.out.println("-->警告:输入正确的id!!,请重新输入");
i = getupdate();
}
return i;
}
}