Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vehicle
- {
- int year, miles;
- double purchasePrice, forSalePrice;
- double [] priceHistory = new double[5];
- String status = "Unknown", make, model, VIN, color;
- String [] feature = new String[5];
- public Vehicle (int yr, String carMake)
- {
- year = yr;
- make = carMake;
- model = "N/A";
- VIN = "N/A";
- color = "N/A";
- }
- //
- public Vehicle (int yr, String carMake, String carModel)
- {
- year = yr;
- make = carMake;
- model = carModel;
- VIN = "N/A";
- color = "N/A";
- }
- public Vehicle(int yr, String carMake, String carModel, char status, double forSalePrice)
- {
- year = yr;
- make = carMake;
- model = carModel;
- setStatus(status);
- this.forSalePrice = forSalePrice;
- color = "N/A";
- VIN = "N/A";
- }
- public void setYear(int vehYear)
- {
- year = vehYear;
- }
- public void setMiles(int vehMiles)
- {
- miles = vehMiles;
- }
- public void setPurchasePrice(double price)
- {
- purchasePrice = price;
- }
- public void setForSalePrice(double price)
- {
- updatePriceHistory(price);
- forSalePrice = price;
- }
- public void setMake(String make)
- {
- this.make = make;
- }
- public void setModel(String model)
- {
- this.model = model;
- }
- public void setVIN(String VIN)
- {
- this.VIN = VIN;
- }
- public void setColor(String color)
- {
- this.color = color;
- }
- public void setFeature(String newFeature)
- {
- int i = 0;
- for(i = 0; i < feature.length; i++)
- {
- if(feature[i] == null)
- {
- feature[i] = newFeature;
- break;
- }
- }
- if(i == feature.length)
- System.out.println("Feature not added, please remove a feature to add new ones.");
- }
- public void removeFeature(int a)
- {
- feature[a] = null;
- while(a < feature.length - 1)
- {
- String temp = feature[a];
- feature[a] = feature[a + 1]; //sorting algorithm
- feature[a + 1] = temp;
- a++;
- }
- }
- public String getFeature(int feat)
- {
- if(feat >= feature.length || feat < 0)
- return "Invalid feature index.";
- else
- {
- return feature[feat];
- }
- }
- public int getFeatureLength()
- {
- return feature.length;
- }
- public boolean setStatus(char stat)
- {
- switch (stat)
- {
- case 'a': status="Active"; break;
- case 'c': status="Canceled"; break;
- case 's': status="Sold"; break;
- default: status="Unknown";
- }
- if (status.equals("Unknown")) return false;
- else return true;
- }
- public String getStatus()
- {
- return status;
- }
- public String getListing()
- {
- return make + " " + model +" " + year + " " + color + " " + forSalePrice;
- }
- public String getAllDetails()
- {
- String temp = feature[0];
- if(temp == null)
- return "Make: " + make + "\n" + "Model: " + model + "\n" + "Year: " + year + "\n" + "Color: " + color + "\n" + "Purchase price: " + purchasePrice + "\n" + "For Sale price: " + forSalePrice;
- else
- {
- for (int i = 1; i < getFeatureLength(); i++) {
- if (getFeature(i) != null)
- {
- temp = temp + ", " + feature[i];
- }
- }
- return "Make: " + make + "\n" + "Model: " + model + "\n" + "Year: " + year + "\n" + "Color: " + color + "\n" + "Purchase price: " + purchasePrice + "\n" + "For Sale price: " + forSalePrice + "\n" + "Features: " + temp;
- }
- }
- public double getSalePrice()
- {
- return forSalePrice;
- }
- private void updatePriceHistory(double newPrice)
- {
- priceHistory[4] = priceHistory[3];
- priceHistory[3] = priceHistory[2];
- priceHistory[2] = priceHistory[1];
- priceHistory[1] = priceHistory[0];
- priceHistory[0] = newPrice;
- }
- public String getMakeModel()
- {
- return make + " " + model;
- }
- }
- import java.util.Scanner;
- public class UsedCarLot {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Vehicle[] auto = { new Vehicle(1998, "Chevrolet"), new Vehicle(2012, "BMW", "M3"), new Vehicle(2015, "Ford"),
- new Vehicle(2008, "Hyundai", "Sonata", 'a', 12040), new Vehicle(2013, "Jeep", "Wrangler", 'a', 17900),
- new Vehicle(2016, "Mazda", "Miata"), new Vehicle(1969, "Ford", "Mustang") };
- System.out.println("Hi! Welcome to our Car Lot!");
- int input = 0;
- System.out.println("Select any number to perform the following operation.");
- while (input != 9) {
- System.out.println("1. Update Vehicle" + "\n" + "2. List Inventory (All)" + "\n"
- + "3. List Summary Inventory (For Sale only)" + "\n" + "4. List Extended Vehicle Details" + "\n"
- + "9. Exit");
- input = scanner.nextInt(); //Provides error relating to this line.
- switch (input) {
- case 1: // update vehicle
- System.out.println("Please select the vehicle you would like to update with the corresponding number.");
- for (int a = 0; a < auto.length - 1; a++) {
- int b = a + 1;
- System.out.println(b + ". " + auto[a].getMakeModel());
- }
- int v = scanner.nextInt();
- updateMenu(auto[v - 1]);
- break;
- case 2: // 2. List Inventory (All)
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- for (int a = 0; a < auto.length - 1; a++) {
- int c = a + 1;
- System.out.println(c + ". " + auto[a].getListing());
- }
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- break;
- case 3: // 3. List Summary Inventory (For Sale only)
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- int b = 1;
- for (int a = 0; a < auto.length - 1; a++) {
- if (auto[a].getStatus().equals("Active")) {
- System.out.println(b + ". " + auto[a].getListing());
- b++;
- }
- }
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- break;
- case 4:// 4. List ALL Vehicle Details
- System.out.println("Select the Make/Model of the car you would like to see extended details about.");
- for (int a = 0; a < auto.length - 1; a++) {
- int d = a + 1;
- System.out.println(d + ". " + auto[a].getMakeModel());
- }
- int detailScanner = scanner.nextInt();
- if (detailScanner < 1 || detailScanner > auto.length - 1)
- System.out.println("Invalid car number.");
- else {
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println(auto[detailScanner - 1].getAllDetails());
- }
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- break;
- case 9: // exit
- System.out.println("Thank you.");
- break;
- default:
- System.out.println("Please enter a number listed.");
- }
- }
- scanner.close();
- }
- private static void updateMenu(Vehicle auto) {
- Scanner scan = new Scanner(System.in);
- int input = 1;
- while (input != 0) {
- System.out.println("Please choose from the following:" + "\n" + "1. Update VIN" + "\n" + "2. Update Model"
- + "\n" + "3. Update Miles" + "\n" + "4. Update Purchase Price" + "\n" + "5. Update For Sale Price"
- + "\n" + "6. Update Status" + "\n" + "7. Update Color" + "\n" + "8. Add Feature" + "\n"
- + "9. Remove Feature" + "\n" + "0. Exit");
- input = scan.nextInt();
- switch (input) {
- case 1: // 1. Update VIN
- System.out.println("Please enter the desired VIN");
- String tempVIN = scan.next();
- if (tempVIN.length() == 17) {
- auto.setVIN(tempVIN);
- System.out.println("VIN successfully set to " + tempVIN + ".");
- } else
- System.out.println("invalid VIN length.");
- break;
- case 2: // 2. Update Model
- System.out.println("Please enter the desired Model, in either one word, or dashed.");
- String tempModel = scan.next();
- auto.setModel(tempModel);
- System.out.println("Model successfully set to " + tempModel + ".");
- break;
- case 3: // 3. Update Miles
- System.out.println("Please enter the desired milage");
- int tempMiles = scan.nextInt();
- auto.setMiles(tempMiles);
- System.out.println("Milage successfully set to " + tempMiles + ".");
- break;
- case 4: // 4. Update Purchase Price
- System.out.println("Please enter the desired Purchase Price");
- double tempPurchase = scan.nextDouble();
- auto.setPurchasePrice(tempPurchase);
- System.out.println("Purchase successfully set to " + tempPurchase + ".");
- break;
- case 5: // 5. Update For Sale Price
- System.out.println("Please enter the desired Sale Price");
- Double tempSale = scan.nextDouble();
- if (tempSale < auto.getSalePrice()) {
- System.out.println("This price is lower than the current for sale price. Do you wish to continue?"
- + "\n" + "1. Yes." + "\n" + "2. No.");
- int a = scan.nextInt();
- if (a == 1) {
- auto.setForSalePrice(tempSale);
- System.out.println("Sale Price successfully set to " + tempSale + ".");
- } else if (a == 2)
- System.out.println("Sale Price not updated.");
- else
- System.out.println("Invalid key. Please select 5 if you wish to update in the future.");
- } else {
- auto.setForSalePrice(tempSale);
- System.out.println("Sale Price successfully set to " + tempSale + ".");
- }
- break;
- case 6: // 6. Update Status
- System.out.println(
- "Please enter the desired Status. Enter a for Active Status. Enter c for Canceled Status. Enter s for Sold Status.");
- int temp = 0;
- while (temp == 0) {
- char tempStatus = scan.next().charAt(0);
- if (tempStatus != 'a' && tempStatus != 'c' && tempStatus != 's' && tempStatus != 'e')
- System.out.println("Invalid character, please try again, or e to exit.");
- else if (tempStatus == 'e')
- temp = 1;
- else {
- auto.setStatus(tempStatus);
- System.out.println("Status successfully set to " + tempStatus + ".");
- temp = 1;
- }
- }
- break;
- case 7:// 7. Update Color
- System.out.println("Please enter the desired Color, in either one word, or dashed.");
- String tempColor = scan.next();
- auto.setColor(tempColor);
- System.out.println("Color successfully set to " + tempColor + ".");
- break;
- case 8: // 8. Add Feature
- System.out.println("Please enter the desired Feature, in either one word, or dashed.");
- String tempFeat = " ";
- while (!tempFeat.equals("0")) {
- tempFeat = scan.next();
- if (!tempFeat.equals("0")) {
- auto.setFeature(tempFeat);
- System.out.println("You may type another feature, or 0 to exit.");
- }
- }
- break;
- case 9: // 9. Remove Feature
- System.out.println("Please remove the desired Feature (select number).");
- int temp2 = 1;
- while (temp2 != 0) {
- System.out.println("List of Features, select number to remove.");
- for (int i = 0; i < auto.getFeatureLength(); i++) {
- if (auto.getFeature(i) != null) {
- System.out.println(i + 1 + ". " + auto.getFeature(i));
- }
- }
- System.out.println("0. To Exit.");
- temp2 = scan.nextInt();
- if (temp2 != 0) {
- System.out.println("Feature Successfully removed, remove another or select 0 to exit.");
- auto.removeFeature(temp2 - 1);
- }
- }
- break;
- case 0: // exit
- break;
- default:
- System.out.println("Please enter a number listed.");
- break;
- }
- }
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement