深圳网站关键词排名优化,做乒乓球网站的图片,浙江省建设厅官网证件查询,进入百度app查看Arraylist是使用最频繁的一个集合#xff0c;它与数组类似#xff0c;不同之处在于它可以动态改变长度#xff0c;不够了可以扩容。
案例#xff1a; 我的思考#xff1a;
首先多个菜品信息可以用Arraylist 来存储#xff0c;那我们需要再创建一个菜品类Food#xff0…Arraylist是使用最频繁的一个集合它与数组类似不同之处在于它可以动态改变长度不够了可以扩容。
案例 我的思考
首先多个菜品信息可以用Arraylist 来存储那我们需要再创建一个菜品类Food为了实现菜品上架等等功能再创建一个FoodOperator类进行操作。
我的代码
//Food.javapublic class Food {private String name;private double price;private int score;public Food() {}public Food(String name, double price, int score) {this.name name;this.price price;this.score score;}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;}public double getScore() {return score;}public void setScore(int score) {this.score score;}
}// FoodOperator.javapublic class FoodOperator {ArrayListFood list new ArrayList();public void addFood(){Food food new Food();Scanner sc new Scanner(System.in);System.out.println(请你输入菜品名字);String name sc.next();food.setName(name);System.out.println(请你输入菜品价格);double price sc.nextDouble();food.setPrice(price);System.out.println(请你输入菜品评级);int score sc.nextInt();food.setScore(score);list.add(food);}public void print(){System.out.println(-----------------);System.out.println(菜品信息);for (int i 0; i list.size(); i) {Food f list.get(i);System.out.println(f.getName());System.out.println(f.getPrice());System.out.println(f.getScore());System.out.println(-----------------);}}public void start(){Scanner sc new Scanner(System.in);System.out.println(操作1上架菜品);System.out.println(操作2打印菜品);System.out.println(操作0退出);while (true){System.out.println(请输入你要选择的操作);String commend sc.next();switch(commend){case 0:return;case 1:addFood();break;case 2:print();break;default:System.out.println(输入无效指令请重试);break;}}}
}
// Test.javapublic class Test {public static void main(String[] args) {FoodOperator foodOperator new FoodOperator();foodOperator.start();}
}