前言
需求:创建一个柜台管理系统,需要实现基本的展示柜台商品功能,以及拓展的上架商品从能,下架商品功能和调整价格功能。
大体思路:首先要有一个商品类和柜台类,然后柜台类的属性是商品类的数组及商品数量,这里的商品类的数组要定义要好长度。柜台类要有一个菜单类,这个菜单类是public的,其他的各种功能都是private的。 然后是输入工具类,商品编号以及商品价格都是数字类型的,用户使用非数字类型要提示并让其重新输入。最后是测试类,直接调用菜单类即可。
一、创建Goods类
public class Goods {
private int id; //商品编号
private String goodsName; //商品名字
private double price; //商品价格
private String desc; //商品描述
public Goods() {
}
public Goods(int id, String goodsName, double price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = desc;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
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;
}
}
二、创建柜台类
public class Counter {
private Goods goods[] = new Goods[10];
private int num;
public Counter() {
this.goods[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goods[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
}
三、创建输入工具类并定义需要用到的方法
/**
* 提供一些输入的方法
*/
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;
}
/**
* 获取商品id
*/
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) {
// e.printStackTrace();
System.out.println("-->>输入非法数字");
n = getPrice(); //递归
}
return n;
}
}
四、在柜台类中创建我们需要的功能
1.创建菜单方法
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);//退出JVM
}
}
}
2.创建展示方法
private void show(){
for (Goods good :
goods) {
if (good != null)
System.out.println(good);
}
System.out.println("-->>当前商品数量:"+ this.num);
}
3.创建上架方法
/**
* 上架商品
*/
private void add(){
if (num >= goods.length){
System.out.println("柜台已经满了,无法上架");
return;
}
System.out.println("-->>请输入商品Id");
int goodsId = InputUtils.getGoodsId();
for (int i = 0; i < goods.length; i ++){
if (this.goods[i] != null && this.goods[i].getId() == goodsId){
System.out.println("-->>当前ID已经存在,请重新输入");
add();
return; //判断生效后后边不用执行。
}
System.out.println("-->>当前Id可以使用,请输入商品信息");
System.out.println("-->>请输入商品名字");
String name = new Scanner(System.in).next();
System.out.println("-->>请输入商品价格");
double price = InputUtils.getPrice();
System.out.println("-->>请输入商品描述");
String desc = new Scanner(System.in).next();
//把信息加载到一个新的Goods对象中
Goods newGoods = new Goods(goodsId,name,price,desc);
//把对象放到柜台中去
for (int j = 0; j < this.goods.length; i++){
if(this.goods[j] != null){
this.goods[j] = newGoods;
this.num++;
System.out.println("添加成功");
System.out.println("-->>当前商品数量" + this.num);
return;
}
}
}
}
4.创建下架方法
/**
* 下架商品
*/
private void delete(){
System.out.println("-->>请输入要下架的商品编号");
int goodsId = InputUtils.getGoodsId();
for (int i = 0; i < this.goods.length; i++){
try {
if (this.goods[i].getId() == goodsId){
//找到以后删除
this.goods[i] = null;
//商品数量减一
this.num--;
//还需不需要继续循环
System.out.println("-->>下架成功");
System.out.println("-->>当前柜台剩余商品数量:" + this.num);
break;//跳出循环
}
} catch (Exception e) {
// e.printStackTrace();
System.out.println("未找到当前商品编号,请重新输入");
delete();
return;
}
}
}
5.创建修改价格方法
/**
* 调整价格
*/
private void update(){
System.out.println("-->>请输入要修改的商品编号");
int goodsId = InputUtils.getGoodsId();
for (int i = 0; i < this.goods.length; i++){
if (this.goods[i] != null && this.goods[i].getId() == goodsId){
System.out.println("请输入修改后的价格");
this.goods[i].setPrice(InputUtils.getPrice());
System.out.println("-->>修改成功");
return;
}
}
System.out.println("-->>修改失败,请检查后重新修改");
update();
return;
}
五、创建测试类并运行
public class CounterTest {
public static void main(String[] args) {
Counter counter = new Counter();
counter.main();
}
}
运行截图