Question

In: Computer Science

We are to make a program about a car dealership using arrays. I got the code...

We are to make a program about a car dealership using arrays. I got the code to display all cars in a list, so I'm good with that. What I'm stuck at is how to make it so when a user inputs x for search, it allows them to search the vehicle. We need two classes, one that shows the car information and another that shows the insert, search, delete, display methods. Here is what I have so far

package a1chrisd;

import java.util.Scanner;

public class Car {

    /**
     * @param args the command line arguments
     */
    String color;
    String model;
    String year;
    String company;
    String plate;

    public Car(String color, String model, String year, String company, String plate) {
        this.color = color;
        this.model = model;
        this.year = year;
        this.company = company;
        this.plate = plate;
    }


   public void introduceSelf() {
       System.out.println("This is a" + " " + this.color + " " + this.year + " " + this.company + " " + this.model + " with plate " + this.plate);
    }

    public static void main(String[] args){
         String arrModel[] = {"Corolla", "Mustang", "Cavalier", "LaSabre", "Civic",
                             "Accord", "Avalon", "Escalade", "XTS", "A220", "Crown Victoria"}; // Car models
       String arrPlate[] = {"11111", "22222", "33333", "44444", "55555",
                              "66666", "77777", "88888", "99999", "00000"}; // car plates
         String arrCompany[] = {"Toyota", "Ford", "Cheverolet", "Buick", "Honda",
                                "Honda", "Toyota", "Cadillac", "Cadillac", "Mercedes Benz", "Ford"}; // car company
         String arrColor[] = {"Red", "White", "White", "Green", "Black",
                               "Green", "Blue", "Black", "Orange", "Brown", "Blue"}; // car color
        String arrYear[] = {"2015", "2019", "1987", "2020", "2020",
                                "2001", "2004", "2016", "2010", "1991"}; // car year
                      
      
        Scanner in = new Scanner(System.in);
   int carsInserted = 0;
           Car arrCar[] = new Car[50];
           for (int i = 0; i < 10; i++){
               Car c = new Car(arrColor[i], arrYear[i], arrCompany[i], arrModel[i], arrPlate[i]);
               arrCar[i] = c;
           }
           
            for(int i = 0; i < 10; i++){
                arrCar[i].introduceSelf();
            }
    }
   

Solutions

Expert Solution

Here is the completed code for this problem. The updated Car class and new CarList class has been attached. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Car.java

public class Car {

      String color;

      String model;

      String year;

      String company;

      String plate;

      public Car(String color, String model, String year, String company,

                  String plate) {

            this.color = color;

            this.model = model;

            this.year = year;

            this.company = company;

            this.plate = plate;

      }

      public void introduceSelf() {

            System.out.println("This is a" + " " + this.color + " " + this.year

                        + " " + this.company + " " + this.model + " with plate "

                        + this.plate);

      }

      // getters and setters for each field

      public String getColor() {

            return color;

      }

      public void setColor(String color) {

            this.color = color;

      }

      public String getModel() {

            return model;

      }

      public void setModel(String model) {

            this.model = model;

      }

      public String getYear() {

            return year;

      }

      public void setYear(String year) {

            this.year = year;

      }

      public String getCompany() {

            return company;

      }

      public void setCompany(String company) {

            this.company = company;

      }

      public String getPlate() {

            return plate;

      }

      public void setPlate(String plate) {

            this.plate = plate;

      }

}

// CarList.java

import java.util.Scanner;

public class CarList {

      private Car[] cars;

      private int count;

      // maximum capacity of above array

      private static final int CAPACITY = 100;

      // constructor initializing empty car list

      public CarList() {

            cars = new Car[CAPACITY];

            count = 0; // current count of cars

      }

      // adds a car to the end

      public void insert(Car c) {

            // adding to index count if array is not full

            if (count < cars.length) {

                  cars[count] = c;

                  count++;

            }

      }

      // removes a car with given plate from array, if found. returns true if

      // found and removed, false if not

      public boolean delete(String plate) {

            // looping and searching for car with given plate

            for (int i = 0; i < count; i++) {

                  if (cars[i].getPlate().equals(plate)) {

                        // found, shifting all remaining cars to left one place

                        for (int j = i; j < count - 1; j++) {

                              cars[j] = cars[j + 1];

                        }

                        // updating count

                        count--;

                        // success

                        return true;

                  }

            }

            // not found

            return false;

      }

      // displays all cars in list

      public void display() {

            for (int i = 0; i < count; i++) {

                  cars[i].introduceSelf();

            }

      }

      // searches a car by plate and returns it if found, else null

      public Car search(String plate) {

            for (int i = 0; i < count; i++) {

                  if (cars[i].getPlate().equals(plate)) {

                        // found

                        return cars[i];

                  }

            }

            // not found

            return null;

      }

      public static void main(String[] args) {

            // creating a car list

            CarList list = new CarList();

            // scanner to read user input

            Scanner scanner = new Scanner(System.in);

            String ch = "";

            // loops until user chooses to quit

            while (!ch.equalsIgnoreCase("q")) {

                  // displaying menu

                  System.out.println("\na. add a car");

                  System.out.println("b. display all cars");

                  System.out.println("c. search a car");

                  System.out.println("d. delete a car");

                  System.out.println("q. quit");

                  System.out.print("Your choice: ");

                  // reading choice

                  ch = scanner.nextLine();

                  // handling choice

                  if (ch.equalsIgnoreCase("a")) {

                        // reading inputs for a car

                        System.out.print("Color: ");

                        String color = scanner.nextLine();

                        System.out.print("Model: ");

                        String model = scanner.nextLine();

                        System.out.print("Year: ");

                        String year = scanner.nextLine();

                        System.out.print("Company: ");

                        String company = scanner.nextLine();

                        System.out.print("Plate: ");

                        String plate = scanner.nextLine();

                        // creating a car, adding to list

                        Car car = new Car(color, model, year, company, plate);

                        list.insert(car);

                  } else if (ch.equalsIgnoreCase("b")) {

                        // displaying all cars

                        list.display();

                  } else if (ch.equalsIgnoreCase("c")) {

                        // reading plate and searching for car with given plate

                        System.out.print("Enter the plate of car you want to search: ");

                        String plate = scanner.nextLine();

                        Car c = list.search(plate);

                        if (c == null) {

                              //not found

                              System.out.println("Not found!");

                        } else {

                              System.out.print("Found: ");

                              c.introduceSelf();

                        }

                  } else if (ch.equalsIgnoreCase("d")) {

                        //reading plate and deleting car with given plate

                        System.out.print("Enter the plate of car you want to delete: ");

                        String plate = scanner.nextLine();

                        if (list.delete(plate)) {

                              System.out.println("Deleted successfully!");

                        } else {

                              System.out.println("Not found!");

                        }

                  }

            }

      }

}

/*OUTPUT*/

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: 1

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: a

Color: Blue

Model: Mustang

Year: 1995

Company: Ford

Plate: 1122

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: b

This is a Blue 1995 Ford Mustang with plate 1122

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: a

Color: Red

Model: Corolla

Year: 2003

Company: Toyota

Plate: 1200

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: b

This is a Blue 1995 Ford Mustang with plate 1122

This is a Red 2003 Toyota Corolla with plate 1200

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: a

Color: Green

Model: Camry

Year: 2000

Company: Chevy

Plate: 1010

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: b

This is a Blue 1995 Ford Mustang with plate 1122

This is a Red 2003 Toyota Corolla with plate 1200

This is a Green 2000 Chevy Camry with plate 1010

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: c

Enter the plate of car you want to search: 1200

Found: This is a Red 2003 Toyota Corolla with plate 1200

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: c

Enter the plate of car you want to search: 888

Not found!

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: d

Enter the plate of car you want to delete: 1122

Deleted successfully!

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: b

This is a Red 2003 Toyota Corolla with plate 1200

This is a Green 2000 Chevy Camry with plate 1010

a. add a car

b. display all cars

c. search a car

d. delete a car

q. quit

Your choice: q


Related Solutions

I got this homework, to make an appointment program. it says like this: Write a superclass...
I got this homework, to make an appointment program. it says like this: Write a superclass Appointment and subclasses OneTime, Daily, and Monthly. An Appointment has a description (for example “see the dentist”) and a date. Write a method occursOn(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. You are provided with a Junit test AppointmentTest .java. Run...
JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
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...
In this assignment you are to make an inventory management system for a car dealership. The...
In this assignment you are to make an inventory management system for a car dealership. The dealerships current method of tracking inventory is using a list which has the color and name of each car they have in inventory. There current inventory list will look like the example below. RED TOYOTAWHITE HONDA...BLACK BMW The types of cars that they order are as follows: TOYOTA, HONDA, BMW, NISSAN, TESLA, DODGE The colors they purchase are as follows: RED, WHITE, BLUE, BLACK,...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
My code does not compile, I am using vim on terminal and got several compiling errors...
My code does not compile, I am using vim on terminal and got several compiling errors this is C++ language I need help fixing my code below is the example expected run and my code. Example run (User inputs are highlighted): Enter your monthly salary: 5000 Enter number of months you worked in the past year: 10 Enter the cost of the car: 36000 Enter number of cars you’ve sold in the past year: 30 Enter number of misconducts observed...
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
You are about to start working at car dealership that is currently reporting losses due to...
You are about to start working at car dealership that is currently reporting losses due to flooding but will be profitable in a few years. Assume you’re your risk adverse and your supervisor cannot fully monitor your actions. The key metrics at this dealership include both financial data (number of sales, margin on sales) as well as qualitative data (survey of experience). You are tasked with designing a compensation contract. 1. Define in your own terms moral hazard and adverse-selection...
You are about to start working at car dealership that is currently reporting losses due to...
You are about to start working at car dealership that is currently reporting losses due to flooding but will be profitable in a few years. Assume you’re your risk adverse and your supervisor cannot fully monitor your actions. The key metrics at this dealership include both financial data (number of sales, margin on sales) as well as qualitative data (survey of experience). You are tasked with designing a compensation contract. 1. Define in your own terms moral hazard and adverse-selection...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT