Java Bootcamp – Workbooks and Challenges
)}

Dealership – Final Touch

From Java Bootcamp Resources -> Module 2 -> 7\. The World of Objects, open the folder: dealership.

If you can't find it, please download the updated resources from Github.

Task 1 – Adding user input.


Add the following code in main(). Then. pick up the user's replies using Scanner.

System.out.println("\n ****** JAVA DEALERSHIP! ****** \n");
System.out.print("Welcome! Enter the type of car you're looking for: ");
//pick up make
System.out.print("Enter your budget: ");
//pick up budget

Task 2 – Re-write the search action.


If the search method finds a car, it needs to return the index of that car. Otherwise, we won't know which car to sell.

  • So, change the return type to int.
/** * Function name: search * * @param make (String) * @param budget (int) * @return (int) <------- * * Inside the function: * 1. Loops through every element in the cars field. * 2. Skips the run if the element is null. * 3. If it finds a car the user wants and can afford: * - println: \nWe found a car in spot <i> \n\n <toString> * - print: If you're interested, type 'yes': * - returns the index * 4. If the loop runs and it didn't find a car * - println: \nYour search didn't match any results.\n * - returns 404 */
  • If it finds a car the user wants and can afford:

    • println : \nWe found a car in spot <i> \n\n <toString of car>

    • print : If you're interested, type 'yes':

    • return the index.

/** * Function name: search * * @param make (String) * @param budget (int) * @return (int) * * Inside the function: * 1. Loops through every element in the cars field. * 2. Skips the run if the element is null. * 3. If it finds a car the user wants and can afford: <------- * - println: \nWe found a car in spot <i> \n\n <toString> * - print: If you're interested, type 'yes': * - returns the index * 4. If the loop runs and it didn't find a car * - println: \nYour search didn't match any results.\n * - returns 404 */
  • If the loop runs and it didn't find a car:

    • println : \nYour search didn't match any results.\n

    • return 404.

/** * Function name: search * * @param make (String) * @param budget (int) * @return (int) * * Inside the function: * 1. Loops through every element in the cars field. * 2. Skips the run if the element is null. * 3. If it finds a car the user wants and can afford: * - println: \nWe found a car in spot <i> \n\n <toString> * - print: If you're interested, type 'yes': * - returns the index * 4. If the loop runs and it didn't find a car <------ * - println: \nYour search didn't match any results.\n * - returns 404 */

Task 3 – Call the search action.


Call the search action and store the return value in a variable.

Task 4 – Case 404 .


In the event of a 404, that means the search action couldn't find a car.

In which case:

  • println : Feel free to browse through our collection of cars.\n

  • print the dealership.

Example output:

>>: Welcome! Enter the type of car you're looking for: Mercedes
>>: Enter your budget: 1000

>>: Sorry, your search didn't match any results.

>>: Feel free to browse through our collection of cars.

>>: Parking Spot: 0
>>: Make: Nissan.
>>: Price: 5000.0.
>>: Year: 2020.
>>: Color: red.
>>: Parts: [tires, keys].

>>: Parking Spot: 1
>>: Make: Dodge.
>>: Price: 8500.0.
>>: Year: 2019.
>>: Color: blue.
>>: Parts: [tires, keys].

>>: Parking Spot: 2
>>: Make: Nissan.
>>: Price: 5000.0.
>>: Year: 2017.
>>: Color: yellow.
>>: Parts: [tires, filter].

>>: Parking Spot: 3
>>: Make: Honda.
>>: Price: 7000.0.
>>: Year: 2019.
>>: Color: orange.
>>: Parts: [tires, filter].

>>: Parking Spot: 4
>>: Make: Mercedes.
>>: Price: 12000.0.
>>: Year: 2015.
>>: Color: jet black.
>>: Parts: [tires, filter, transmission].

Task 5 – Selling the car.


If it finds a car, we know the last thing printed was:

If you're interested, type 'yes':

In that case, pick up the user's decision. If it's yes:

  • Sell them a car.

Example output:

>>: Welcome! Enter the type of car you're looking for: Mercedes
>>: Enter your budget: 13000

>>: We found a car in spot 4

>>: Make: Mercedes.
>>: Price: 12000.0.
>>: Year: 2015.
>>: Color: jet black.
>>: Parts: [tires, filter, transmission].

>>: If you're interested, type 'yes': yes

>>: You bought the beautiful 2015 jet black Mercedes for 12000.0.
>>: Please drive your car to the nearest exit.

Task 6 – Ignore letter cases (Optional)


The equalsIgnoreCase method ignores letter cases.

Use it to:

  • Ignore letter cases when the user types the make they're looking for.

  • Ignore letter cases when they type their decision.

Task 7 – Admire your work!

The dealership application offers a lot of functionality. Yet, I'll wager that your code in main() is expressive and easy to follow.

Therein lies the beauty of Object-Oriented Programming!

Good Luck!


Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.