follow these steps and answer the questions(except question3) ? I also want to see class changes. I
Fantastic news! We've Found the answer you've been seeking!
Question:
follow these steps and answer the questions(except question3) ? I also want to see class changes. I will attach all classes that you need. Thank you!
EvilAddDriver.java
package com.sait.itsc315.application; import java.io.*; import com.sait.itsc315.managers.VehicleManagementSystem; import com.sait.itsc315.problemdomain.Vehicle; public class EvilAddDriver { /** * Entry point for program. * @param args * @throws IOException */ public static void main(String[] args) throws IOException { VehicleManagementSystem vms = new VehicleManagementSystem(); Vehicle evil = new Vehicle(-999, "Evil", 1, "ABCD1234", 1.50); vms.getVehicles().add(evil); vms.save(); } }
GoodAddDriver.java
package com.sait.itsc315.application; import java.io.*; import com.sait.itsc315.managers.VehicleManagementSystem; import com.sait.itsc315.problemdomain.Vehicle; public class GoodAddDriver { /** * Entry point for program. * @param args * @throws IOException */ public static void main(String[] args) throws IOException { VehicleManagementSystem vms = new VehicleManagementSystem(); vms.addVehicle(1234, "Toyota Camry", 2015, "1GCRCREC5FZ145755", 9995); vms.save(); } }
VehicleManagementSystem.java
package com.sait.itsc315.managers; import java.io.*; import java.util.*; import com.sait.itsc315.problemdomain.*; public class VehicleManagementSystem { public static final String TEXT_FILE = "res/vehicles.csv"; public static final String BINARY_FILE = "res/vehicles.bin"; private ArrayList vehicles; /** * Constructor for Vehicle Management System. * @throws IOException */ public VehicleManagementSystem() throws IOException { this.vehicles = new ArrayList<>(); try { this.loadFromBinary(); } catch (IOException e) { this.loadFromText(); } } /** * Loads vehicles from file. * @throws IOException */ private void loadFromText() throws IOException { BufferedReader br = new BufferedReader(new FileReader(TEXT_FILE)); String line = br.readLine(); while (line != null) { String[] parts = line.split(","); int id = Integer.parseInt(parts[0]); String name = parts[1]; int year = Integer.parseInt(parts[2]); String vin = parts[3]; double price = Double.parseDouble(parts[4]); Vehicle vehicle = new Vehicle(id, name, year, vin, price); this.vehicles.add(vehicle); line = br.readLine(); } br.close(); } /** * Loads vehicles from the binary file (if it exists). * @throws IOException */ private void loadFromBinary() throws IOException { RandomAccessFile raf = new RandomAccessFile(new File(BINARY_FILE), "rw"); int length = raf.readInt(); for (int i = 0; i < length; i++) { int id = raf.readInt(); String name = raf.readUTF(); int year = raf.readInt(); String vin = raf.readUTF(); double price = raf.readDouble(); Vehicle vehicle = new Vehicle(id, name, year, vin, price); this.vehicles.add(vehicle); } raf.close(); } /** * Saves vehicles to binary file. * @throws IOException */ public void save() throws IOException { RandomAccessFile raf = new RandomAccessFile(new File(BINARY_FILE), "rw"); raf.writeInt(this.vehicles.size()); for (Vehicle vehicle : this.vehicles) { raf.writeInt(vehicle.getId()); raf.writeUTF(vehicle.getName()); raf.writeInt(vehicle.getYear()); raf.writeUTF(vehicle.getVin()); raf.writeDouble(vehicle.getPrice()); } raf.close(); } /** * Adds a vehicle. * @param id ID * @param name Name * @param year Year * @param vin VIN * @param price Price */ public void addVehicle(int id, String name, int year, String vin, double price) { Vehicle vehicle = new Vehicle(id, name, year, vin, price); this.vehicles.add(vehicle); } /** * Finds a vehicle with an ID. * @param id Vehicle ID. * @return Found vehicle or null if not found. */ public Vehicle findById(int id) { Vehicle found = null; for (Vehicle vehicle : vehicles) { if (vehicle.getId() == id) { found = vehicle; break; } } return found; } /** * Displays a vehicles information. * @param id ID of vehicle. * @param brand Brand * @param model Model * @param year Year * @param price Price */ public void display(int id, String name, int year, String vin, double price) { System.out.printf("ID:\t%d\n", id); System.out.printf("Name:\t%s\n", name); System.out.printf("Year:\t%d\n", year); System.out.printf("VIN:\t%s\n", vin); System.out.printf("Price:\t%.2f\n", price); } /** * Gets the vehicles * @return List of vehicles. */ public ArrayList getVehicles() { return this.vehicles; } }
SearchDriver.java
package com.sait.itsc315.application; import java.util.*; import com.sait.itsc315.managers.*; import com.sait.itsc315.problemdomain.*; import java.io.*; public class SearchDriver { /** * Entry point for program. * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); VehicleManagementSystem vms = new VehicleManagementSystem(); System.out.print("Enter the ID of the vehicle: "); int idToFind = in.nextInt(); System.out.println(); Vehicle vehicle = vms.findById(idToFind); if (vehicle == null) { System.out.println("No vehicle with that ID was found."); return; } System.out.println("Found vehicle information:"); /***** Start modifying code here ******/ int id = vehicle.getId(); String name = vehicle.getName(); int year = vehicle.getYear(); String vin = vehicle.getVin(); double price = vehicle.getPrice(); // Check if the vehicle is a Toyota and create a Toyota object if (id >= 200 && id <= 299) { Toyota toyota = new Toyota(vehicle); price = toyota.getPrice(); } /***** End modifying code here ******/ vms.display(id, name, year, vin, price); vms.save(); in.close(); } }
Vehicle.java
package com.sait.itsc315.problemdomain; public final class Vehicle { private final int id; private final String name; private final int year; private final String vin; private final double price; /** * Constructor for Vehicle. * @param id ID * @param brand Brand of vehicle. * @param model Model of vehicle. * @param year Year vehicle was made. * @param price Price of vehicle. */ public Vehicle(int id, String name, int year, String vin, double price) { this.id = id; this.name = name; this.year = year; this.vin = vin; this.price = price; } /** * @return the id */ public int getId() { return id; } /** * @param id the id to set * @throws UnsupportedOperationException */ public void setId(int id) { throw new UnsupportedOperationException("Cannot modify immutable object."); } /** * @return the name */ public String getName() { return name; } /** * @param brand the name to set * @throws UnsupportedOperationException */ public void setName(String name) { throw new UnsupportedOperationException("Cannot modify immutable object."); } /** * @return the year */ public int getYear() { return year; } /** * @param year the year to set * @throws UnsupportedOperationException */ public void setYear(int year) { throw new UnsupportedOperationException("Cannot modify immutable object."); } /** * @return the vin */ public String getVin() { return vin; } /** * @param vin the vin to set * @throws UnsupportedOperationException */ public void setVin(String vin) { throw new UnsupportedOperationException("Cannot modify immutable object."); } /** * @return the price */ public double getPrice() { return price; } /** * @param price the price to set * @throws UnsupportedOperationException */ public void setPrice(double price) { throw new UnsupportedOperationException("Cannot modify immutable object."); } }
res/vehicles.csv
143 | BMW F10 | 2019 | 9FC1CJ130AJGUM9L1 | 14788 |
527 | Honda That S | 2013 | BDST4Z374A5W5NE1D | 8766 |
522 | Honda Logo | 2012 | HCDK3U417ALDJM247 | 8671 |
488 | Chevrolet Explorer | 2016 | ZP4RUS0C1ARAFYPZJ | 13235 |
497 | Chevrolet Trax | 2004 | YVP9WMN00AXXTA84Y | 2510 |
353 | Ford Fairlane | 2015 | 0BJGPE5J1AEF8A5X3 | 14208 |
381 | Ford F-450 | 2020 | 71GKXAZC6A9MT08S5 | 12605 |
278 | Toyota Brevis | 2019 | X78PCY983A9L0R9D7 | 12572 |
291 | Toyota Tercel | 2015 | CM4LUZY38AB7LDXE7 | 13268 |
127 | BMW 128 | 2008 | BZH6S3851A84932EM | 1833 |
171 | BMW 528 | 2018 | 81W5XX948A4F0NW28 | 12593 |
586 | Honda CR-V | 2003 | EEE1CMR94ALM5ZBVE | 2936 |
Related Book For
Cost Management Accounting and Control
ISBN: 978-0324559675
6th Edition
Authors: Don R. Hansen, Maryanne M. Mowen, Liming Guan
Posted Date: