In: Computer Science
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();
}
}
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