Question: Java Write a pair of serialization / deserialization apps that create a users Car list, w/ a class named Car, which includes model, VINumber (int),
Java
Write a pair of serialization / deserialization apps that create a users Car list, w/ a class named Car, which includes model, VINumber (int), and CarMake (an enum, with valid values FORD, GM, TOYOTA, and HONDA) as fields. Use an array similar to the way the SerializeObjects did to handle several BankAccounts. Your app must include appropriate Exception Handling via try catch blocks (what if the file is not found, or the user inputs characters instead of digits for VINumber).
import java.io.*; import java.util.Scanner;
public class SerializeObjects { public static void main(String[] args) throws IOException { double balance; // An account balance final int NUM_ITEMS = 3; // Number of accounts // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Create a BankAccount2 array BankAccount2[] accounts = new BankAccount2[NUM_ITEMS]; // Populate the array. for (int i = 0; i < accounts.length; i++) { // Get an account balance. System.out.print("Enter the balance for " + "account " + (i + 1) + ": "); balance = keyboard.nextDouble();
// Create an object in the array. accounts[i] = new BankAccount2(balance); } // Create the stream objects. FileOutputStream outStream = new FileOutputStream("Objects.dat"); ObjectOutputStream objectOutputFile = new ObjectOutputStream(outStream); // Write the serialized objects to the file. for (int i = 0; i < accounts.length; i++) { objectOutputFile.writeObject(accounts[i]); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
