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.
Please include the modified changes to exisitng rental class. Giving u code for additional requirement.
COPY TO CODE:
package staticClass2;
//rental class with additional fields
class Rental {
private String phoneNumber;
private String contractNumber;
public String getContractNumber() {
return contractNumber;
}
//set contract number
public void setContractNumber(String contractNumber) {
//get the contract number and upper case it
contractNumber = contractNumber.toUpperCase();
//if it is of 4 digit
if(contractNumber.length()==4){
//then check if first character is alphabet and remaining digit
String firstChar = contractNumber.charAt(0)+"";
String remaining = contractNumber.substring(1);
if((firstChar.matches("[a-zA-Z]+") && remaining.matches("^\\d+$"))){
this.contractNumber = contractNumber;
}else{
this.contractNumber="A000";
}
}else{
this.contractNumber="A000";
}
}
public String getPhoneNumber() {
return "("+phoneNumber.substring(0,3) +") "+phoneNumber.substring(3,6)+"-"+phoneNumber.substring(6);
}
public void setPhoneNumber(String phoneNumber) {
//replace all non numeric from phone number
phoneNumber = phoneNumber.replaceAll("[^0-9]", "");
//now check the length
if(phoneNumber.length()==10){
this.phoneNumber= phoneNumber;
}else{
this.phoneNumber = "0000000000";
}
}
}
public class RentalDemo {
public static void main(String[] args) {
Rental rent = new Rental() ;
System.out.println("First details");
rent.setContractNumber("W111");
rent.setPhoneNumber("1234567890");
System.out.println(rent.getPhoneNumber());
System.out.println(rent.getContractNumber());
System.out.println("\n\n\nSecond details");
Rental rent2 = new Rental();
rent2.setContractNumber("2343");
rent2.setPhoneNumber("3439 09");
System.out.println(rent2.getPhoneNumber());
System.out.println(rent2.getContractNumber());
}
}
OUTPUT: