Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Final Instructions After completing step 4, the basic functionality of the CarLot application is up and running! After completing step 5, be sure to read

Final Instructions

After completing step 4, the basic functionality of the CarLot application is up and running!

After completing step 5, be sure to read the Final Submission Instructions below.

In step 5, you must make two enhancements:

Enhancement 1 - Change CarLot to Use Inheritance

The CarLot class currently has one attribute, inventory, which is of type ( ArrayList). CarLot HAS-A ArrayList.

Change the CarLot class so that it uses inheritance instead of composition. In other words, CarLot IS-A ArrayList. If required, modify the CarLotTester class as well.

Hint: Remove the inventory attribute

Grading Elements:

  1. CarLot successfully uses inheritance
  2. The CarLot tester runs successfully
  3. CarLot has not attributes

Enhancement 2 - Student's Choice

After implementing the CarLot application, you have probably identified areas where the application can improve. Implement at least 1 improvement in the CarLot class. Be sure to document the enhancement and identify the enhancement in your java code.

Enhancements that illustrate extra creativity and technical mastery are eligible to receive up to 10 additional extra credit points for the project

Grading Elements:

  1. Student's enhancement is clearly documented
  2. Student's enhancement clearly improves the CarLot application
  3. The CarLot application continues to correctly function. If the enhancement changes the behavior of the application, the changes must be appropriate and correctly implemented

Final Submission Instructions

  1. Package all of your project files into a single zip file
  2. Include ALL files.
    1. Car.java
    2. CarLot.java
    3. CarLotMain.java
    4. All tester classes
    5. A sample carlot.txt file containing an inventory of at least 3 cars, one of which must be sold
    6. Any other files

**********************************************************

public class Car{

private String identifier = ""; private int miles = 0; private double mpg = 0; private int cost = 0; private int salesPrice = 0; private boolean isSold = false; private double priceSold = 0.0; private double profit = 0.0;

public Car(){}

public Car(String identifier, int miles, double mpg, int cost, int salesPrice){ this.identifier = identifier; this.miles = miles; this.mpg = mpg; this.cost = cost; this.salesPrice = salesPrice; }

public String getIdentifier(){ return this.identifier; }

public int getMilage(){ return this.miles; }

public double getMPG(){ return this.mpg; }

public int getCost(){ return this.cost; }

public int getPrice(){ return this.salesPrice; }

public double getPriceSold(){ return this.priceSold; }

public String isSold(){ if(this.isSold){ return "Yes"; } else{ return "No"; } } public double getProfit(){ return this.profit; }

public String toString(){ return "Car: " + this.identifier + ", Mileage: " + this.miles + ", MPG: " + this.mpg + ", Sold: " + isSold() + ", Cost: " + this.cost + ", Selling price: " + this.salesPrice + ", Sold For " + this.priceSold + ", Profit: " + this.profit; }

public void setID(String id){ this.identifier = id; }

public void setMileage(int mileage){ this.miles = mileage; }

public void setMPG(double mpg){ this.mpg = mpg; }

public void setCost(int cost){ this.cost = cost; }

public void setSalesPrice(int sales){ this.salesPrice = sales; }

public void sellCar(double priceSold){ this.priceSold = priceSold; this.isSold = true; this.profit = this.priceSold - this.cost; }

public double compareMPG(Car otherCar){ if(this.mpg < otherCar.mpg){ return -1; } else if(this.mpg == otherCar.mpg){ return 0; } else{ return 1; } }

public int compareMiles(Car otherCar){ if(this.miles < otherCar.miles){ return -1; } else if(this.miles == otherCar.miles){ return 0; } else{ return 1; } }

public int comparePrice(Car otherCar){ if(this.salesPrice < otherCar.salesPrice){ return -1; } else if(this.salesPrice == otherCar.salesPrice){ return 0; } else{ return 1; } }

}

*********************************************************

import java.util.ArrayList; import java.io.IOException; import java.util.Scanner; import java.io.File; import java.io.PrintWriter;

public class CarLot {

private ArrayList inventory = new ArrayList<>();

public ArrayList getInventory() { return this.inventory; }

public void setInventory(ArrayList inventory) { this.inventory = inventory; }

public Car findCarByIdentifier(String identifier) { Car id = new Car(); for (Car car : this.inventory) { String carID = car.getIdentifier().trim(); String IdCheck = identifier.trim(); if (carID.equals(IdCheck)) { id = car; break; } else { id = null; } } return id; }

public ArrayList getCarsInOrderOfEntry() { ArrayList entries = new ArrayList<>(this.inventory); return entries; }

public ArrayList getCarsSortedByMPG() { ArrayList sortedByMPG = new ArrayList<>(this.inventory); selectionSort(sortedByMPG); return sortedByMPG; }

private static void selectionSort(ArrayList sortedByMPG) { int size = sortedByMPG.size(); for (int i = 0; i < size - 1; i++) { Car lowestMPG = sortedByMPG.get(i); int currentMinIndex = i;

for (int j = i + 1; j < size; j++) { Car two = sortedByMPG.get(j); double res = lowestMPG.compareMPG(two); if (res == 1) { lowestMPG = two; currentMinIndex = j; } }

if (currentMinIndex != i) { Car otherCar = sortedByMPG.get(currentMinIndex); sortedByMPG.set(currentMinIndex, sortedByMPG.get(i)); sortedByMPG.set(i, otherCar); } } }

public Car getCarWithBestMPG() { ArrayList sorted = getCarsSortedByMPG(); return sorted.get(sorted.size() - 1); }

public Car getCarWithHighestMileage() { ArrayList MileageCheck = this.inventory; Car highestMileage = new Car(); int highest = 0; for (Car car : MileageCheck) { if (car.getMilage() > highest) { highest = car.getMilage(); highestMileage = car; } } return highestMileage; }

public double getAverageMpg() { double average = 0.0; int sum = 0; ArrayList carList = this.inventory; int size = carList.size(); for (Car car : carList) { sum += car.getMPG(); } average = sum / (double) size; return average; }

public double getTotalProfit() { double totalProfit = 0.0; for (Car car : this.inventory) { if (car.isSold() == "Yes") { totalProfit += car.getProfit(); } } return totalProfit; }

public void addCar(String identifier, int miles, double mpg, int cost, int salesPrice) { Car newCar = new Car(identifier, miles, mpg, cost, salesPrice); this.inventory.add(newCar); }

public void sellCar(String identifier, double priceSold) throws IllegalArgumentException {

Car sold = findCarByIdentifier(identifier); if (sold != null) { sold.sellCar(priceSold); } else { throw new IllegalArgumentException("Could not find the car " + identifier); } }

public void saveToDisk() throws IOException {

File csvFile = new File("carlot.txt"); if (csvFile.exists()) { System.out.println("File already exists in system"); System.exit(0); } System.out.println("writing the file in the system.. :"); PrintWriter printer = new PrintWriter(csvFile); ArrayList cars = this.inventory; for (Car car : cars) { String row = car.getIdentifier() + "," + car.getMilage() + "," + car.getMPG() + "," + car.getCost() + "," + car.getPrice(); printer.println(row); }

printer.close(); System.out.println("Done File Writing"); }

public ArrayList loadFromDisk() {

this.inventory.clear(); try {

File file = new File("carlot.txt"); Scanner input = new Scanner(file); System.out.println("Reading file...");

while (input.hasNextLine()) { String line = input.nextLine(); String[] lineArray = line.split(",", 5); String identifier = lineArray[0]; int miles = Integer.parseInt(lineArray[1]); double mpg = Double.parseDouble(lineArray[2]); int cost = Integer.parseInt(lineArray[3]); int salesPrice = Integer.parseInt(lineArray[4]); Car car = new Car(identifier, miles, mpg, cost, salesPrice); this.inventory.add(car); } input.close();

} catch (Exception ex) { ex.printStackTrace(); } System.out.println("Finished reading.."); System.out.println("Inventory loaded: " + this.inventory.toString());

return this.inventory; }

}

***********************************************************************

import java.io.IOException; import java.util.Scanner;

public class CarLotMain {

public static void main(String[] args) throws IOException { char anyKey; int option; CarLot car = new CarLot(); Scanner input = new Scanner(System.in); do { System.out.println("[0] Exit"); System.out.println("[1] Add a car to inventory"); System.out.println("[2] Sell a car from inventory"); System.out.println("[3] List inventory by order of acquisition"); System.out.println("[4] List Inventory From Best MPG to Worst MPG"); System.out.println("[5] Display car with best MPG"); System.out.println("[6] Display car with the highest mileage"); System.out.println("[7] Display overall MPG for the entire inventory"); System.out.println("[8] Display profit for all sold cars"); System.out.println("[9] save the file"); System.out.println("[10] load contents from file"); System.out.print(" Enter a number from 0 to 10: "); option = input.nextInt(); System.out.println("You Selected Option " + option);

if (option == 1) { System.out.println("Enter CarId, Mileage, MPG, Cost, Sales Price");

String CarId = input.next(); int mileage = input.nextInt(); double mpg = input.nextDouble(); int cost = input.nextInt(); int salesPrice = input.nextInt(); car.addCar(CarId, mileage, mpg, cost, salesPrice); System.out.println("Car Successfully Added to Inventory "); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0);

}

else if (option == 2) { System.out.println("Enter CarId, ActualSalesPrice"); String CarId = input.next(); double ActualSalesPrice = input.nextDouble(); car.sellCar(CarId, ActualSalesPrice); System.out.println("Car Sold!!"); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 3) { car.getCarsInOrderOfEntry(); System.out.println(car.getCarsInOrderOfEntry()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 4) { car.getCarsSortedByMPG(); System.out.println("Listing Inventory From Best MPG to Worst MPG " + car.getAverageMpg()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 5) { car.getCarWithBestMPG(); System.out.println("Displaying car with best MPG " + car.getCarWithBestMPG()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 6) { car.getCarWithHighestMileage(); System.out.println("Displaying car with the highest mileage " + car.getCarWithHighestMileage()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 7) { car.getAverageMpg(); System.out.println("Display overall MPG for the entire inventory " + car.getAverageMpg()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 8) { car.getTotalProfit(); System.out.println("Displaying profit for all sold cars " + car.getTotalProfit()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); } else if (option == 9) { car.saveToDisk(); System.out.println(car.getCarsInOrderOfEntry()); System.out.println("Enter any number to continue:"); anyKey = input.next().charAt(0); }

else if (option == 10) { car.loadFromDisk(); anyKey = input.next().charAt(0); System.out.println("Enter any number to continue:"); } } while ((option >= 1) && (option <= 10)); input.close(); System.out.println("End of Program"); } }

****************************************

import java.util.ArrayList; import java.io.IOException;

public class CarLotTester {

public static void main(String[] args){

Car carNum1 = new Car("Test1", 100000, 25.0, 1100, 11000); Car carNum2 = new Car("Test2", 60000, 30.0, 1200, 25500); Car car3 = new Car(); car3.setID("Toyota"); car3.setMPG(40.0); car3.setMileage(15000); car3.setCost(5000); car3.setSalesPrice(11000);

ArrayList addCars = new ArrayList<>(); addCars.add(carNum1); addCars.add(carNum2); addCars.add(car3);

CarLot carLotOne = new CarLot(); carLotOne.setInventory(addCars);

carLotOne.addCar("Nissan", 20000, 24, 5000, 8000);

try{ carLotOne.saveToDisk(); } catch(IOException e){ System.out.println(e.getStackTrace()); }

try{ carLotOne.loadFromDisk(); } catch(Exception e){ System.out.println(e.getMessage()); }

System.out.println(); System.out.println("1. Cars in ascending order of entry: " + carLotOne.getCarsInOrderOfEntry()); System.out.println("2. Sort cars by MPG in ascending order: " + carLotOne.getCarsSortedByMPG()); System.out.println("3. Car with best MPG: " + carLotOne.getCarWithBestMPG()); System.out.println("4. Car with highest mileage: " + carLotOne.getCarWithHighestMileage()); System.out.println("5. Average MPG of the cars: " + carLotOne.getAverageMpg()); carLotOne.sellCar("Nissan", 10000); System.out.println("Total profit from cars sold: " + carLotOne.getTotalProfit());

carLotOne.sellCar("Random", 20000); } }

*****************************************************************************************

public class CarTester {

public static void main(String[] args) {

try {

Car carNum1 = new Car("Test1", 12000, 25.0, 1100, 11000); Car carNum2 = new Car("Test2", 60000, 30.0, 1200, 25500);

Car car3 = new Car(); car3.setID("Toyota"); car3.setMileage(15000); car3.setMPG(40.0); car3.setCost(5000); car3.setSalesPrice(11000);

carNum1.sellCar(13000.00); carNum2.sellCar(15000.00);

System.out.println(carNum1); System.out.println(carNum2); System.out.println(car3);

System.out.println(carNum1.compareMPG(carNum2)); System.out.println(carNum1.compareMiles(carNum2)); System.out.println(carNum1.comparePrice(carNum2)); } catch (Exception ex) {

System.err.println("Error....: " + ex.getMessage()); }

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Who do you usually turn to for help when facing a problem?

Answered: 1 week ago