实体类
package com.hp.goods;
public class GoodsEntity {
private Integer id;//商品id
private String name;//商品名称
private Double price;//商品价格
//无参
public GoodsEntity() {
}
//有参
public GoodsEntity(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
//gat and set
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "GoodsEntity{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
管理层
package com.hp.goods;
import java.util.Scanner;
public class GoodsManager {
private static GoodsEntity[] goodsArrays = new GoodsEntity[5];
private static int num = 0;
/**
* 定义商品数组;商品柜台
*/
static {
goodsArrays[0] = new GoodsEntity(1,"鞋",1000.00);
goodsArrays[1] = new GoodsEntity(2,"上衣",100.00);
}
/**
* 展示全部商品
*/
public static void show(){
for (int i = 0; i < goodsArrays.length; i++) {
if (goodsArrays[i]!=null){
System.out.println(goodsArrays[i]);
}
}
}
/**
* 添加商品
*/
public static void add() {
if (num == 5) {
System.out.println("--商品满了--");
return;
}
GoodsEntity goods = new GoodsEntity(3,"手机",1600.00);
System.out.println("--请输入商品id--");
// Scanner sc = new Scanner(System.in);
goods.setId(new Scanner(System.in).nextInt());
System.out.println("--请输入商品名字--");
goods.setName(new Scanner(System.in).nextLine());
System.out.println("--请输入商品价格--");
goods.setPrice(new Scanner(System.in).nextDouble());
for (int i = 0; i < goodsArrays.length; i++) {
if (goodsArrays[i] == null) {
goodsArrays[i] = goods;
System.out.println("--添加成功--");
num++;
return;
}
}
}
/**
* 删除商品
*/
public static void del(){
System.out.println("--请输入要删除的商品id--");
int id = new Scanner(System.in).nextInt();
if (id>5 && id<1){
System.out.println("--商品不存在--");
return;
}else {
for (int i = 0; i < goodsArrays.length; i++) {
if (i == id-1){
goodsArrays[i]=null;
System.out.println("删除成功");
return;
}
}
}
}
//修改商品
public static void update(){
System.out.println("--请输入要修改商品的id--");
int id = new Scanner(System.in).nextInt();
if(id>5 && id<1){
System.out.println("--商品不存在--");
return;
}else {
System.out.println("修改名字");
String name = new Scanner(System.in).nextLine();
System.out.println("修改价格");
Double price = new Scanner(System.in).nextDouble();
GoodsEntity goodsEntity = new GoodsEntity(id,name,price);
for (int i = 0; i < goodsArrays.length; i++) {
if (i == id-1){
goodsArrays[i]=goodsEntity;
System.out.println("修改成功");
return;
}
}
}
}
public static void main(String[] args) {
add();
}
}
测试类
package com.hp.goods;
import java.util.Scanner;
public class GoodsTest {
public static void main(String[] args) {
GoodsManager gm = new GoodsManager();
System.out.println("===商品柜台菜单===");
System.out.println("1.展示商品 2.添加商品");
System.out.println("3.删除商品 4.修改商品");
System.out.println("0.退出");
while (true){
System.out.println("--请输入操作指令--");
int key = new Scanner(System.in).nextInt();
switch (key){
case 1:GoodsManager.show();break;
case 2:GoodsManager.add();break;
case 3:GoodsManager.del();break;
case 4:GoodsManager.update();break;
case 5:System.exit(0);
}
}
}
}