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.
Use the following driver (or something similar) for testing entire project.
class Driver {
public static void main(String [] args) {
//Build Automobile Object from a file.
Automobile FordZTW = (Some instance method in a class of Util package).readFile("FordZTW.txt");
//Print attributes before serialization
FordZTW.print();
//Serialize the object
Lab1.autoutil.FileIO.serializeAuto(FordZTW);
//Deserialize the object and read it into memory.
Automobile newFordZTW = Lab1.autoutil.FileIO.DeserializeAuto("auto.ser"); //Print new attributes.
newFordZTW.print();
}
}
Ans)
Java program
Driver.java:
package driver;
import model.Automotive;
import util.FileIO;
public class Driver {
public static void main(String[] args) {
FileIO file = new FileIO();
Automotive FordZTW = file.readFile("FordZTW.txt");
System.out.println("Print attributes before serialization");
FordZTW.print();
// serialize Automotive object
file.serializeAuto(FordZTW);
// Deserialize Automotive object and read it into memory
Automotive newFordXTW = file.deserializeAuto("auto.ser");
System.out.println("\nNew Automotive attributes are:");
newFordXTW.print();
System.out.println("\nSeraching Option Set by name 'color':");
newFordXTW.findOptionSet("color");
System.out.println("\nSeraching Option 'automatic' in Option Set 'transmission':");
FordZTW.findOption("transmission", "automatic");
}
}
Automotive.java:
package model;
import java.io.*;
public class Automotive implements Serializable {
private String name;
private float basePrice;
private OptionSet opsets[];
private int nextInsertPosition;
// default constructor
public Automotive() {
name = "";
basePrice = 0f;
opsets = null;
// initializing counter to 0
nextInsertPosition = 0;
}
// parameterized constructor
public Automotive(String name, float basePrice, int optionSetSize) {
opsets = new OptionSet[optionSetSize];
this.name = name;
this.basePrice = basePrice;
// initializing counter to 0
nextInsertPosition = 0;
}
// getters
public String getName() {
return name;
}
public float getBasePrice() {
return basePrice;
}
public OptionSet getOpsets(int i) {
return opsets[i];
}
// setters
public void setName(String name) {
this.name = name;
}
public void setBasePrice(float basePrice) {
this.basePrice = basePrice;
}
public void setOpset(OptionSet[] opsets) {
this.opsets = opsets;
}
// toString() converts buffered string to a string
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Automotive Name: ").append(name)
.append("Base Price: ").append(basePrice);
String str = stringBuffer.toString();
return str;
}
// print() prints automotive object's attributes
public void print() {
System.out.println(toString());
for (int i = 0; i < opsets.length; ++i) {
opsets[i].print();
}
}
// addOptionSet() adds optionSet in the optionSet array
public void addOptionSet(String str, int length) {
OptionSet optset = new OptionSet(str, length);
opsets[nextInsertPosition] = optset;
nextInsertPosition++;
}
// addOption() adds option in the required OptionSet.
public void addOption(String optionSetName, String optionName,
float price) {
for (int i = 0; i < opsets.length; ++i) {
OptionSet.Option opt = opsets[i].new Option();
if (opsets[i].getName() == optionSetName) {
opt.setName(optionName);
opt.setPrice(price);
opsets[i].addOption(opt);
break;
}
}
}
// findOptionSet() searches for the required optionSet using optionSetName
// and returns OptionSet object if found else null.
public OptionSet findOptionSet(String optionSetName) {
for (int i = 0; i < opsets.length; ++i) {
if (opsets[i].getName().equalsIgnoreCase(optionSetName)) {
opsets[i].print();
return opsets[i];
}
}
return null;
}
// findOption() searches for the option in the required optionSet using
// optionName and returns option object if found else null.
public OptionSet.Option findOption(String optionSetName,
String optionName) {
for (int i = 0; i < opsets.length; ++i) {
if (opsets[i].getName().equalsIgnoreCase(optionSetName)) {
return opsets[i].findOption(optionName);
}
}
return null;
}
// deleteOptionSet() deletes the required optionset from the option set
// array
public boolean deleteOptionSet(String optionSetName) {
OptionSet opset = findOptionSet(optionSetName);
if (opset == null)
return false;
OptionSet[] newOptset = new OptionSet[opsets.length - 1];
for (int j = 0; j < opsets.length - 1; ++j) {
if (opsets[j] != opset) {
newOptset[j] = opsets[j];
}
}
opsets = newOptset;
return true;
}
// deleteOption() deletes the required option from the optionSetArray
public boolean deleteOption(String optionSetName, String optionName) {
OptionSet opset = findOptionSet(optionSetName);
if (opset != null) {
return opset.deleteOption(optionName);
}
return false;
}
}
OptionSet.java:
package model;
import java.io.*;
public class OptionSet implements Serializable {
private Option opts[];
private String name;
private int nextInsertPosition;
// default constructor
public OptionSet() {
opts = null;
name = "";
nextInsertPosition = 0;
}
// parameterized constructor
public OptionSet(String name, int size) {
opts = new Option[size];
this.name = name;
nextInsertPosition = 0;
}
// OptionSet getters:
protected Option[] getOpts() {
return opts;
}
protected String getName() {
return name;
}
// OptionSet setters:
protected void setOpt(Option[] opts) {
this.opts = opts;
}
protected void setName(String name) {
this.name = name;
}
// addOption() adds new Option to the OptionSet
protected void addOption(Option option) {
opts[nextInsertPosition] = option;
nextInsertPosition++;
}
// findOption() finds option by optionName in the Option set
protected Option findOption(String optionName) {
for (int i = 0; i < opts.length; ++i) {
if (opts[i].getName().equalsIgnoreCase(optionName)) {
opts[i].print();
return opts[i];
}
}
return null;
}
// deleteOption()
protected boolean deleteOption(String OptionName) {
Option opt = findOption(OptionName);
// if option not found, return false
if (opt == null)
return false;
Option[] newOptions = new Option[opts.length - 1];
for (int i = 0; i < opts.length - 1; ++i) {
if (opts[i] != opt)
newOptions[i] = opts[i];
}
opts = newOptions;
return true;
}
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("OptionSet Name: ").append(name);
String str = stringBuffer.toString();
return str;
}
// print() prints automotive object's attributes
protected void print() {
System.out.println(toString());
for (int i = 0; i < opts.length; ++i) {
opts[i].print();
}
}
public class Option implements Serializable {
private String name;
private float price;
// default constructor
public Option() {
name = "NULL";
price = 0;
}
// Option class getters
protected String getName() {
return name;
}
protected float getPrice() {
return price;
}
// Option class setters
protected void setName(String name) {
this.name = name;
}
protected void setPrice(Float price) {
this.price = price;
}
// toString() converts buffered string to a string
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Option Name: ").append(name)
.append(", Price: $").append(price);
String str = stringBuffer.toString();
return str;
}
// print() prints Option object's attributes
protected void print() {
System.out.println(toString());
}
}
}
FileIO.java:
package util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import model.Automotive;
public class FileIO {
// read a file and store its attributes in Automotive object
public Automotive readFile(String fileName) {
Automotive automotive = null;
try {
FileReader file = new FileReader(fileName);
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
// read and store base price in automotive object
if (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else {
String[] retval = line.split(":");
float basePrice = Float.parseFloat(retval[1]);
automotive = new Automotive("Focus Wagon ZTW", basePrice,
5);
}
}
while (!eof) {
final String line = buff.readLine();
if (line == null)
eof = true;
else {
final String[] lineParts = line.split(":");
String[] options = lineParts[1].split(";");
automotive.addOptionSet(lineParts[0].trim(),
options.length);
for (int i = 0; i < options.length; ++i) {
String[] optionAttributes = options[i].split(",");
automotive.addOption(lineParts[0].trim(),
optionAttributes[0].trim(),
Float.parseFloat(optionAttributes[1]));
}
}
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
return automotive;
}
// serialize Automotive object in a file
public void serializeAuto(Automotive automotive) {
try {
FileOutputStream fileOut = new FileOutputStream("auto.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(automotive);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in auto.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
// deserialize Automotive object from given file
public Automotive deserializeAuto(String fileName) {
Automotive automotive = null;
try {
FileInputStream fileIn = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fileIn);
automotive = (Automotive) in.readObject();
in.close();
fileIn.close();
return automotive;
} catch (IOException i) {
i.printStackTrace();
return null;
} catch (ClassNotFoundException c) {
System.out.println("Automotive class not found");
c.printStackTrace();
return null;
}
}
}
Output
if using package names using in below screen shot
---------*************"***--------------
if your satisfy above answer please give positive rating or like ?
please don't dislike
Thank you!