Question

main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...

  1. main

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.

  1. displayMenu

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.

  1. addData

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.

  1. deleteData

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.

  1. printData

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?

Homework Answers

Answer #1

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)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; public class ListDriver {    /*    * main    *    * An...
import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a string array. User input is retrieved from that std in.    * A text based menu is displayed that contains a number of options.    * The user is prompted to choose one of the available options:    * (1) Build List, (2) Add item, (3) Remove item, (4) Remove...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Add a second data member (another int) to the class ObjX. Then in main assign a...
Add a second data member (another int) to the class ObjX. Then in main assign a value to the new variable and print it. class ObjX { // No "static" keyword for either member. int i; void print () { System.out.println ("i=" + i); } } public class DynamicExample { public static void main (String[] argv) { // First create an instance, which allocates space from the heap. ObjX x = new ObjX (); // Now access members via the...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
Practice manipulating an object by calling its methods. Complete the class Homework1 class using String methods....
Practice manipulating an object by calling its methods. Complete the class Homework1 class using String methods. Remember that all the String methods are accessors. They do not change the original String. If you want to apply multiple methods to a String, you will need to save the return value in a variable. Complete the class by doing the following: + Print the word in lowercase + Replace "e" with "3" (Use the unmodified variable word) + Print the changed word...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT