Question: First the program should load all the objects from a binary save file into a JList model. the details of each object should be visible
First the program should load all the objects from a binary save file into a JList model. the details of each object should be visible to the user, either through the JList itself or using other components to display the info for the currently selected object the user should be able to add new car objects using GUI textboxes and an add button (The objects should be inserted into their sorted position in the JList model) the user should be able to delete objects in the JList by selecting them and using a delete button when the program closes, the objects in JList model should be saved back out to the binary file.
package com.company; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.io.*; import java.lang.Comparable; public class Main { public static void main(String[] args) { class cars { } ArrayList carList = new ArrayList(); // read binary file and load cars to arraylist Car car = null; try { FileInputStream fileIn = new FileInputStream("cars.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); while (true) { try { car = (Car) in.readObject(); carList.add(car); } catch (IOException e) { break; } } in.close(); fileIn.close(); } catch (IOException i) { } catch (ClassNotFoundException c) { } // print out list of cars if (carList.isEmpty()) System.out.println("List is Empty!"); else { System.out.println("Listing Cars (sorted by make,model,year):"); for (int i = 0; i < carList.size(); i++) { System.out.println( carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear()); } } // create a new car by user Scanner userInput = new Scanner(System.in); System.out.println("Creating new Car. Enter a make:"); String model, make, year; model = userInput.nextLine(); System.out.println("Enter a model: "); make = userInput.next(); System.out.println("Enter the year: "); year = userInput.next(); Car newCar = new Car(make, model, year); // add to list carList.add(newCar); // sort list Collections.sort(carList); // print list System.out.println(" Listing Cars (sorted by make):"); for (int i = 0; i < carList.size(); i++) { System.out.println( carList.get(i).getMake() + " " + carList.get(i).getModel() + " " + carList.get(i).getYear()); } // save arrayList to file try { FileOutputStream fileOut = new FileOutputStream("cars.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); for (int i = 0; i < carList.size(); i++) { out.writeObject(carList.get(i)); } out.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("List successfully backed up!"); } } class Car implements Serializable, Comparable { private String make; private String model; private String year; Car(String make, String model, String year) { this.model = make; this.make = model; this.year = year; } public String getMake() { return this.make; } public String getModel() { return this.model; } public String getYear() { return this.year; } @Override public int compareTo(Car o) { int i; i = this.make.compareTo(o.make); if (i != 0) return i; i = this.model.compareTo(o.model); if (i != 0) return i; else return this.year.compareTo(o.year); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
