📄 shop.java
字号:
package autosale;import java.io.*;import java.util.*;public class Shop { private final String fileName = "shop.dat"; private List<Vehicle> vehicles; public Shop() { vehicles = new ArrayList<Vehicle>(); } private static void printMenu() { System.out.println("\n\nMain menu:"); System.out.println("1. New car"); System.out.println("2. New motorcycle"); System.out.println("3. Delete vehicle"); System.out.println("4. Show all vehicles"); System.out.println("5. Show all cars"); System.out.println("6. Show all motorcycles"); System.out.println("7. Quit"); } private static int chooseOption(int min, int max) throws Exception { Scanner sc = new Scanner(System.in); while (true) { System.out.print("Enter a number: "); int option = sc.nextInt(); if (option < min || option > max) { System.out.println("Invalid number, you must enter a value between " + String.valueOf(min) + " and " + String.valueOf(max) + "!"); } else { return option; } } } private void newCar() { vehicles.add(new Car()); } private void newMc() { vehicles.add(new Motorcycle()); } private void deleteVehicle() throws Exception { if (vehicles.size() == 0) { System.out.println("The shop is empty."); return; } showAllOfType(Vehicle.class); System.out.println("Select which vehicle to delete."); int selection = chooseOption(1, vehicles.size()); int pos = selection - 1; vehicles.remove(pos); } private void showAllOfType(Class c) throws Exception { if (vehicles.size() == 0) { System.out.println("The shop empty."); return; } for (int i = 0 ; i < vehicles.size() ; i++) { if (c.isInstance(vehicles.get(i))) { System.out.println(""); System.out.println(String.valueOf(i + 1)); Printable p = vehicles.get(i); p.print(); System.out.println(vehicles.get(i).getInsurancePrice()); } } } private void load() throws Exception { File f = new File(fileName); if (!f.exists()) return; ObjectInputStream os = new ObjectInputStream(new FileInputStream(fileName)); List v = (List)os.readObject(); os.close(); for (Object o : v) { vehicles.add((Vehicle)o); } } private void save() throws Exception { ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName)); os.writeObject(vehicles); os.close(); } private void run() { //Vehicle v = new Vehicle(); //vehicles.add(v); try { load(); while (true) { printMenu(); int choice = chooseOption(1, 7); switch (choice) { case 1: //New car newCar(); Collections.sort(vehicles, null); break; case 2: //New mc newMc(); Collections.sort(vehicles, null); break; case 3: //Delete car deleteVehicle(); break; case 4: //Show all vehicles showAllOfType(Vehicle.class); break; case 5: //Show all cars showAllOfType(Car.class); break; case 6: //Show all mcs showAllOfType(Motorcycle.class); break; case 7: //Quit save(); System.exit(0); break; } } } catch (Exception ex) { System.out.println("Unrecoverable error!"); System.out.println(ex.getMessage()); for (StackTraceElement ste : ex.getStackTrace()) { System.out.println(ste.getClassName() + "::" + ste.getMethodName() + " Line:" + ste.getLineNumber()); } } } public static void main(String[] args) { Shop s = new Shop(); s.run(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -