商品

package com.oracle.demo01;

import java.util.ArrayList;
import java.util.Scanner;

public class Demo02 {
 public static void main(String[] args){
     Scanner sc=new Scanner(System.in);
     Good g1=new Good();
     g1.name="少林寺酥饼核桃";
     g1.price=120;
     g1.gid=9001;
     Good g2=new Good();
     g2.gid=9002;
     g2.name="尚康杂粮牡丹饼";
     g2.price=20;
     Good g3=new Good();
     g3.gid=9003;
     g3.name="新疆原产哈密瓜";
     g3.price=7;
     ArrayList<Good> arr=new ArrayList<Good>();
     arr.add(g1);
     arr.add(g2);
     arr.add(g3);
    while(true){
        //展示菜单
        show();
        //让用户输入
        int choose=sc.nextInt();
        switch(choose){
        case 1:getGood(arr);
            break;
        case 2:addGood(arr);
            break;
        case 3:updateGood(arr);
            break;
        case 4:
            break;
        case 5://退出
            return;
        default:
            System.out.println("您的输入有误,请重新输入!");
            break;
        }
    }
}
  //商品展示页面
 public static void show(){
     System.out.println("----欢迎光临oracle超市------");
     System.out.println("1.货物清单");
     System.out.println("2.添加货物");
     System.out.println("3.修改货物");
     System.out.println("4.删除货物");
     System.out.println("5.退出");
     System.out.println("请输入您的选择");
 }

//查询所有商品
   public static void getGood(ArrayList<Good> arr){
       System.out.println("----商品清单----");
       System.out.println("商品编号\t商品名称\t\t商品价格");
       for(int i=0;i<arr.size();i++){
           System.out.println(arr.get(i).gid+"\t"+arr.get(i).name+"\t"+arr.get(i).price);
       }
   }
   //添加商品
   public static void addGood(ArrayList<Good> arr){
       System.out.println("请输入您要新增的商品编号:");
       Scanner sc=new Scanner(System.in);
       int id=sc.nextInt();
       System.out.println("请输入您要新增的商品名称");
       String name=sc.next();
       System.out.println("请输入您要新增的商品价格");
       double price=sc.nextDouble();
       //创建Good对象,先将数据存入对象
       Good good=new Good();
       good.gid=id;
       good.name=name;
       good.price=price;
       arr.add(good);                                  
   }
   //修改商品信息
   public static void updateGood(ArrayList<Good> arr){
       Scanner sc=new Scanner(System.in);
       System.out.println("请输入您要修改的商品编号");
       int id=sc.nextInt();
       System.out.println("请输入您要修改后的商品名称");
       String name=sc.next();
       System.out.println("请输入您要修改后的商品价格");
       double price=sc.nextDouble();
       //遍历集合id所对应的Good对象
       for(int i=0;i<arr.size();i++){
           if(arr.get(i).gid==id){
               arr.get(i).name=name;
               arr.get(i).price=price;               
           }
       }
   }
}

posted @ 2019-12-14 15:55  坤坤坤  阅读(164)  评论(0)    收藏  举报