标签:修改 void stat 编号 输入 lse 方法 add while
package com.oracle.demo01; import java.util.ArrayList; import java.util.Scanner; public class Demo03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Goods g1 = new Goods(); Goods g2 = new Goods(); Goods g3 = new Goods(); g1.gid = 9001; g1.name = "少林寺酥饼核桃"; g1.price = 120; g2.gid = 9002; g2.name = "尚康杂粮牡丹饼"; g2.price = 20; g3.gid = 9003; g3.name = "新疆原产哈密瓜"; g3.price = 7; ArrayList<Goods> arr = new ArrayList<Goods>(); arr.add(g1); arr.add(g2); arr.add(g3); while (true) { show(); int choose = sc.nextInt(); switch (choose) { case 1: getGoods(arr); break; case 2: addGoods(arr); break; case 3: updateGoods(arr); break; case 4: dropGoods(arr); 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 getGoods(ArrayList<Goods> arr) { System.out.println("=======================商品清单======================"); System.out.println("商品编号\t\t商品名称\t\t商品价格"); for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i).gid + "\t\t" + arr.get(i).name + "\t" + arr.get(i).price); } } // 添加商品 public static void addGoods(ArrayList<Goods> 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(); Goods goods = new Goods(); goods.gid = id; goods.name = name; goods.price = price; arr.add(goods); } // 修改商品 public static void updateGoods(ArrayList<Goods> arr){ System.out.println("请输入您要修改的商品编号(输入0返回主菜单):"); for(int i=0;i<arr.size();i++){ System.out.println(arr.get(i).gid+" "+arr.get(i).name); } Scanner sc=new Scanner(System.in); int uid=0; aa:while (true) { int id=sc.nextInt(); for(int i=0;i<arr.size();i++){ if(arr.get(i).gid==id){ uid=id; break aa; }else if(id==0){ return; } } System.out.println("请输入正确的商品编号(输入0返回主菜单):"); } System.out.println("请修改商品名称:"); String name=sc.next(); System.out.println("请修改商品价格:"); double price=sc.nextDouble(); //遍历,按id找gid for(int i=0;i<arr.size();i++){ if(arr.get(i).gid==uid){ arr.get(i).name=name; arr.get(i).price=price; } }
//第二种修改的方法 for (int i = 0; i < arr.size(); i++) { if (arr.get(i).gid == uid) { Goods goods=new Goods(); goods.gid=uid; goods.name=name; goods.price=price; arr.set(i, goods); } }
} //删除商品 private static void dropGoods(ArrayList<Goods> arr) { System.out.println("请输入您要删除的商品编号(输入0返回主菜单):"); for(int i=0;i<arr.size();i++){ System.out.println(arr.get(i).gid+" "+arr.get(i).name); } Scanner sc=new Scanner(System.in); int uid=0; aa:while (true) { int id=sc.nextInt(); for(int i=0;i<arr.size();i++){ if(arr.get(i).gid==id){ uid=id; break aa; }else if(id==0){ return; } } System.out.println("请输入正确的商品编号(输入0返回主菜单):"); } for(int i=0;i<arr.size();i++){ if(arr.get(i).gid==uid){ arr.remove(i); } } } }
标签:修改 void stat 编号 输入 lse 方法 add while
原文地址:https://www.cnblogs.com/l1314/p/12040139.html