数组的简单增删改

//遍历
public void show(){
    for (int i = 0; i < goodses.length; i++) {
        if (goodses[i]!=null){
            System.out.println(goodses[i]);
        }
    }
}
Scanner sc = new Scanner(System.in);
//添加
public void add(){
    /*
    System.out.println("请输出产品编号");
    int id = sc.nextInt();

     */
    System.out.println("请输出产品名字");
    String name = sc.next();
    System.out.println("请输出产品价格");
    double price = sc.nextInt();
    System.out.println("请输出产品描述");
    String desc = sc.next();

    //获得数组为空的下标
    for (int i = 0; i<num ; i++) {

        //如果正确为null 那么添加到数组中
        if (goodses[i] == null){
            this.goodses[i] = new Goods(10001+i, name, price, desc);
            System.out.println("添加成功");
            break;
        }

    }

}
Goods g = new Goods();


public void delete() {

    System.out.println("请输入要删除的商品id");
    int a = sc.nextInt();

    for (int i = 0; i < this.goodses.length; i++) {

        //循环goods[i] 如果找到goods[i]获取的id是输入的id 赋值为null
        if (this.goodses[i] != null && this.goodses[i].getId() == a) {
            this.goodses[i] = null;
            System.out.println("删除成功");
            break;
        }

    }

}
public void update(){

    System.out.println("请输入需要修改的产品id");
    int a = sc.nextInt();
    System.out.println("请输入修改后的价格");
    double price = sc.nextDouble();
    for (int i = 0; i < this.goodses.length; i++) {
        //循环goods[i] 如果找到goods[i]获取的id是输入的id 设置价格为输入的价格
        if (this.goodses[i] != null && this.goodses[i].getId() == a) {
            this.goodses[i].setPrice(price);
            System.out.println("修改成功");
            break;
        }


    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值