PLEASE make sure the code compiles. RENTAL CLASS import java.util.Scanner; publi
ID: 3717560 • Letter: P
Question
PLEASE make sure the code compiles.
RENTAL CLASS
import java.util.Scanner;
public class Rental
{
public final static int MINS_PER_HOUR=60;
public final static double perHour=40.00;
private String contractNumber;
private int hours;
private int minsOver;
private double cost;
private String contactPhoneNumber;
private int equipment;
private final static String[]equipments={"Jet Ski","Pontoon Boat","Rowboat","Canoe","Kayak","Beach Chair","Umbrella","Other"};
public Rental(String num, int minutes, String phone, int equipment)
{
setContractNumber(num);
setHoursAndMinutes(minutes);
setContactPhoneNumber(phone);
setEquipment(equipment);
}
public Rental()
{
this("A000", 0, "0000000000",7);
}
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;
}
public void setContactPhoneNumber(String number){
number = number.replaceAll("[^0-9]", "");
contactPhoneNumber = number;
if(number.length()!=10)
contactPhoneNumber = "0000000000";
}
public void setHoursAndMinutes(int minutes)
{
hours = minutes/MINS_PER_HOUR;
minsOver = minutes % MINS_PER_HOUR;
if(minsOver <= perHour)
cost = hours * perHour + minsOver;
else
cost = hours * perHour + perHour;
}
public String getContractNumber()
{
return contractNumber;
}
public int getHours()
{
return hours;
}
public int getMinsOver()
{
return minsOver;
}
public String getContactPhoneNumber()
{
StringBuilder sb = new StringBuilder(contactPhoneNumber);
sb.insert(0,'(');
sb.insert(4,')');
sb.insert(8,'-');
return sb.toString();
}
public double getCost()
{
return cost;
}
public String getEquipment()
{
return equipments[equipment];
}
public void setEquipment(int equipment)
{
if(equipment > 7)
this.equipment = 7;
else
this.equipment = equipment;
}
}
RENTAL DEMO
import java.util.Scanner;
public class RentalDemo
{
public static void main(String[] args)
{
Rental rentals[] = new Rental[8];
for(int i=0; i<8; i++)
{
System.out.println(" Enter details of Rental " + (i+1));
System.out.println("--------------------------------------------------- ");
rentals[i] = new Rental(getContractNumber(), getMinutes(), getContactPhoneNumber(), getEquipment());
}
int choice = 0;
Scanner inputDevice = new Scanner(System.in);
while(choice != 4)
{
showMenu();
choice = Integer.parseInt(inputDevice.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;
}
}
}
System.out.println("Details of the Rentals sorted in ascending order by Contract number: ");
System.out.println();
for (int i = 0; i < 8; i++)
{
System.out.println(" Details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}
System.out.println();
break;
case 2:
for (int i=0; i<8; i++)
{
for (int j=i+1; j<8; j++)
{
if (rentals[i].getCost() > rentals[j].getCost())
{
temp = rentals[i];
rentals[i] = rentals[j];
rentals[j] = temp;
}
}
}
System.out.println();
System.out.println("Details of the Rentals sorted in ascending order by Cost : ");
for (int i=0; i<8; i++)
{
System.out.println(" Details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}
System.out.println();
break;
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;
}
}
}
System.out.println();
System.out.println("Details of the Rentals sorted in ascending order by Equipment Type: ");
for (int i=0; i<8; i++)
{
System.out.println(" Details of Rental " + (i+1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}
System.out.println();
break;
case 4:
System.out.println("GoodBye");
System.exit(0);
default:
System.out.println("Please select the correct choice.");
}
}
}
public static void showMenu()
{
System.out.println("Please choose from the following option to sort rentals: ");
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. Quit");
}
public static String getContractNumber()
{
String num;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter contract number: ");
num = inputDevice.nextLine();
return num;
}
public static String getContactPhoneNumber()
{
String phoneNumber;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter contact phone number: ");
phoneNumber = inputDevice.nextLine();
return phoneNumber;
}
public static int getMinutes()
{
int minutes;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter number of minutes: ");
minutes = inputDevice.nextInt();
return minutes;
}
public static int getEquipment()
{
int equipment;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter the equipment number: ");
equipment = inputDevice.nextInt();
return equipment;
}
public static void displayDetails(Rental r)
{
System.out.println(" Contract #" + r.getContractNumber());
System.out.print("Rental time is " + r.getHours() + " hours and " + r.getMinsOver());
System.out.printf(" minutes, at a rate of $%.2f",r.perHour);
System.out.printf(" the total cost is $%.2f ",r.getCost());
System.out.println("Contact phone Number is :"+r.getContactPhoneNumber());
System.out.println("Equipment Rented: " + r.getEquipment());
}
}
2. a. In Chapter 8, you created a Rental class for Sammy's Seashore Supplies Now extend the class to create a LessonWithRental class. In the extended class, include a new Boolean field that indicates whether a lesson is required or optional for the type of equipment rented. Also include a final array that contains Strings representing the names of the instructors for each of the eight equipment types, and store names that you choose in the array. Create a LessonWithRental constructor that requires arguments for an event number, minutes for the rental, and an integer equipment type. Pass the first two parameters to the Rental constructor, and assign the last parameter to the equipment type. For the first two equipment types (jet ski and pontoon boat), set the Boolean lesson required field to true; otherwise, set it to false. Also include a getInstructor) method that builds and returns a String including the String for the equipment type, a message that indicates whether a lesson is required, and the instructor's name. Save the file as LessonWithRental.java b. In Chapter 9, you created a RentalDemo program for Sammy's Seashore Supplies. The program uses an array of Rental objects and allows the user to sort Rentals in ascending order by contract number, equipment type, or price. Now modify the program to use an array of four LessonWithRental objects. Prompt the user for all values for each object, and then allow the user to continuously sort the LessonWithRental descriptions by contract number, equipment type, or price Save the file as LessonWithRentalDemo.javaExplanation / Answer
public class LessonWithRental extends Rental
{
boolean lesson[];
String instructNames[];
String equipmentType;
Rental rent;
LessonWithRental(String contNum, int rentMinut, String equipType)
{
super(contNum, rentMinut);
equipmentType = equipType;
instructNames = new String[] { "Rosy", "Leander", "Jacob", "Maria",
"LeeSung", "Boston", "Yunishi", "Henry" };
lesson = new boolean[8];
rent = new Rental();
for (int i = 0; i < lesson.length; i++)
{
if (i < 2)
lesson[i] = true;
else
lesson[i] = false;
}
}
String getInstructor(int index)
{
String str = "";
rent.setEquipType(index);
str += "Equipment type: " + rent.getEquipTypeString() + " ";
str += "Required Lessons: " + lesson[index] + " ";
str += "Instructor: " + instructNames[index] + " ";
return str;
}
}
RentalDemo.java
import java.util.Scanner;
public class RentalDemo
{
public static void main(String[] args)
{
String contractNum;
int minutes;
Rental[] rentals = new Rental[3];
int x;
for (x = 0; x < rentals.length; ++x)
{
contractNum = getContractNumber();
minutes = getMinutes();
rentals[x] = new Rental(contractNum, minutes);
rentals[x].setContactPhone(getPhone());
rentals[x].setEquipType(getType());
}
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("Enter equipment type >> ");
eType = input.nextInt();
return eType;
}
public static void displayDetails(Rental r)
{
System.out.println(" Contract #" + r.getContractNumber());
System.out.println("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 $" + r.getPrice());
System.out.println("Contact phone number is: " + r.getContactPhone());
System.out.println("Equipment rented is type #" + r.getEquipType() +
" " + r.getEquipTypeString());
}
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;
}
}
/*************** Rental *****************/
class Rental
{
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];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.