Question

In: Computer Science

Write an inventory program in java for a used car lot. You should have one Car...

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.

Solutions

Expert Solution

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:


Related Solutions

Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
in java pls Write a Car Dealership management program. This program will allow the employees of...
in java pls Write a Car Dealership management program. This program will allow the employees of the Dealership to add cars to their database and sell them. You will be implementing two classes: Car and CarDealership. Car will store data associated with the cars and CarDealership will be the management program. Note: 5 points of your grade is based on Coding Style. You will need to update the Starter Code to follow the standards described here. Use the "Run" button...
Write the following java program. Desc: The program computes the cost of parking a car in...
Write the following java program. Desc: The program computes the cost of parking a car in a public garage at the rate $5.00/hour. The client will always be charged for whole hours. For example, if a car parked for 2 hours and 1 minute, the client will be charged for 3 hours. Input: User inputs the entry time and exit time in 24-hr clock format (hh:mm) Output: The enter and exit times, the length of time the car is parked...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT