Question

In: Computer Science

Sammy's Seashore Supplies rents beach equipment to tourists. Inprevious chapters, you have developed a class...

Sammy's Seashore Supplies rents beach equipment to tourists. In previous chapters, you have developed a class that holds equipment rental information and an application that tests the methods using four objects of the class. Now modify the Rental and RentalDemo classes as follows:

  • Modify the method that sets the contract number in the Rental class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits, then the contract number is forced to A000. If the initial letter in the contract number is not uppercase, force it to be so.

  • Add a contact phone number field to the Rental class.

  • Add a set method for the contact phone number field in the Rental class. Whether the user enters all digits or any combination of digits, spaces, dashes, dots, or parentheses for a phone number, store it as all digits. For example, if the user enters (920) 872-9182, store the phone number as 9208729182. If the user enters a number with fewer or more than 10 digits, store the number as 0000000000.

  • Add a get method for the phone number field. The get method returns the phone number as a String constructed as follows: parentheses surround a three-digit area code, followed by a space, followed by the three-digit phone exchange, followed by a hyphen, followed by the last four digits of the phone number.

  • Modify the RentalDemo program so that besides the contract number and minutes, the program also prompts the user for and retrieves a contact phone number for each of the sample objects. Display the phone number along with the other Rental details. Test the RentalDemo application to make sure it works correctly with valid and invalid contract and phone numbers.

Save the files as Rental.java and RentalDemo.java.

Solutions

Expert Solution

Rental.java code

public class Rental 
{   
        //Fields        
        public static final int MINUTES_IN_HOUR = 60;
        public static final double HOURLY_RATE = 40.00;
        private double lessonFee = 27;
        public static final int CONTRACT_NUM_LENGTH = 4;
        protected static String contractNumber;
        int equipment;
        private int hours;
        private int extraMinutes;
        private double price;
        private String contactPhoneNumber;         
        private static final String[] equipments = {"Personal Watercraft","Pontoon Boat","Rowboat","Canoe","Kayak","Beach Chair","Umbrella","Other"};

        //Methods         
        public Rental(String num, int minutes) 
          {
               setContractNumber(num);
               setHoursAndMinutes(minutes);
               setContactPhoneNumber(contactPhoneNumber);
           }
           //String to format contract number, ?, phone number, contract length. I can't get the error to go away unless I leave it a stub.
           public Rental(String string, int i, String string2, int j) 
           {
           }

           //Set Method to set the contract number.
           public void setContractNumber(String num) 
           {
                   if(num.length()!=4 || !num.substring(1).matches("[0-9]+") || !num.substring(0,1).matches("[a-zA-Z]+"))
                 num="A000";
               if(!num.substring(0,1).matches("[A-Z]+"))
                 num=num.toUpperCase();
               contractNumber=num;
           }

           //Set Method to set the contact phone number.
           public void setContactPhoneNumber(String number)
           {
               number=number.replaceAll("[^0-9]", "");
               contactPhoneNumber=number;
                if(number.length()!=10)
               contactPhoneNumber="000-000-0000";
           }

           //Set Method to set the hours and minutes.
           public void setHoursAndMinutes(int minutes) 
           {
               hours=minutes/MINUTES_IN_HOUR;
               extraMinutes=minutes%MINUTES_IN_HOUR;
                if(extraMinutes<=HOURLY_RATE)
               price=hours*HOURLY_RATE+extraMinutes;
                else
               price=hours*HOURLY_RATE+HOURLY_RATE;
           }
           
           //Set method to set equipment rental.
           public void setEquipment(int equipment) 
           {
               if(equipment>7)
                   this.equipment=7;
               else
                   this.equipment=equipment;
           }

           //Get method to get the contract number.
           public static String getContractNumber() 
           {
               return contractNumber;
           }

           //Get method to get the hours of rental.
           public int getHours() 
           {
               return hours;
           }

           //Get method to get the extra minutes of the rental.
           public int getExtraMinutes() 
           {
               return extraMinutes;
           }

           //Get method to get the renters phone number.
           public String getContactPhoneNumber() 
           {
               StringBuilder sb = new StringBuilder(contactPhoneNumber);
               sb.insert(0,'(');
               sb.insert(4,')');
               sb.insert(8,'-');
               return sb.toString();
           }
         
           //Get method to get the rental price.
           public double getPrice() 
           {
               return price+lessonFee;
           }
           
           //Get method to get the equipment rented.
           public String getEquipment() 
           {
               return equipments[equipment];
           }
}

RentalDemo.java code

import java.util.Scanner;
public class RentalDemo 
{
        public static void main(String[] args) 
        {
                //The array to return 8 rentals.
                Rental rentals[] = new Rental[8];
                        for (int i = 0; i < 8; i++) 
                        {
                                System.out.println("\nRental " + (i + 1));
                                rentals[i] = new Rental(getContractNumber(), getMinutes(), getContactPhoneNumber(), getEquipment());
                        }

                int choice = 0;
                        Scanner scan = new Scanner(System.in);
                while(choice != 4) 
                {
                        showMenu();
                choice = Integer.parseInt(scan.next());
                Rental temp = null;
                        switch(choice) 
                        {
                                case 1:
                                for (int i = 0; i < 8; i++)
                        {
                                for (int j = i + 1; j < 8; j++)
                                {
                                        if (rentals[i].getContractNumber().compareTo(rentals[j].getContractNumber()) > 0)
                                        {
                                                temp = rentals[i];
                                                rentals[i] = rentals[j];
                                                rentals[j] = temp;
                                        }
                                }
                        }       

                //Sorts  by Contract Number.
                System.out.println("Rentals Sorted In Ascending Order By Contract Number: ");
                System.out.println();
                        for (int i = 0; i < 8; i++) 
                        {
                                System.out.println("\nRental " + (i + 1));
                                displayDetails(rentals[i]);
                        }

                System.out.println();
                case 2:
                for (int i = 0; i < 8; i++)
                {
                        for (int j = i + 1; j < 8; j++)
                        {
                                if (rentals[i].getPrice() > rentals[j].getPrice())
                                {
                                        temp = rentals[i];
                                        rentals[i] = rentals[j];
                                        rentals[j] = temp;
                                }
                        }
                }

                //Sorts by Price.
                System.out.println();
                System.out.println("Rentals Sorted In Ascending Order By Price: ");
                        for (int i = 0; i < 8; i++) 
                        {
                                System.out.println("\nRental " + (i + 1));
                                displayDetails(rentals[i]);
                        }

                System.out.println();
                case 3:
                for (int i = 0; i < 8; i++)
                {
                        for (int j = i + 1; j < 8; j++)
                        {
                                if (rentals[i].getEquipment().compareTo(rentals[j].getEquipment()) > 0)
                                {
                                        temp = rentals[i];
                                        rentals[i] = rentals[j];
                                        rentals[j] = temp;
                                }
                        }
                }

                //Sorts by Equipment Type.
                System.out.println();
                System.out.println("Rentals Sorted In Ascending Order By Equipment Type: ");
                        for (int i = 0; i < 8; i++)
                        {
                                System.out.println("\nRental " + (i + 1));
                                displayDetails(rentals[i]);
                        }
                
                System.out.println();
                        case 4:
                System.out.println("Goodbye!!");
                System.exit(0);
                        default:
                System.out.println("Select The Correct Choice!");
                        }
                        }
                        }

                //Menu Method.
                public static void showMenu() 
                {
                        System.out.println("Please Select The Option: ");
                        System.out.println("1. Sort By Contact number.");
                        System.out.println("2. Sort By Price.");
                        System.out.println("3. Sort By Equipment Type.");
                        System.out.println("4. Exit");
                }

                //Get Method.
                public static String getContractNumber() 
                {
                        String num;
                        Scanner input = new Scanner(System.in);
                        System.out.print("Enter Contract Number: ");
                        num = input.nextLine();
                        return num;
                }

                //Get Method.
                public static String getContactPhoneNumber() 
                {
                        String phoneNumber;
                        Scanner input = new Scanner(System.in);
                        System.out.print("Enter Contact Phone Number: ");
                        phoneNumber = input.nextLine();
                        return phoneNumber;
                }

                //Get Method.
                public static int getMinutes() 
                {
                        int minutes;
                        Scanner input = new Scanner(System.in);
                        System.out.print("Enter minutes: ");
                        minutes = input.nextInt();
                        return minutes;
                }

                //Get Method.
                public static int getEquipment()
                {
                        int equipment;
                        Scanner input = new Scanner(System.in);
                        System.out.print("Enter Equipment Type: ");
                        equipment = input.nextInt();
                        return equipment;
                }

                //Method to display details.
                public static void displayDetails(Rental r) 
                {
                        System.out.println("\nThe Contract Number Is: " + r.getContractNumber());
                        System.out.print("For a rental time of " + r.getHours() + " hours & " + r.getExtraMinutes());
                        System.out.printf(" minutes, at a rate of $%.2f,", r.HOURLY_RATE);                      
                        System.out.printf(" the total price is $%.2f\n", r.getPrice());
                        System.out.println("Contact Phone Number: " + r.getContactPhoneNumber());
                        System.out.println("The equipment Type Rented Is: " + r.getEquipment());
                }
}

Related Solutions

Sammy’s Seashore Supplies rents beach equipment to tourists. In previous chapters, you have developed a class...
Sammy’s Seashore Supplies rents beach equipment to tourists. In previous chapters, you have developed a class that holds equipment rental information and an application that tests the methods using four objects of the class. Now modify the Rental and RentalDemo classes as follows: Modify the method that sets the contract number in the Rental class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits, then the...
Carly's Catering provides meals for parties and special events. In previous chapters, you developed a class...
Carly's Catering provides meals for parties and special events. In previous chapters, you developed a class that holds catering event information and an application that tests the methods using three objects of the class. Now modify the EventDemo class to do the following: Continuously prompt for the number of guests for each Event until the value falls between 5 and 100 inclusive. For one of the Event objects, create a loop that displays Please come to my event! as many...
Carly's Catering provides meals for parties and special events. In previous chapters, you have developed a...
Carly's Catering provides meals for parties and special events. In previous chapters, you have developed a class that holds catering event information and an application that tests the methods using four objects of the class. Now modify the Event and EventDemo classes as follows: Modify the method that sets the event number in the Event class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits, then...
Carly’s Catering provides meals for parties and special events. In previous chapters, you have developed a...
Carly’s Catering provides meals for parties and special events. In previous chapters, you have developed a class that holds catering event information and an application that tests the methods using four objects of the class. Now modify the Event and EventDemo classes as follows: • Modify the method that sets the event number in the Event class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits,...
Carly’s Catering provides meals for parties and special events. In previous chapters, you have developed a...
Carly’s Catering provides meals for parties and special events. In previous chapters, you have developed a class that holds catering event information and an application that tests the methods using four objects of the class. Now modify the Event and EventDemo classes as follows: • Modify the method that sets the event number in the Event class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits,...
Now that we have completed Chapters 1 through 6 and have developed an understanding of assets,...
Now that we have completed Chapters 1 through 6 and have developed an understanding of assets, both short-term and long-term, apply your understanding of what you have learned to the following questions. Part 1 – Current and long-term assets From the perspective of a potential long-term investor, describe what you might look for when examining the assets of a corporation. Include in your answer an understanding of how short-term assets and long-term operating assets are different and what role they...
I. You have studied the chapters on unemployment and business cycles. Please review those chapters before...
I. You have studied the chapters on unemployment and business cycles. Please review those chapters before you answer this question a) Find the time series data (quarterly or monthly) on the unemployment rate, inflation rate and real GDP growth in the U.S. from 1980 to 2005, and discuss whether the Okun’s Law is valid or not. Then, discuss whether the Phillips curve exists in the U.S. economy( you have to report your data source and or the website). b) Which...
When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach
  When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach, perhaps you would be horrified by the high price you would have to pay for a bottle of soft drink such as Coca Cola or Pepsi Cola or wine or even bottled water. Perhaps you begin to ponder why the same commodity that you can get at a supermarket at one tenth the hotel...
When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach
  When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach, perhaps you would be horrified by the high price you would have to pay for a bottle of soft drink such as Coca Cola or Pepsi Cola or wine or even bottled water. Perhaps you begin to ponder why the same commodity that you can get at a supermarket at one tenth the hotel...
Describe an issue you have had with an employer that is mentioned in the chapters. It...
Describe an issue you have had with an employer that is mentioned in the chapters. It could be lack of due process when terminated, discrimination, lack of equal pay, etc. If it is too personal or you do not have an example, find one on the web or ask a friend. "Employee SH Workplace Issues" and "Employee SH Diversity and Discrimination" For example: I wanted to work at the cannery when I was in college and was told I couldn't...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT