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
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).
Display 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.
Expert Answer
Explanation::
- Complete code is implemented in java.
- Both class i.e Service class and SalonReport are provided below.
- A method for printing objects is created in SalonReport class named print().
- Three methods for sorting are created using bubble sort namedbubbleSortService(),bubbleSortPrice(),bubbleSortMinutes().
- In main an array of type Service is created of size 6 named obj.
- Please Note that both class should be in same directory to run the code successfully.
- All important information is given in code itself in form of Comments.
CODE IN JAVA:::
Service.java code::
public class Service{
public String service; /*Description is stored in string datatype named service*/
public double price; /*Price is stored in double data type named variable price*/
public int minutes; /*Minutes is stored in integer data type named minutes*/
/*
* Below constructor takes three parameters String s for description ,
* double p for price and int m for minutes.
* All values are assigned to respective class variables.
*/
Service(String s,double p,int m){
service=s;
price=p;
minutes=m;
}
/*
* Below are methods to return the respective value individually.
* Method names are 1) getSerive(), which returns serive description
* 2) getPrice(), it returns price of the service.
* 3) getMinutes(), it returns minutes.
*/
String getService(){
return service;
}
double getPrice(){
return price;
}
int getMinutes(){
return minutes;
}
}
SalonReport.java code::
import java.util.*;
public class SalonReport{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
/*
* As per the requirement an Array of type Service class is created.
* Array size is 6
*/
Service[] obj=new Service[6];
/*
* Now creating objects and storing data in each object manually.
* I have also created one method to print the objects
*/
obj[0]=new Service(“Cut”,8.00,15);
obj[1]=new Service(“Shampoo”,4.00,10);
obj[2]=new Service(“Manicure”,18.00,30);
obj[3]=new Service(“Style”,48.00,55);
obj[4]=new Service(“Permanent”,18.00,35);
obj[5]=new Service(“Trim”,6.00,5);
int key=-1;
/*
* For sorting purpose I have used three different function for each parameter.
*
*/
do{
System.out.println(“nType any key from below to perform repective operation::”);
System.out.println(“n1::Sort by Service Descriptionn2::Sort by Pricen3::Sort by Time (Minutes)n0::Exit”);
key=sc.nextInt();
switch(key){
case 0:
break;
case 1:
/*Sorts the objects depending on service description*/
bubbleSortService(obj);
print(obj);
break;
case 2:
/*Sorts the objects depending on price from low to high*/
bubbleSortPrice(obj);
print(obj);
break;
case 3:
/*Sorts the objects depending on Minutes from low to high*/
bubbleSortMinutes(obj);
print(obj);
break;
default:
System.out.println(“n Enter valid option…”);
break;
}//switch case ends
System.out.println(“n=======Loop Ends========”);
}while(key!=0); // do-while ends here
}// main function ends here
/* This method sorts the objects based on service description*/
public static void bubbleSortService(Service[] obj) {
int n = obj.length;
Service temp; /*creating temporary object of type service for swapping purpose */
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(obj[j-1].getService().compareTo(obj[j].getService())>0){
//swap elements
temp = obj[j-1];
obj[j-1] = obj[j];
obj[j] = temp;
}
}
}
}
/* This method sorts the objects based on price*/
public static void bubbleSortPrice(Service[] obj) {
int n = obj.length;
Service temp; /*creating temporary object of type service for swapping purpose */
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(obj[j-1].getPrice() > obj[j].getPrice()){
//swap elements
temp = obj[j-1];
obj[j-1] = obj[j];
obj[j] = temp;
}
}
}
}
/* This method sorts the objects based on minutes*/
public static void bubbleSortMinutes(Service[] obj) {
int n = obj.length;
Service temp; /*creating temporary object of type service for swapping purpose */
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(obj[j-1].getMinutes() > obj[j].getMinutes()){
//swap elements
temp = obj[j-1];
obj[j-1] = obj[j];
obj[j] = temp;
}
}
}
}
/* This function just prints the sorted objects in descent format*/
public static void print(Service obj[]){
System.out.printf(“n Service Description Price Time”);
for(int i=0;i<6;i++){
System.out.printf(“n%20s %10s$ %10s “,obj[i].getService(),obj[i].getPrice(),obj[i].getMinutes());
}
}
}
Output::
Test Run ::
C:PRO>javac SalonReport.java
C:PRO>java SalonReport
Type any key from below to perform repective operation::
1::Sort by Service Description
2::Sort by Price
3::Sort by Time (Minutes)
0::Exit
1
Service Description Price Time
Cut 8.0$ 15
Manicure 18.0$ 30
Permanent 18.0$ 35
Shampoo 4.0$ 10
Style 48.0$ 55
Trim 6.0$ 5
=======Loop Ends========
Type any key from below to perform repective operation::
1::Sort by Service Description
2::Sort by Price
3::Sort by Time (Minutes)
0::Exit
2
Service Description Price Time
Shampoo 4.0$ 10
Trim 6.0$ 5
Cut 8.0$ 15
Manicure 18.0$ 30
Permanent 18.0$ 35
Style 48.0$ 55
=======Loop Ends========
Type any key from below to perform repective operation::
1::Sort by Service Description
2::Sort by Price
3::Sort by Time (Minutes)
0::Exit
3
Service Description Price Time
Trim 6.0$ 5
Shampoo 4.0$ 10
Cut 8.0$ 15
Manicure 18.0$ 30
Permanent 18.0$ 35
Style 48.0$ 55
=======Loop Ends========
Type any key from below to perform repective operation::
1::Sort by Service Description
2::Sort by Price
3::Sort by Time (Minutes)
0::Exit
0
=======Loop Ends========
C:PRO>
Image for better understanding of output format::
Sorting by Service description::
Sorting by Price::
Sorting by Minutes::