柜台管理系统
1.初步设计
最基本的代码
不包含任何的健壮性和重复性判断
能实现需求即可
不写工具类和异常处理等等
最终能实现基本的运行和操作
2.健壮性调整
增加工具类InputUtils来解决输入异常的问题
show();
增加商品数量为0时的提示
不输出null商品
delete();
无(根据自己的需求)
add();
增加商品数量已满的提示
增加商品编号不能重复的判断
update();
根据商品编号(输入要修改的商品编号)
修改商品的价格要求:从设计之初,把柜台商品管理的设计概要和需求,以及后续
一步一步如何设计的,文字+代码
1.初步设计的过程和代码,以及测试结果(可以复制控制台的文字沾到博客)
初步设计
package com.hp.good;
public class Goods {
private int id;//商品编号
private String goodsName;//商品名称
private double price;//商品价格
private String 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;
}
@Override
public String toString() {
return "Goods{" +
"编号=" + id +
", 商品名称='" + goodsName + '\'' +
", 价格=" + price +
", 描述='" + desc + '\'' +
'}';
}
}
package com.hp.xin;
public class GoodD {
private int num;
private Good[] g = new Good[10];
//无参构造器初始化2个商品
public GoodD(){
this.g[0]=new Good(1001,“巧克力”,25,“美味可口,恋爱必备!”);
this.g[1]=new Good(1002,“卫龙辣条”,1,“隔壁小孩馋哭了!”);
num=2;//相当于两个商品
}
//展示柜台所有的商品(不能输出null)
public void show(){
for (int i = 0; i < g.length; i++) {
if (g[i]!=null){
System.out.println(g[i]);
}
}
}
}
测试类
package com.hp.xin;
public class GoodTest {
public static void main(String[] args) {
GoodD g = new GoodD();
g.show();
}
}
健壮性调整
package com.hp.good;
import javax.naming.Name;
import java.util.Scanner;
public class Counter {
Goods[] g = new Goods[10];
int num =10;
public Counter(){
this.g[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.g[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num = 2;
}
public void show(){
for (int i = 0; i < g.length; i++) {
if(this.g[i] != null) {
System.out.println(g[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);
}
}
}
//删除商品
public void delete(){
System.out.println("-->请输入要下架的商品编号:");
int goodsId = InputUtils.getFGoodsId();
for (int i = 0; i <this.g.length ; i++) {
//查找对应id的商品对象
if(this.g[i]!= null & this.g[i].getId()==goodsId){
//删除
this.g[i] = null;
//商品数量减一
this.num--;
System.out.println("--》下架成功;当前商品数量"+this.num);
//跳出方法(只有找到方法才执行)
return;
}
}
System.out.println("警告!!未找到商品");
}
//添加商品
public void add(){
if(this.num==this.g.length){
System.out.println("-->警告:柜台已满,不能再添加商品了");
return;
}
System.out.println("请输入商品ID");
int goodsId = InputUtils.getFGoodsId();
//判断id不能重复
for (int i = 0; i <this.g.length ; i++) {
if(this.g[i]!=null && this.g[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("--》请输入商品价格");
double price = InputUtils.getPrice();
System.out.println("--》请输入商品描述");
String desc = new Scanner(System.in).next();
//存到商品对象
Goods newGood = new Goods(goodsId,name,price,desc);
for (int i = 0; i < this.g.length; i++) {
if(this.g[i]==null){
this.g[i] = newGood;
this.num++;
System.out.println("商品上架成功!!柜台数量为"+this.num);
return;
}
}
}
//修改商品
public void update(){
System.out.println("请输入要修改的商品id:");
int id=InputUtils.getFGoodsId();
for (int i = 0; i < this.g.length; i++) {
if (this.g[i].getId()!=id){
System.out.println("查无此编号!请重新输入");
update();
return;//发生错误,后续代码不执行
}
System.out.println("--》请输入要修改商品价格");
double price=InputUtils.getPrice();
for (int j = 0; j < this.g.length; j++) {
if (this.g[j].getPrice()!=price){
this.g[j].setPrice(price);
System.out.println("修改成功!"+"\n"+"修改之后为:"+this.g[i]);
return;
}
}
}
}
}
package com.hp.good;
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 getFGoodsId(){
int n=0;
try {
n = new Scanner(System.in).nextInt();
} catch (Exception e) {
e.printStackTrace();
System.out.println("-->警告:输入异常,请重新输入");
n = getFGoodsId();
}
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;
}
}
package com.hp.good;
public class CounterTest {
public static void main(String[] args) {
Counter c = new Counter();
c.show();
c.main();
}
}
添加:
显示
删除
修改
初步设计
首先写实体类,每个属性定义,以及实现get和set法方法,添加有参无参构造器,以及tostring方法。然后写柜台类,定义柜台商品数量,无参构造器初始化2个商品。利用if判断和for循环实现展示柜台所有的商品(不能输出null),然后写测试类测试
健壮性调整
增加了,添加,修改,删除方法,以及工具类等。
编写工具类方便柜台类直接调用。在柜台类写菜单方法实现选择,然后实现添加,修改,删除和需求。