package 水果管理系统; import java. util. ArrayList ; import java. util. List ;
import java. util. Scanner ;
class Fruit { private String name; private double price; public Fruit ( String name, double price) { this . name = name; this . price = price; } public String getName ( ) { return name; } public double getPrice ( ) { return price; } public void setPrice ( double price) { this . price = price; } @Override public String toString ( ) { return "ccc.水果管理系统.Fruit [name=" + name + ", price=" + price + "]" ; }
} public class AYL { private List < Fruit > fruits; public void AYL ( ) { fruits = new ArrayList < > ( ) ; } public void addFruit ( Fruit fruit) { fruits. add ( fruit) ; System . out. println ( "水果已成功添加!" ) ; } public void removeFruit ( int index) { if ( index >= 0 && index < fruits. size ( ) ) { fruits. remove ( index) ; System . out. println ( "水果已成功删除!" ) ; } else { System . out. println ( "无效的索引!" ) ; } } public void modifyFruitPrice ( int index, double newPrice) { if ( index >= 0 && index < fruits. size ( ) ) { Fruit fruit = fruits. get ( index) ; fruit. setPrice ( newPrice) ; System . out. println ( "水果价格已成功修改!" ) ; } else { System . out. println ( "无效的索引!" ) ; } } public void displayAllFruits ( ) { if ( fruits. isEmpty ( ) ) { System . out. println ( "水果列表为空!" ) ; } else { for ( Fruit fruit : fruits) { System . out. println ( fruit) ; } } } public static void main ( String [ ] args) { AYL system = new AYL ( ) ; Scanner scanner = new Scanner ( System . in) ; int choice = 0 ; while ( true ) { System . out. println ( "=====================" ) ; System . out. println ( "1. 添加水果" ) ; System . out. println ( "2. 删除水果" ) ; System . out. println ( "3. 修改水果价格" ) ; System . out. println ( "4. 查询所有水果" ) ; System . out. println ( "5. 退出" ) ; System . out. println ( "=====================" ) ; System . out. print ( "请输入数字操作选项:" ) ; choice = scanner. nextInt ( ) ; switch ( choice) { case 1 : System . out. print ( "请输入水果名称:" ) ; String name = scanner. next ( ) ; System . out. print ( "请输入水果价格:" ) ; double price = scanner. nextDouble ( ) ; system. addFruit ( new Fruit ( name, price) ) ; break ; case 2 : System . out. print ( "请输入要删除的水果索引:" ) ; int removeIndex = scanner. nextInt ( ) ; system. removeFruit ( removeIndex) ; break ; case 3 : System . out. print ( "请输入要修改价格的水果索引:" ) ; int modifyIndex = scanner. nextInt ( ) ; System . out. print ( "请输入新的价格:" ) ; double newPrice = scanner. nextDouble ( ) ; system. modifyFruitPrice ( modifyIndex, newPrice) ; break ; case 4 : system. displayAllFruits ( ) ; break ; case 5 : System . out. println ( "退出程序!" ) ; scanner. close ( ) ; System . exit ( 0 ) ; default : System . out. println ( "无效的选项!" ) ; break ; } } }
}