创建实体类
package cn.hp;
public class Goods {
private int id;
private String goodsName;
private double price;
private String desc;
public Goods() {
}
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=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
public Goods(int id, String goodsName, double price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = desc;
}
}
添加功能
package cn.hp;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.SortedMap;
public class Counter {
Goods goodses[] = new Goods[10];
//定义柜台商品数量
int num = 3;
public Counter() {
this.goodses[0] = new Goods(10001,"乐事薯片",6.5,"最喜欢青柠味的");
this.goodses[1] = new Goods(10002, "可口可乐", 3, "好喝。。嗝..");
this.goodses[2] = new Goods(10003, "巧乐兹", 5.5, "雪糕刺客");
}
//判断是否为空
public void main(){
//菜单管理
while(true){
System.out.println("--------------------------------");
System.out.println("-- 1.商品列表 2.上架商品---");
System.out.println("-- 3.下架商品 4.调整价格---");
System.out.println("-- 0.退出 ---");
//选择功能
int key = new Scanner(System.in).nextInt();
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 show(){
for (int i = 0; i <10; i++) {
if (goodses[i]!=null) {
System.out.println(goodses[i]);
}
}
}
//添加商品信息
Scanner sc=new Scanner(System.in);
public void add(){
Goods good =new Goods();
System.out.println("输入商品编号");
int a=sc.nextInt();
good.setId(a);
System.out.println("输入商品名称");
String b=sc.next();
good.setGoodsName(b);
System.out.println("输入商品价格");
double c=sc.nextDouble();
good.setPrice(c);
System.out.println("输入商品介绍");
String d=sc.next();
good.setDesc(d);
for (int i = 0; i < goodses.length; i++) {
if(goodses[i]==null){
goodses[i]=good;
num++;
System.out.println("--添加商品结束---");
return;
}
}
}
public void delete(){
System.out.println("输入要删除商品的序号");
int a=sc.nextInt();
for (int i = 0; i < goodses.length; i++) {
if(goodses[i]!=null){
goodses[a-1]=null;
System.out.println("删除成功!!");
break;
}
}
}
public void update(){
System.out.println("输入要修改商品价格的序号");
int a=sc.nextInt();
System.out.println("输入要修改de价格");
double b=sc.nextDouble();
for (int i = 0; i < goodses.length ; i++) {
if(goodses[i]!=null){
goodses[a-1].setPrice(b);
System.out.println("修改成功!!");
break;
}
}
}
}
测试输出方法
package cn.hp;
public class GoodsTest {
public static void main(String[] args) {
Counter counter = new Counter();
counter.main();
}
}