Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Table 1 shows the various services offered by a hair salon, including its prices

ID: 3707282 • Letter: T

Question

Table 1 shows the various services offered by a hair salon, including its prices and times required: 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 Table 1 Salon services, prices, and times 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 each returns one of the data field’s values. Save the class as Service.java. Write an application named SalonReport that contains an array to hold six Service objects, and fill it with the data from Table 1. Include methods to sort the array 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.

Explanation / Answer

//Code to copy

//SalonReport.java
import java.util.*;
public class SalonReport
{
   public static void main(String[] args)
   {
       //create an array of size 6 of type Service
       Service[] services=new Service[6];

       // Create the services objects.
       services[0]=new Service("Cut", 8.00F, 15);
       services[1]=new Service("Shampoo", 4.00F, 10);
       services[2]=new Service("Manicure", 18.00F, 30);
       services[3]=new Service("Style", 48.00F, 55);
       services[4]=new Service("Permanent", 18.00F, 35);
       services[5]=new Service("Trim", 6.00F, 5);

       boolean quit = false;
       String input = "";
       int choice = 0;
       Scanner scan=new Scanner(System.in);
       do
       {
           //prompt user to enter choice of sorting
           System.out.println("1) description ");
           System.out.println("2) price");
           System.out.println("3) time ");
           System.out.println("4) quit");
           input=scan.nextLine();
           // Attempt to convert the input into an integer.
           choice = Integer.parseInt(input);
           // Sort the services according to the user's entry.
           switch (choice)
           {
           case 1:
               sortByDescription(services);
               printServices(services);
               break;
               // By price.
           case 2:
               sortByPrice(services);
               printServices(services);
               break;
               // By duration in minutes.
           case 3:
               sortByDuration(services);
               printServices(services);
               break;

               // Quit.
           case 4:
               quit = true;
               break;
           }
       }while (!quit);
   }

   public static void printServices(Service services[])
   {
       for (int i = 0; i < services.length; i++) {
           System.out.printf("%-20s%-10.2f%-10d ",
                   services[i].getDescription(),
                   services[i].getPrice(),                  
                   services[i].getDuration());
       }
   }

   private static void sortByDescription(Service[] services)
   {
       int n = services.length;
       for (int i = 0; i < n-1; i++)
       {
           for (int j = 0; j < n-i-1; j++)
           {
               if (services[j].getDescription().compareTo(services[j+1].getDescription())>0)
               {
                   // swap price at j and j+1 index
                   double tempprice= services[j].getPrice();
                   services[j].setPrice(services[j+1].getPrice());
                   services[j+1].setPrice(tempprice);

                   // swap description at j and j+1 index                  
                   String tempdesc = services[j].getDescription();
                   services[j].setDescription(services[j+1].getDescription());
                   services[j+1].setDescription(tempdesc);

                   // swap duration at j and j+1 index
                   int tempduration = services[j].getDuration();
                   services[j].setDuration(services[j+1].getDuration());
                   services[j+1].setDuration(tempduration);
               }
           }
       }

   }

   private static void sortByPrice(Service[] services)
   {

       int n = services.length;
       for (int i = 0; i < n-1; i++)
       {
           for (int j = 0; j < n-i-1; j++)
           {
               if ((int)(services[j].getPrice() - services[j+1].getPrice())>0)
               {
                   // swap price at j and j+1 index
                   double tempprice= services[j].getPrice();
                   services[j].setPrice(services[j+1].getPrice());
                   services[j+1].setPrice(tempprice);

                   // swap description at j and j+1 index                  
                   String tempdesc = services[j].getDescription();
                   services[j].setDescription(services[j+1].getDescription());
                   services[j+1].setDescription(tempdesc);

                   // swap duration at j and j+1 index
                   int tempduration = services[j].getDuration();
                   services[j].setDuration(services[j+1].getDuration());
                   services[j+1].setDuration(tempduration);


               }
           }
       }
   }
   private static void sortByDuration(Service[] services)
   {

       int n = services.length;
       for (int i = 0; i < n-1; i++)
       {
           for (int j = 0; j < n-i-1; j++)
           {
               if ((services[j].getDuration() - services[j+1].getDuration())>0)
               {
                   // swap price at j and j+1 index
                   double tempprice= services[j].getPrice();
                   services[j].setPrice(services[j+1].getPrice());
                   services[j+1].setPrice(tempprice);

                   // swap description at j and j+1 index                  
                   String tempdesc = services[j].getDescription();
                   services[j].setDescription(services[j+1].getDescription());
                   services[j+1].setDescription(tempdesc);

                   // swap duration at j and j+1 index
                   int tempduration = services[j].getDuration();
                   services[j].setDuration(services[j+1].getDuration());
                   services[j+1].setDuration(tempduration);
               }
           }
       }

   }
}

------------------------------------------------------------------------------------------------------------------------------

//Service.java
public class Service
{
   //declare instance variables
   private String description;
   private double price;
   private int duration;

   /*Constructor */
   public Service(String d,
           float p, int time)
   {
       this.description = d;
       this.price = p;
       this.duration = time;
   }
   /*Returns the description*/
   public String getDescription()
   {
       return this.description;
   }
   /*Returns the price*/
   public double getPrice()
   {
       return price;
   }
   /*Returns the duration*/
   public int getDuration()
   {
       return this.duration;
   }

   /*Returns the description*/
   public void setDescription(String description)
   {
       this.description=description;
   }
   /*Returns the price*/
   public void setPrice(double price)
   {
       this.price=price;
   }
   /*Returns the duration*/
   public void setDuration(int duration)
   {
       this.duration=duration;
   }
}//end of Service class

------------------------------------------------------------------------------------------------------------------------------

Sample Output:

1) description
2) price
3) time
4) quit
1
Cut 8.00 15
Manicure 18.00 30
Permanent 18.00 35
Shampoo 4.00 10
Style 48.00 55
Trim 6.00 5   
1) description
2) price
3) time
4) quit
2
Shampoo 4.00 10
Trim 6.00 5   
Cut 8.00 15
Manicure 18.00 30
Permanent 18.00 35
Style 48.00 55
1) description
2) price
3) time
4) quit
3
Trim 6.00 5   
Shampoo 4.00 10
Cut 8.00 15
Manicure 18.00 30
Permanent 18.00 35
Style 48.00 55
1) description
2) price
3) time
4) quit
4