Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. In previous chapters, you have created several classes for Sammys Seashore Supplies. Now, Sammy has decided to restructure his rates to include different fees

a. In previous chapters, you have created several classes for Sammys Seashore Supplies. Now, Sammy has decided to restructure his rates to include different fees for equipment types in addition to the fees based on rental length, and to charge for required lessons for using certain equipment. Create an abstract class named Equipment that holds fields for a numeric equipment type, a String equipment name, and a fee for renting the equipment. Include a final array that holds the equipment namesjet ski, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Also include a final array that includes the surcharges for each equipment type$50, $40, $15, $12, $10, $2, $1, and $0, respectively. Include a constructor that requires an equipment type and sets the field to the type unless it is out of range, in which case the type is set to the other code. Include get and set methods for each field and include an abstract method that returns a String explaining the lesson policy for the type of equipment. Save the file as Equipment.java. b. CreatetwoclassesthatextendEquipmentEquipmentWithoutLessonand EquipmentWithLesson. The constructor for each class requires that the equipment type be in rangethat is, jet skis, pontoon boats, rowboats, canoes, and kayaks are EquipmentWithLesson objects, but other equipment types are not. In both subclasses, the constructors set the equipment type to other if it is not in range. The constructors also set the equipment fee, as described in part 2a. Each subclass also includes a method that returns a message indicating whether a lesson is required, and the cost ($27) if it is. Save the files as EquipmentWithoutLesson.java and EquipmentWithLesson.java.

c. InChapter8,you created a Rental class. Now, modify it to contain an Equipment data field and an additional price field that holds a base price before equipment fees are added. Remove the array of equipment Strings from the Rental class as well as the method that returns an equipment string. Modify the Rental constructor so that it requires three parameters: contract number, minutes for the rental, and an equipment type. The method that sets the hours and minutes now sets a base price before equipment fees are included. Within the constructor, set the contract number and time as before, but add statements to create either an EquipmentWithLesson object or an EquipmentWithoutLesson object, and assign it to the Equipment data field. Assign the sum of the base price (based on time) and the equipment fee (based on the type of equipment) to the price field. Save the file as Rental.java. d. InChapter8,youcreatedaRentalDemoclassthatdisplaysdetailsforfourRental objects. Modify the class as necessary to use the revised Rental class that contains an Equipment field. Be sure to modify the method that displays details for the Rental to include all the pertinent data for the equipment. Figure 11-36 shows the last part of the output from a typical execution. Save the file as RentalDemo.java.

public class Rental implements Comparable {

public static final int MINUTES_IN_HOUR = 60;

public static final int HOUR_RATE = 40;

public static final int CONTRACT_NUM_LENGTH = 4;

public static final String[] EQUIP_TYPES = {

"jet ski",

"pontoon boat",

"rowboat",

"canoe",

"kayak",

"beach chair",

"umbrella",

"other"

};

private String contractNumber;

private int hours;

private int extraMinutes;

private double price;

private String contactPhone;

private int equipType;

public Rental(String num, int minutes) {

setContractNumber(num);

setHoursAndMinutes(minutes);

}

public Rental() {

this("A000", 0);

}

public void setContractNumber(String num) {

boolean numOk = true;

if (num.length() != CONTRACT_NUM_LENGTH || !Character.isLetter(num.charAt(0)) || !Character.isDigit(num.charAt(1)) || !Character.isDigit(num.charAt(2)) || !Character.isDigit(num.charAt(3))) contractNumber = "A000";

else contractNumber = num.toUpperCase();

}

public void setHoursAndMinutes(int minutes) {

hours = minutes / MINUTES_IN_HOUR;

extraMinutes = minutes % MINUTES_IN_HOUR;

if (extraMinutes <= HOUR_RATE) price = hours * HOUR_RATE + extraMinutes;

else price = hours * HOUR_RATE + HOUR_RATE;

}

public String getContractNumber() {

return contractNumber;

}

public int getHours() {

return hours;

}

public int getExtraMinutes() {

return extraMinutes;

}

public double getPrice() {

return price;

}

public String getContactPhone() {

String phone;

phone = "(" + contactPhone.substring(0, 3) + ") " + contactPhone.substring(3, 6) + "-" + contactPhone.substring(6, 10);

return phone;

}

public void setContactPhone(String phone) {

final int VALID_LEN = 10;

final String INVALID_PHONE = "0000000000";

contactPhone = "";

int len = phone.length();

for (int x = 0; x < len; ++x) {

if (Character.isDigit(phone.charAt(x))) contactPhone += phone.charAt(x);

}

if (contactPhone.length() != VALID_LEN) contactPhone = INVALID_PHONE;

}

public void setEquipType(int eType) {

if (eType < EQUIP_TYPES.length) equipType = eType;

else equipType = EQUIP_TYPES.length - 1;

}

public int getEquipType() {

return equipType;

}

public String getEquipTypeString() {

return EQUIP_TYPES[equipType];

}

@Override

public int compareTo(Rental o) {

if(this.contractNumber.compareTo(o.contractNumber) > 0 )

return 1;

else if(this.contractNumber.compareTo(o.contractNumber) < 0 )

return -1;

else

return 0;

}

}

public class LessonWithRental extends Rental{

boolean lessonReq;

final String[] instructorNames = {"Reagan","Sara","Parker","Taylor","Cindy","Pam","Steve","Izzy"};

public LessonWithRental(String eventnumber,int minutes,int equptype){

super(eventnumber,minutes);

this.setEquipType(equptype);

if(equptype == 0 || equptype == 1){

lessonReq = true;

}else

lessonReq = false;

}

public String getInstructor(){

String output="";

output = "Equipment Type: " + this.EQUIP_TYPES[this.getEquipType()] + (lessonReq ? ": This equipment requires a lesson and the instructor's name is: "

+ " " + instructorNames[this.getEquipType()] : ": This equipment does not require an instructor, but if you would like a lesson the instructor's name is: " + " " + instructorNames[this.getEquipType()]);

return output;

}

}

import java.util.Scanner;

public class LessonWithRentalDemo {

public static void main(String[] args) {

String contractNum;

int minutes;

LessonWithRental[] rentals = new LessonWithRental[3];

int x;

for (x = 0; x < rentals.length; ++x) {

contractNum = getContractNumber();

minutes = getMinutes();

int eqipType = getType();

String phone = getPhone();

rentals[x] = new LessonWithRental(contractNum, minutes, eqipType);

rentals[x].setContactPhone(phone);

}

for (x = 0; x < rentals.length; ++x) displayDetails(rentals[x]);

}

public static String getContractNumber() {

String num;

Scanner input = new Scanner(System. in );

System.out.print(" Enter contract number >> ");

num = input.nextLine();

return num;

}

public static int getMinutes() {

int minutes;

final int LOW_MIN = 60;

final int HIGH_MIN = 7200;

Scanner input = new Scanner(System. in );

System.out.print("Enter minutes >> ");

minutes = input.nextInt();

while (minutes < LOW_MIN || minutes > HIGH_MIN) {

System.out.println("Time must be between " + LOW_MIN + " minutes and " + HIGH_MIN + " minutes");

System.out.print("Please reenter minutes >> ");

minutes = input.nextInt();

}

return minutes;

}

public static int getType() {

int eType;

Scanner input = new Scanner(System. in );

System.out.println("Equipment types:");

for (int x = 0; x < Rental.EQUIP_TYPES.length; ++x) System.out.println(" " + x + " " + Rental.EQUIP_TYPES[x]);

System.out.print("Please enter equipment type >> ");

eType = input.nextInt();

return eType;

}

public static void displayDetails(LessonWithRental r) {

System.out.println(" Contract #" + r.getContractNumber());

System.out.printf("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes() + " minutes, at a rate of $" + r.HOUR_RATE + " per hour and $1 per minute, the price is $%.2f ", r.getPrice());

System.out.println("Contact phone number is: " + r.getContactPhone());

System.out.println("Equipment rented is type #" + r.getEquipType() + " " + r.getEquipTypeString());

System.out.println(r.getInstructor());

}

public static Rental getLongerRental(Rental r1, Rental r2) {

Rental longer = new Rental();

int minutes1;

int minutes2;

minutes1 = r1.getHours() * Rental.MINUTES_IN_HOUR + r1.getExtraMinutes();

minutes2 = r2.getHours() * Rental.MINUTES_IN_HOUR + r2.getExtraMinutes();

if (minutes1 >= minutes2) longer = r1;

else longer = r2;

return longer;

}

public static String getPhone() {

String phone;

Scanner input = new Scanner(System. in );

System.out.print("Enter contact phone number >> ");

phone = input.nextLine();

return phone;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions