酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

一、 考试题目:java高级综合应用-水果超市 (一)、语言和环境 1、实现语言:java 2、环境要求:java1.8版本或以上,idea2018.1版本或以上 (二)、功能需求 为了方便维护超市水果信息,统一进行管理,可以把超市里所有水果信息录入系统,及时进行更新和统计功能。使用java高级应用到实际生活当中,并掌握代码的规范性和可读性。使之能更好地学好java各种应用。 二、 考试时间:120分钟 三、 考试要求: 通过面向对象保存到集合中相关录入数据,同时也对集合中的数据进行磁盘文件保存fruit.bin。主要功能包括:(1)展示所有水果信息模块;(2)添加水果信息模块;(3)查看特定水果信息模块;(4)水果下架模块;(5)按照价格降序展示模块。(6)退出; 需要完成的功能模块是(1)、(2)、(5)、(6); 要求: 1、 水果类fruit(属性:name,price,number,content) 2、 业务类fruitbiz 3、 测试类testfriut 注意业务类方法: 1、 查询所有数据show() 2、 添加数据add (fruit fruit) 3、 查询名称数

为了满足考试要求,我会提供一个简单的Java代码实现,包括`Fruit`类、`FruitBiz`类和`TestFruit`类。这里假设我们使用了`java.util.ArrayList`来存储水果信息,并使用`java.io.Serializable`接口进行序列化和反序列化以保存到磁盘文件。 ```java // Fruit.java import java.io.Serializable; public class Fruit implements Serializable { private String name; private double price; private int number; private String content; // 构造函数,getter和setter public Fruit(String name, double price, int number, String content) { this.name = name; this.price = price; this.number = number; this.content = content; } // getters and setters... } // FruitBiz.java import java.io.*; import java.util.ArrayList; public class FruitBiz { private ArrayList<Fruit> fruits; public FruitBiz() { fruits = new ArrayList<>(); } // 展示所有数据 public void show() { for (Fruit fruit : fruits) { System.out.println("Name: " + fruit.getName() + ", Price: " + fruit.getPrice()); } } // 添加数据 public void add(Fruit fruit) { fruits.add(fruit); } // 查询名称数量 public int countByName(String name) { return fruits.stream().filter(f -> f.getName().equals(name)).count(); } // 按价格降序展示 public void sortByPriceDesc() { fruits.sort((a, b) -> Double.compare(b.getPrice(), a.getPrice())); show(); } // 保存到磁盘 public void saveToDisk(String fileName) { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) { oos.writeObject(fruits); } catch (IOException e) { e.printStackTrace(); } } // 从磁盘加载 public void loadFromDisk(String fileName) { try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) { fruits = (ArrayList<Fruit>) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } // TestFruit.java public class TestFruit { public static void main(String[] args) { FruitBiz biz = new FruitBiz(); // 添加水果 Fruit apple = new Fruit("Apple", 5.99, 100, "Fresh"); biz.add(apple); // 展示所有水果 biz.show(); // 查看特定水果数量 int appleCount = biz.countByName("Apple"); System.out.println("Apple count: " + appleCount); // 按价格降序展示 biz.sortByPriceDesc(); // 保存到磁盘 biz.saveToDisk("fruit.bin"); // 退出 biz.loadFromDisk("fruit.bin"); biz.show(); } } ``` 这个代码实现了基本的功能需求,你可以根据实际需求进行调整和优化。请确保在运行这段代码之前,已经创建了一个名为"fruit.bin"的文件,以便测试保存和加载功能。 [2024-06-19 12:48:33 | AI写代码神器 | 683点数解答]

相关提问