RentalDemo program for sammy\'s seashore supplies. The program uses an array of
ID: 3760401 • Letter: R
Question
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.java Here's the RentalDemo program to be modified:
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;
}
}
Explanation / Answer
import java.util.Scanner;
public class RentalDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String contractName;
int mins;
//getting inputs
//HERE IS THE PROBLEM, IF I TAKE THE FOR LOOP OUT IT WILL WORK FINE BUT ONLY ONCE.
for (int x=0;x<10;x++)
{
System.out.println("Please enter contractName");
contractName = input.nextLine();
System.out.println("Please enter mins");
mins = input.nextInt();
}
//object name
Rental mike = new Rental(contractName, mins);
//to clear buffer
input.nextLine();
//getting inputs
System.out.println("Please enter contractName");
contractName = input.nextLine();
System.out.println("Please enter mins");
mins = input.nextInt();
//object name
Rental luke = new Rental(contractName,mins);
//to clear buffer
input.nextLine();
//getting inputs
System.out.println("Please enter contractName");
contractName = input.nextLine();
System.out.println("Please enter mins");
mins = input.nextInt();
//object name
Rental ihab = new Rental(contractName,mins);
//outputs
System.out.println("Contract Number: " + mike.getContractNumber()+ " Hours: "+mike.getHours()+ " Minutes: " +mike.getMins()+
" Total Price: " +mike.getPrice());
System.out.println(" Contract Number: " + luke.getContractNumber()+ " Hours: "+luke.getHours()+ " Minutes: " +luke.getMins()+
" Total Price: " +luke.getPrice());
System.out.println(" Contract Number: " + ihab.getContractNumber()+ " Hours: "+ihab.getHours()+ " Minutes: " +ihab.getMins()+
" Total Price: " +ihab.getPrice());
//methods
int highest = compare(mike,luke);
int highest2 = compare(ihab,mike);
int highest3 = compare(luke,ihab);
if (highest > highest2 && highest > highest3)
{
System.out.println(" Contract Number:"+ mike.getContractNumber()+ " is the greater");
}
else if (highest2 > highest && highest2 > highest3)
{
System.out.println(" Contract Number:" +ihab.getContractNumber()+ " is the greater");
}
else
{
System.out.println(" Contract Number:" +luke.getContractNumber()+ " is the greater");
}
}
public static int compare(Rental ob1, Rental ob2)
{
int mins;
if ((ob1.getHours() > ob2.getHours()) || ob1.getMins() > ob2.getMins())
{
mins = ob1.getHours()*60 + ob1.getMins();
return mins;
}
else
{
mins = ob1.getHours()*60 + ob1.getMins();
return mins;
}
}
}
package com.javacodegeeks.core.reflection;
public class RentCar {
private int rate;
private String type;
public int price;
public RentCar(int length) {
if (length < 455) {
type = "small";
rate = 35;
} else if ((length >= 455) && (length < 495)) {
type = "mid-sized";
rate = 45;
} else if (length >= 495) {
type = "large";
rate = 55;
}
}
public int getRate() {
return rate;
}
public String getType() {
return type;
}
public void setRate(int rate) {
this.rate = rate;
}
public void setType(String type) {
this.type = type;
}
public void computeRentalCost(int numDays) {
price = numDays * rate;
System.out
.println("The cost of your rental car is " + price + " euros");
}
}
package com.javacodegeeks.core.reflection;
import java.lang.reflect.*;
import java.util.Arrays;
public class ReflectionExample {
public static void main(String[] args) {
// Obtain the class object if we know the name of the class
Class rental = RentCar.class;
try {
// get the absolute name of the class
String rentalClassPackage = rental.getName();
System.out.println("Class Name is: " + rentalClassPackage);
// get the simple name of the class (without package info)
String rentalClassNoPackage = rental.getSimpleName();
System.out.println("Class Name without package is: "
+ rentalClassNoPackage);
// get the package name of the class
Package rentalPackage = rental.getPackage();
System.out.println("Package Name is: " + rentalPackage);
// get all the constructors of the class
Constructor[] constructors = rental.getConstructors();
System.out.println("Constructors are: "
+ Arrays.toString(constructors));
// get constructor with specific argument
Constructor constructor = rental.getConstructor(Integer.TYPE);
// initializing an object of the RentCar class
RentCar rent = (RentCar) constructor.newInstance(455);
// get all methods of the class including declared methods of
// superclasses
// in that case, superclass of RentCar is the class java.lang.Object
Method[] allmethods = rental.getMethods();
System.out.println("Methods are: " + Arrays.toString(allmethods));
for (Method method : allmethods) {
System.out.println("method = " + method.getName());
}
// get all methods declared in the class
// but excludes inherited methods.
Method[] declaredMethods = rental.getDeclaredMethods();
System.out.println("Declared Methods are: "
+ Arrays.toString(declaredMethods));
for (Method dmethod : declaredMethods) {
System.out.println("method = " + dmethod.getName());
}
// get method with specific name and parameters
Method> new Class[] { Integer.TYPE });
System.out.println("Method is: " + oneMethod);
// call computeRentalCost method with parameter int
oneMethod.invoke(rent, 4);
// get all the parameters of computeRentalCost
Class[] parameterTypes = oneMethod.getParameterTypes();
System.out.println("Parameter types of computeRentalCost() are: "
+ Arrays.toString(parameterTypes));
// get the return type of computeRentalCost
Class returnType = oneMethod.getReturnType();
System.out.println("Return type is: " + returnType);
// gets all the public member fields of the class RentCar
Field[] fields = rental.getFields();
System.out.println("Public Fields are: ");
for (Field oneField : fields) {
// get public field name
Field field = rental.getField(oneField.getName());
String fieldname = field.getName();
System.out.println("Fieldname is: " + fieldname);
// get public field type
Object fieldType = field.getType();
System.out.println("Type of field " + fieldname + " is: "
+ fieldType);
// get public field value
Object value = field.get(rent);
System.out.println("Value of field " + fieldname + " is: "
+ value);
}
// How to access private member fields of the class
// getDeclaredField() returns the private field
Field privateField = RentCar.class.getDeclaredField("type");
String name = privateField.getName();
System.out.println("One private Fieldname is: " + name);
// makes this private field instance accessible
// for reflection use only, not normal code
privateField.setAccessible(true);
// get the value of this private field
String fieldValue = (String) privateField.get(rent);
System.out.println("fieldValue = " + fieldValue);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}