In: Computer Science
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.
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());
}
}