A. Create a class that holds the service description, price and number of minute
ID: 3771200 • Letter: A
Question
A. Create a class that holds the service description, price and number of minutes it takes to perform the service. Include a constructor that requires arguments for all three data fields and three get methods that return one of the data field’s values. Save the class as Service.java
Service Description Price ($) Time(Minutes)
Cut 8.00 15
Shampoo 4.00 10
Manicure 18.00 30
Style 48.00 55
Permanent 18.00 35
Trim 6.00 5
B. Write an application named SalonReport that contains an array to hold six Service objects, and fill it with the data from the above table. Hard code the data from the above table into your program (do not have your program request the user to input the data). Include methods to sort the array of Service objects in ascending order by each of the data fields. Prompt the user for the preferred sort order, and display the list of services in the requested order. Save the program as SalonReport.java
C. Enhance the program by displaying a menu that asks the user how they want to sort the services menu. 1) Sort by Service Description, 2) Sort by Price, 3) Sort by Time (Minutes), or 0) to Exit. Add a do...while() loop that keeps prompting the user for the next preferred sort order until the user finally chooses “0” to exit.
Explanation / Answer
public class Service {
private String description;
private double price;
private int time;
public Service(String description, double price, int time) {
super();
this.description = description;
this.price = price;
this.time = time;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public int getTime() {
return time;
}
@Override
public String toString() {
return this.description+" "+this.price+" "+this.time;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class SalonReport {
private ArrayList<Service> services = new ArrayList<Service>();
public SalonReport() {
services.add( new Service("Cut", 8.00, 15));
services.add( new Service("Shampoo", 4.00, 10));
services.add( new Service("Manicure", 18.00, 30));
services.add( new Service("Style", 48.00, 55));
services.add( new Service("Permanent", 18.00, 35));
services.add( new Service("Trim", 6.00, 5));
}
public ArrayList<Service> sortByDescription () {
Collections.sort(services, new Comparator<Service>(){
@Override
public int compare(Service s1, Service s2) {
return s1.getDescription().compareTo(s2.getDescription());
}
});
return services;
}
public ArrayList<Service> sortByPrice () {
Collections.sort(services, new Comparator<Service>(){
@Override
public int compare(Service s1, Service s2) {
if(s1.getPrice() <= s2.getPrice()){
return -1;
} else {
return 1;
}
}
});
return services;
}
public ArrayList<Service> sortByTime () {
Collections.sort(services, new Comparator<Service>(){
@Override
public int compare(Service s1, Service s2) {
if(s1.getTime() <= s2.getTime()){
return -1;
} else {
return 1;
}
}
});
return services;
}
public static void main(String[] args) {
int choice=-1;
Scanner s = new Scanner(System.in);
SalonReport report = new SalonReport();
ArrayList<Service> result = null;
do{
System.out.println("Please choose the select order");
System.out.println("1) Sort by Service Description");
System.out.println("2) Sort by Price");
System.out.println("3) Sort by Time (Minutes), or");
System.out.println("0) to Exit.");
System.out.print("Your Input: ");
choice = s.nextInt();
switch (choice) {
case 1:
result = report.sortByDescription();
break;
case 2:
result = report.sortByPrice();
break;
case 3:
result = report.sortByTime();
break;
default:
continue;
}
System.out.println("Service Description Price ($) Time(Minutes)");
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
}while(choice!=0);
s.close();
}
}
Output:
Please choose the select order
1) Sort by Service Description
2) Sort by Price
3) Sort by Time (Minutes), or
0) to Exit.
Your Input: 1
Service Description Price ($) Time(Minutes)
Cut 8.0 15
Manicure 18.0 30
Permanent 18.0 35
Shampoo 4.0 10
Style 48.0 55
Trim 6.0 5
Please choose the select order
1) Sort by Service Description
2) Sort by Price
3) Sort by Time (Minutes), or
0) to Exit.
Your Input: 2
Service Description Price ($) Time(Minutes)
Shampoo 4.0 10
Trim 6.0 5
Cut 8.0 15
Permanent 18.0 35
Manicure 18.0 30
Style 48.0 55
Please choose the select order
1) Sort by Service Description
2) Sort by Price
3) Sort by Time (Minutes), or
0) to Exit.
Your Input: 3
Service Description Price ($) Time(Minutes)
Trim 6.0 5
Shampoo 4.0 10
Cut 8.0 15
Manicure 18.0 30
Permanent 18.0 35
Style 48.0 55
Please choose the select order
1) Sort by Service Description
2) Sort by Price
3) Sort by Time (Minutes), or
0) to Exit.
Your Input: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.