a. Write an application for Cody’s Care Shop that shows a user a list of availab
ID: 3736525 • Letter: A
Question
a. Write an application for Cody’s Care Shop that shows a user a list of available services: oil change, tire rotation, battery check, or brake inspection. Allow the user to enter a string that corresponds to one of the options, and display the option and its price as $25, $22, $15, or $5, accordingly. Display an error message if the user enters an invalid item. Save the file as CarCareChoice.java
b. It might not be reasonable to expect users to type long entries such as “oil change” accurately. Modify the CareCareChoice class so that as long as the user enters the first three characters of a service, the choice is considered valid. Save the file as CarCareChoice2.java
Explanation / Answer
Please find my implementation.
Please rate my answer of you liked it!!
CarCareChoice.java
import java.util.Scanner;
public class CarCareChoice {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String services[] = {"oil change", "tire rotation", "battery check", "brake inspection"};
int prices[] = {25,22,15,5};
System.out.println("Enter the service: ");
String service = scan.nextLine();
boolean found = false;
for(int i=0;i<services.length;i++) {
if(services[i].equalsIgnoreCase(service)) {
System.out.println("Price: $"+prices[i]);
found = true;
break;
}
}
if(!found) {
System.out.println("an invalid item");
}
}
}
Output:
Enter the service:
tire rotation
Price: 22
CarCareChoice2.java
import java.util.Scanner;
public class CarCareChoice2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String services[] = {"oil change", "tire rotation", "battery check", "brake inspection"};
int prices[] = {25,22,15,5};
System.out.println("Enter the service: ");
String service = scan.nextLine();
boolean found = false;
for(int i=0;i<services.length;i++) {
if(services[i].substring(0,3).equalsIgnoreCase(service)) {
System.out.println("Price: $"+prices[i]);
found = true;
break;
}
}
if(!found) {
System.out.println("an invalid item");
}
}
}
Output:
Enter the service:
oil
Price: 25
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.