题目要求:
实体类:Goods
属性
id 商品编号
goodsName 商品名称
price 商品价格
desc 商品描述
封装..提供get和set
无参构造器和有参构造器
有参构造器初始化初始化商品对象
重写toString()方法
可以直接显示数据
柜台类:Counter
属性:
柜台商品列表,固定10个商品位置
Goods[]goodses = new Goods[10];
num 柜台商品数量
构造器:
无参构造器初始化2个商品
public Counter(){
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
}
业务方法:
展示柜台所有的商品(不能输出null)
public void show(){
}
测试类:CounterTest
程序入口main方法中,创建柜台对象,调用show()方法展示柜台商品
package cn.hp.demo01;
import java.util.Scanner;
/**
* 柜台类
* 展示商品对象
*/
public class Counter {
private Goods[] goods = new Goods[10]; //商品柜台,固定10个
private int num = 10; //柜台商品数量
private int idNum; //作为商品自增id
//无参构造器初始化3个商品
public Counter() {
this.goods[0] = new Goods(1001, "巧克力", 25, "美味可口,恋爱必备!");
this.goods[1] = new Goods(1002, "卫龙辣条", 3.5, "隔壁小孩馋哭了!");
this.goods[2] = new Goods(1003, "冰淇淋", 10, "甜腻腻~");
num = 3; //商品数量为3
idNum = 1004; //准备下一次id
}
//柜台的功能菜单
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("请输入0-4的数字选择功能执行:");
int key = InputUtils.getNum();
switch (key) {
case 1:
this.show();
break;
case 2:
this.add();
break;
case 3:
this.delete();
break;
case 4:
this.update();
break;
case 0:
System.exit(0); //退出Java虚拟机(JVM)
}
}
}
//展示柜台所有的商品 (不能输出null)
//判断是否为空字段,不为空则输出
private void show() {
System.out.println("-------- 柜台商品信息 ----------");
if (this.num == 0) {
System.out.println("警告:目前柜台暂无商品!");
return;
}
for (int i = 0; i < this.goods.length; i++) {
if (this.goods[i] != null)
System.out.println(this.goods[i]);
}
System.out.println("--------------------");
}
/**
* 调整价格:
* 输入要调整的商品id
* 然后从柜台查找
* 输入新的价格进行修改
*/
private void update() {
//当前柜台是否为空
if (this.num == 0) {
System.out.println("警告:柜台已空!!没得修改!!");
return;
}
System.out.println("请输入商品id:");
int goodsId = InputUtils.getGoodsId();
//查找
for (int i = 0; i < this.goods.length; i++) {
//找为null的位置
if (this.goods[i] != null && this.goods[i].getId() == goodsId) {
System.out.println(this.goods[i]);//重现商品信息
System.out.println("请输入商品价格:");
double price = new Scanner(System.in).nextDouble();
this.goods[i].setPrice(price);//底层实际改价格的代码
System.out.println("-->价格调整成功!!!");
return;//方法结束
}
}
System.out.println("-->警告:商品找不到!!");
}
/**
* 删除商品
*/
private void delete() {
//当前柜台是否为空
if (this.num == 0) {
System.out.println("警告:柜台已空!!无法删除!!");
return;
}
System.out.println("请输入要删除商品的id");
int goodsId = InputUtils.getGoodsId();
//从柜台列表(属性:数组)
for (int i = 0; i < this.goods.length; i++) {
//查看是否有匹配的id
if (this.goods[i] != null && this.goods[i].getId() == goodsId) {
System.out.println("提示:商品已找到,正在删除...");
this.goods[i] = null; //实际删除代码
this.num--; //柜台商品的数量
System.out.println("删除成功!");
return;//方法结束
}
}
//如果程序执行到这里,说明什么?说明没有找到对应id的商品
System.out.println("警告:商品不存在,删除失败!!");
}
/**
* 上架商品:
* 首先输入要上架的商品信息,存到实体类
* 再把实体类保存到柜台(属性数组)
*/
private void add() {
//当前柜台是否已满
if (this.num == 10) {
System.out.println("警告:柜台已满!不能再上架了!");
return;
}
//System.out.println("-->请输入商品id:");
//int goodsId = new Scanner(System.in).nextInt();
System.out.println("请输入商品名称:");
String name = InputUtils.getGoodsName(this.goods);
System.out.println("请输入商品价格:");
double price = new Scanner(System.in).nextDouble();
System.out.println("请输入商品描述:");
String desc = new Scanner(System.in).next();
//创建商品实体类对象,作为数据的载体
Goods goods = new Goods(this.idNum, name, price, desc);
//添加到柜台没有放商品的位置,元素为null的位置
for (int i = 0; i < this.goods.length; i++) {
//找为null的位置
if (this.goods[i] == null) {
this.goods[i] = goods;
this.num++; //柜台商品的数量
this.idNum++; //准备下一次的自增id
System.out.println("添加成功!");
return; //方法结束
}
}
}
}
package cn.hp.demo01;
/**
* 测试使用柜台展示商品的模拟
*/
public class CounterTest {
public static void main(String[] args) {
//创建柜台对象
Counter c= new Counter();
//调用main()方法展示柜台
c.main();
/*
String s = ""; //长度为0的字符串对象
s = null; //String类型的变量s没有保存字符串对象
System.out.println("字符串["+s+"]的字符个数:"+s.length());
*/
}
}
package cn.hp.demo01;
/**
* 商品信息实体类
* 一个商品对象,表示一个商品信息
*/
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;
}
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 + '\'' +
'}';
}
}
package cn.hp.demo01;
import java.util.Scanner;
/**
* 用作从控制台输入的工具类
*/
public class InputUtils {
/**
* 获取柜台菜单功能号码
*/
public static int getNum(){
int n= 0;
try {//try语句块设置的是可能会发生异常的代码
n = new Scanner(System.in).nextInt();
} catch (Exception e) { //一旦发生异常,catch语句块会捕捉异常,并处理
//e.printStackTrace();
System.out.println("警告:输入非法数字!请重新输入...");
n = getNum(); //递归,方法自己调用自己
}
//检验是否是0-4的功能号
if (n<0 || n>4){
System.out.println("警告:非法命令!请重新输入");
n = getNum(); //递归
}
return n;
}
/**
* 获取商品id
*/
public static int getGoodsId(){
int n= 0;
try {//try语句块放置的是可能会发生异常的代码
n = new Scanner(System.in).nextInt();
} catch (Exception e) { //一旦发生异常,catch语句块会捕捉异常,并处理
//e.printStackTrace();
System.out.println("警告:输入非法数字!请重新输入");
n = getNum(); //递归,方法自己调用自己
}
return n;
}
/**
* 获取商品id
*/
public static String getGoodsName(Goods[]goods){
String name = new Scanner(System.in).next();
//增加:商品名称不能重复
for (int i = 0; i < goods.length; i++) {
//查找柜台中是否已经存在同名的商品,如果存在就不能添加
if(goods[i]!=null && goods[i].getGoodsName().equals(name)) {
System.out.println("警告:商品名称已存在!请重新输入...");
name = getGoodsName(goods);
break;
}
}
return name;
}
}