In: Computer Science
Write an inventory program in java for a used car lot. You should have one Car parent class and child classes for 3 vehicle types (convertible, SUV, Classic, etc). The parent class should have 3 variables and 3 functions, each child class should have 1 variable unique to it and 1 function. Your program should allow the user to store up to 100 cars in the inventory and write the cars out to a file when done.
PROGRAM CODE:
Car.java
package carinventory;
public class Car {
private String plateNumber;
private int numberOfSeats;
private int numberOfDoors;
Car(String number, int seats, int doors)
{
plateNumber = number;
numberOfSeats = seats;
numberOfDoors = doors;
}
public String getPlateNumber() {
return plateNumber;
}
public int getNumberOfSeats() {
return numberOfSeats;
}
public int getNumberOfDoors() {
return numberOfDoors;
}
@Override
public String toString() {
return "Plate Number: " + plateNumber + " Seats: " + numberOfSeats + " Doors: " + numberOfDoors;
}
}
Convertible.java
package carinventory;
public class Convertible extends Car{
//to know if the convertible roof is automated or manual
private boolean isAutomated;
Convertible(String number, int seats, int doors, boolean automated) {
super(number, seats, doors);
this.isAutomated = automated;
}
public boolean getIsAutomated()
{
return isAutomated;
}
@Override
public String toString() {
return super.toString() + " Automated: " + isAutomated;
}
}
Classic.java
package carinventory;
public class Classic extends Car{
//To know if the classic car is more than 100 years old
private boolean isAntique;
Classic(String number, int seats, int doors, boolean antique) {
super(number, seats, doors);
this.isAntique = antique;
}
public boolean getIsAntique()
{
return isAntique;
}
@Override
public String toString() {
return super.toString() + " Antique: " + isAntique;
}
}
SUV.java
package carinventory;
public class SUV extends Car{
private boolean isCrossOver;
SUV(String number, int seats, int doors, boolean crossOver) {
super(number, seats, doors);
this.isCrossOver = crossOver;
}
public boolean getIsCrossOver()
{
return isCrossOver;
}
@Override
public String toString() {
return super.toString() + " Cross over: " + isCrossOver;
}
}
Inventory.java
package carinventory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class Inventory {
private Car cars[];
private int size;
public Inventory() {
cars = new Car[100];
size = 0;
}
public void addCar(Car car)
{
cars[size++] = car;
}
public void writeToFile(String fileName)
{
try {
FileOutputStream out = new FileOutputStream(new File(fileName));
PrintWriter writer = new PrintWriter(out);
for(int i=0; i<size; i++)
{
writer.write(cars[i].toString() + "\n");
}
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
carInventoryTester.java
package carinventory;
public class carInventoryTester {
public static void main(String[] args) {
Inventory carInventory = new Inventory();
Car toyota = new SUV("TWS 234", 4, 4, true);
Car classic = new Classic("QWS 345", 2, 2, true);
Car convertible = new Convertible("TER 456", 4, 2, false);
carInventory.addCar(toyota);
carInventory.addCar(classic);
carInventory.addCar(convertible);
carInventory.writeToFile("Cars.txt");
}
}
OUTPUT: