In: Computer Science
Project Description
In this project you will build a car configuration application in six units. Each unit provides learning opportunities in Object Oriented Design. You are expected to document these lessons and apply them in the next unit. You will notice that the design guidance will taper off as you progress through units in Project 1. You will be expected to design on your own.
Project 1 - Unit 1
In this project you will build a Car Configuration Application using Java. In this unit you will develop a “reference” base object model, read a text file to build the reference base object model and archive it using Serialization.
I would like you to start with a proof of concept – so we will first build the underlying object using normal Java Classes and Inner Classes.
For our proof of concept please consider the following requirements:
We will build Ford's Focus Wagon ZTW model with these options:
● Color - Fort Knox Gold Clearcoat Metallic, Liquid Grey Clearcoat Metallic, Infra-Red Clearcoat, Grabber Green Clearcoat Metallic, Sangria Red Clearcoat Metallic, French Blue Clearcoat Metallic, Twilight Blue Clearcoat Metallic, CD Silver Clearcoat Metallic, Pitch Black Clearcoat, Cloud 9 White Clearcoat
● Transmission - automatic or manual
● Brakes/Traction Control - Standard, ABS, or ABS with Advance Trac
● Side Impact Airbags - present or not present
● Power Moonroof - present or not present
Configuration options and cost data:
| 
 Base Price  | 
 $18,445  | 
| 
 Color  | 
 No additional cost  | 
| 
 Transmission  | 
 0 for automatic, $815 for standard (this is a "negative option")  | 
| 
 Brakes/Traction Control  | 
 $0 for standard, $400 for ABS, $1625 for ABS with Advance Trac  | 
| 
 Side Impact Air Bags  | 
 $0 for none, $350 if selected  | 
| 
 Power Moonroof  | 
 $0 for none, $595 if selected  | 
Your Deliverable:
Design and code classes for these requirements and write a driver program to instantiate a Ford Wagon ZTW object and write it to a file. Test your code with a couple of instances of Forward Wagon ZTW.
package file;
import java.io.*;
import java.util.*;
// Defines class CarConfiguration implements Serializable
interface
class CarConfiguration implements Serializable
{
   // Instance variables to store car configuration
information
   String carName;
   double basePrice;
   String color;
   String transmission;
   String brakesOrTractionControl;
   String sideImpactAirbags;
   String powerMoonroof;
  
   // Instance variables to store calculated cost
   double transmissionCost;
   double brakesOrTractionControlCost;
   double sideImpactAirbagsCost;
   double powerMoonroofCost;
   double finalCost;
  
   // Default constructor to assign default values to
instance variables
   CarConfiguration()
   {
       basePrice = 0.0;
       carName = color = transmission =
brakesOrTractionControl =
          
    sideImpactAirbags = powerMoonroof = null;
       transmissionCost =
brakesOrTractionControlCost = sideImpactAirbagsCost =
       powerMoonroofCost = finalCost =
0.0;
   }// End of default constructor
  
   // Parameterized constructor to assign parameter
values to instance variables
   CarConfiguration(double basePrice, String carName,
String color,
           String
transmission, String brakesOrTractionControl,
           String
sideImpactAirbags, String powerMoonroof)
   {
       this.basePrice = basePrice;
       this.carName = carName;
       this.color = color;
       this.transmission =
transmission;
       this.brakesOrTractionControl
=brakesOrTractionControl;
       this.sideImpactAirbags =
sideImpactAirbags;
       this.powerMoonroof =
powerMoonroof;
   }// End of parameterized constructor
  
   // Overloads method to return car configuration
information
   public String toString()
   {
       return "\n Base Price: $" +
basePrice + "\n Car Name: " + carName +
          
    "\n Color: " + color +
          
    "\n Transmission: " + transmission +
          
    "\n Transmission Cost: $" + transmissionCost
+
          
    "\n Brakes / Traction Control: " +
brakesOrTractionControl +
          
    "\n Brakes / Traction Control Cost: $" +
brakesOrTractionControlCost +
          
    "\n Side Impact Airbags: " + sideImpactAirbags
+
          
    "\n Side Impact Airbags Cost: $" +
sideImpactAirbagsCost +
          
    "\n Power Moonroof: " + powerMoonroof +
          
    "\n Power Moonroof Cost: $" + powerMoonroofCost
+
          
    "\n Final Amount: $" + finalCost + "\n";
   }// End of method
}// End of class CarConfiguration
// Driver class definition
public class CarConfigurationApplication
{
   // Creates an array list to store CarConfiguration
objects
   ArrayList<CarConfiguration> cars = new
ArrayList<>();
  
   // Method to calculate each type cost and final
amount
   void calculateCost()
   {
       // Loops till number of records
      
       for(int c = 0; c < cars.size();
c++)
       {
           // Checks if
transmission is automatic
          
if(cars.get(c).transmission.equalsIgnoreCase("automatic"))
          
    // Assigns the amount 0.0
          
    cars.get(c).transmissionCost = 0.0;
          
           // Otherwise
transmission is manual
           else
          
    // Assigns the amount 815.0
          
    cars.get(c).transmissionCost = 815.0;
          
           // Checks if
brakes/tractionControl is standard
          
if(cars.get(c).brakesOrTractionControl.equalsIgnoreCase("standard"))
          
    cars.get(c).brakesOrTractionControlCost =
0.0;
          
           // Otherwise
checks if brakes/tractionControl is ABS
           else
if(cars.get(c).brakesOrTractionControl.equalsIgnoreCase("ABS"))
          
    cars.get(c).brakesOrTractionControlCost =
400.0;
          
           // Otherwise
brakes/tractionControl is ABS with Advance Trac
           else
          
    cars.get(c).brakesOrTractionControlCost =
1625.0;
          
           // Checks if
side impact airbags is present
          
if(cars.get(c).sideImpactAirbags.equalsIgnoreCase("present"))
          
    cars.get(c).sideImpactAirbagsCost = 350.0;
          
           // Otherwise
side impact airbags is not present
           else
          
    cars.get(c).sideImpactAirbagsCost = 0.0;
          
           // Checks if
power moonroof is present
          
if(cars.get(c).powerMoonroof.equalsIgnoreCase("present"))
          
    cars.get(c).powerMoonroofCost = 595.0;
          
           // Otherwise
power moonroof is not present
           else
          
    cars.get(c).powerMoonroofCost = 0.0;
          
           // Calculates
final cost
          
cars.get(c).finalCost = cars.get(c).basePrice +
          
        cars.get(c).transmissionCost
+
          
       
cars.get(c).brakesOrTractionControlCost +
          
       
cars.get(c).sideImpactAirbagsCost +
          
       
cars.get(c).powerMoonroofCost;
          
       
       }// End of for loop
   }// End of method
  
   // Method to read records from file
   void readRecords()
   {
       // Scanner class object created for
file
       Scanner readF = null;
      
       // try block begins
       try
       {      
   
           // Opens the
file for reading
           // Scanner class
constructor take File class object as parameter
           // File class
constructor takes file name as parameter
           readF = new
Scanner(new File("carConfig.txt"));
          
           // Loops till
end of record
           // hasNextLine
method returns true if any
           // data
available in file
          
while(readF.hasNextLine())
           {
          
    // Reads a record
          
    String record = readF.nextLine();
          
    // Split the record with comma delimiter
          
    String each[] = record.split(", ");
          
    // Creates an object using parameterized
constructor
          
    // adds it to array list
          
    cars.add(new
CarConfiguration(Double.parseDouble(each[0]),
          
            each[1],
each[2], each[3], each[4], each[5], each[6]));
           }// End of while
loop          
   
       }// End of try block
          
       
       // Catch block to handle
FileNotFoundException exception
       catch(FileNotFoundException
fe)
       {
          
System.out.print("\n Unable to open the file for reading.");
       }// End of catch block
      
       // Calls the method to calculate
cost
       calculateCost();
      
       // Closes the Scanner class object
for file
       readF.close();
   }// End of method
  
   // Method to write records to file (serialize an
Object)
   void writeFile()
   {         
       // try block begins
       try
       {
           // Creates an
object of the class FileOutputStream to open the file
           FileOutputStream
fileOutStr = new FileOutputStream("carConfigAmt.txt");
          
           // Creates an
object of the class ObjectOutputStream to write object
          
ObjectOutputStream outObj = new
ObjectOutputStream(fileOutStr);
          
           // Calls the
method to write array list objects
          
outObj.writeObject(cars);
          
           // Close the
object stream
          
outObj.close();
           // Closer the
file
          
fileOutStr.close();
       }// End of try block
      
       // Catch block to handle
FileNotFoundException
       catch(FileNotFoundException
e)
       {
          
e.printStackTrace();
       }// End of catch block
      
       // Catch block to handle
IOException
       catch(IOException e)
       {
          
e.printStackTrace();
       }// End of catch block
   }// End of method
  
   // Method to read records from file (deserialize an
Object)
   void readFile()
   {
       // try block begins
       try
       {
           // Creates an
object of the class FileInputStream to open the file
           FileInputStream
fileInStr = new FileInputStream("carConfigAmt.txt");
          
           // Creates an
object of the class ObjectInputStream to read object
          
ObjectInputStream inObj = new ObjectInputStream(fileInStr);
          
           // Calls the
method to read object and displays it
          
System.out.println("Car Configuration: \n" +
          
       
inObj.readObject().toString());
          
           // Close the
object stream
          
inObj.close();
           // Closer the
file
          
fileInStr.close();
       }
       // End of try block
      
       // Catch block to handle
ClassNotFoundException
       catch(ClassNotFoundException
e)
       {
          
e.printStackTrace();
       }// End of catch block
      
       // Catch block to handle
FileNotFoundException
       catch(FileNotFoundException
e)
       {
          
e.printStackTrace();
       } // End of catch block
      
       // Catch block to handle
IOException
       catch(IOException e)
       {
          
e.printStackTrace();
       }// End of catch block
   }// End of method
  
   // main method definition
   public static void main(String []ss)
   {
       // Creates an object of class
CarConfigurationApplication
       CarConfigurationApplication cca =
new CarConfigurationApplication();
      
       // Calls the method to read records
from file
       cca.readRecords();
       // Calls the method to write
records with amount
       cca.writeFile();
       // Calls the method to read records
from file and displays it
       cca.readFile();
   }// End of main method
}// End of driver class
Sample Output:
Car Configuration:
[
Base Price: $560000.0
Car Name: Ford Wagon ZTW
Color: Fort Knox Gold Clearcoat Metallic
Transmission: automatic
Transmission Cost: $0.0
Brakes / Traction Control: ABS
Brakes / Traction Control Cost: $400.0
Side Impact Airbags: present
Side Impact Airbags Cost: $350.0
Power Moonroof: present
Power Moonroof Cost: $595.0
Final Amount: $561345.0
,
Base Price: $890000.0
Car Name: Centro
Color: Liquid Grey Clearcoat Metallic
Transmission: manual
Transmission Cost: $815.0
Brakes / Traction Control: ABS with Advance Trac
Brakes / Traction Control Cost: $1625.0
Side Impact Airbags: not present
Side Impact Airbags Cost: $0.0
Power Moonroof: present
Power Moonroof Cost: $595.0
Final Amount: $893035.0
]
carConfig.txt file contents
560000.00, Ford Wagon ZTW, Fort Knox Gold Clearcoat Metallic,
automatic, ABS, present, present
890000.00, Centro, Liquid Grey Clearcoat Metallic, manual, ABS with
Advance Trac, not present, present