The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects.
It will call the displayMenu method to display the menu.
Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu.
Be sure to use the most appropriate statement for this type of repetition.
Parameters:
none
Return value:
none
Be sure to use the keyword static!
This method displays the menu, which should look like this:
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice:
Note that displayMenu should NOT read the user’s choice. It should only display. Main will read user input.
Parameters:
one ArrayList of type Car
Return value:
none
Be sure to use the keyword static!
This method prompts the user for the type and price of the car to be added. Instantiate a Car object with this information, and then call the add() method of the ArrayList object using this Car object.
Parameters:
one ArrayList of type Car
Return value:
none
Be sure to use the keyword static!
This method prompts the user for the type of car to delete. Use a standard for loop to iterate through the ArrayList object until a match is found. If a match is found, call the remove() method of the ArrayList object, using the appropriate index for that element. If a match is not found, display a warning message.
Parameters:
one ArrayList of type Car
Return value:
none
Be sure to use the keyword static!
This method uses the enhanced for loop syntax to iterate through the ArrayList and displays each car’s information in a neatly formatted list. The price must include a comma, and the prices must align on the right. Remember, you’re calling the get methods of the Car object at each position in the ArrayList.
Sample Run
Welcome to the JJC Car Dealership Inventory Program
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 1
Type of car: Ford
Price of car: 21000
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 1
Type of car: Honda
Price of car: 23000
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 3
Make Price
---- -----
Ford 21,000
Honda 23,000
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 2
Type of car to delete: Ford
Ford deleted from inventory
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 3
Make Price
---- -----
Honda 23,000
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 2
Type of car to delete: VW
No such car in inventory
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your choice: 4
this is my code as of right now:
public class Car {
private String make; // make of car as a string
private int price; // price of vechile
// two agruement constructor initialize make and price array
public Car(String make, int price) {
this.make = make;
this.price = price;
}
public String getMake() {
return make;
}
public int getPrice() {
return price;
}
}// end of Car class
import java.util.ArrayList;
import java.util.Scanner;
public class Dealer {
public static void main(String[] args) {
// creates a new arraylist of strings
ArrayList<String> carlist = new
ArrayList<String>();
// creates a Scanner object to obtain input from the command
window
Scanner input = new Scanner(System.in);
// display menu
System.out.printf("Welcome to the JJC Car Dealership Inventory
Program%n%n");
displayMenu();
// reads input from user
int inputs = input.nextInt();
while (inputs != 4){
// use if/else statements for user menu
if (inputs == 1){
// calls addData method
addData(carlist);
}// end of if input menu is 1 from user
else if (inputs == 2){
// calls deleteData method
deleteData(carlist);
}// end of if else from user entering 2 from menu
else if (inputs == 3){
printData(carlist);
}
displayMenu();
inputs = input.nextInt();
}// end of while
}// end of main
public static void displayMenu (){
// users choice in menu
System.out.printf("%n1. Add a car to inventory%n2. Delete a car
from inventory%n3. Print inventory%n4. Exit%n%n");
System.out.printf("Your Choice: ");
}// end of displayMenu method
public static void addData(ArrayList<String> car){
System.out.printf("%nType of car: "); // prints user for type of
car
Scanner addCar = new Scanner(System.in); // creates addCar object
to read user input
String make = addCar.nextLine();// reads user input
System.out.printf("Price of car: ");// prints user for price of
car
Scanner addPrice = new Scanner(System.in); // creates addPrice
object to read user inpiy
int price = addPrice.nextInt(); // read user input
}// end of addData method
public static void deleteData(ArrayList<String> car){
ArrayList<String> carlist = new
ArrayList<String>();
System.out.printf("%nType of car to delete: ");
Scanner deleteCar = new Scanner(System.in);
String delete = (deleteCar.nextLine());
}// end of deleteData method
public static void printData(ArrayList<String> car){
// print inventory
System.out.printf("%nMake Price%n");
System.out.printf("---- -----%n");
// display each element in car
for (String cars : car){
System.out.printf("%s", cars);
}
}// end of printData method
}// end of Dealer class
question, how do i use the arraylist to get car make and price and display it with commas. Also making a method to add a new car and price and delete the car along with its price?
I have changed the Dealer class, Car class is unchanged. The changed in Dealer class are highlighted
import java.util.ArrayList;
import java.util.Scanner;
public class Dealer {
public static void main(String[] args) {
// creates a new arraylist of strings
ArrayList carlist = new
ArrayList<Car>();
// creates a Scanner object to obtain input from the command
window
Scanner input = new Scanner(System.in);
// display menu
System.out.printf("Welcome to the JJC Car Dealership Inventory
Program%n%n");
displayMenu();
// reads input from user
int inputs = input.nextInt();
while (inputs != 4){
// use if/else statements for user menu
if (inputs == 1){
// calls addData method
addData(carlist);
}// end of if input menu is 1 from user
else if (inputs == 2){
// calls deleteData method
deleteData(carlist);
}// end of if else from user entering 2 from menu
else if (inputs == 3){
printData(carlist);
}
displayMenu();
inputs = input.nextInt();
}// end of while
}// end of main
public static void displayMenu (){
// users choice in menu
System.out.printf("%n1. Add a car to inventory%n2. Delete a car
from inventory%n3. Print inventory%n4. Exit%n%n");
System.out.printf("Your Choice: ");
}// end of displayMenu method
public static void
addData(ArrayList<Car> car){
System.out.printf("%nType of car: "); // prints user for type of
car
Scanner addCar = new Scanner(System.in); // creates addCar object
to read user input
String make = addCar.nextLine();// reads user input
System.out.printf("Price of car: ");// prints user for price of
car
Scanner addPrice = new Scanner(System.in); // creates addPrice
object to read user inpiy
int price = addPrice.nextInt(); // read user input
car.add(car.size(),new
Car(make,price));//creating a new car object and adding it to the
arrayList
}// end of addData method
public static void deleteData(ArrayList<Car> car){
ArrayList carlist = new ArrayList<Car>();
System.out.printf("%nType of car to delete: ");
Scanner deleteCar = new Scanner(System.in);
String delete = (deleteCar.nextLine());
for (int
i=0;i<car.size();i++){
if(
car.get(i).getMake().equals(delete)){//searching the index of the
particular car name and delete it from the arrayList
car.remove(i);
}
}
}// end of deleteData method
public static void printData(ArrayList<Car> car){
// print inventory
System.out.printf("%nMake\tPrice%n");
System.out.printf("------------------%n");
// display each element in car
for (int
i=0;i<car.size();i++){
System.out.println(
car.get(i).getMake()+"\t"+ car.get(i).getPrice());//printing the
details of the cars in the inventory
}
}// end of printData method
}// end of Dea
Output:
run:
Welcome to the JJC Car Dealership Inventory Program
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your Choice: 1
Type of car: ford
Price of car: 200
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your Choice: 3
Make Price
------------------
ford 200
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your Choice: 2
Type of car to delete: ford
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your Choice: 3
Make Price
------------------
1. Add a car to inventory
2. Delete a car from inventory
3. Print inventory
4. Exit
Your Choice: 4
BUILD SUCCESSFUL (total time: 29 seconds)
Get Answers For Free
Most questions answered within 1 hours.