***UPDATE: I previously posted wrong Rental.java and RentalDemo.java code, check
ID: 3740845 • Letter: #
Question
***UPDATE: I previously posted wrong Rental.java and RentalDemo.java code, check below for correct code ***
In the last chapter, you modified the RentalDemo program for Sammy's Seashore Supplies to accept and display data for an array of three Rental objects. Now, modify the program to use an array of eight Rental objects. Prompt the user to choose an option to sort Rentals in ascending order by contract number, price, or equipment type. Display the sorted list, and continue to prompt the user for sorting options until the user enters a sentinel value. Save the file as RentalDemo.java.
Java Programming 8th Edition
Chapter 9 Case Problem 2
Not sure if you need it, but I've included my Rental.java file
*** Rental.java ***
import java.util.Scanner;
class Rental
{
final int MINUTES_PER_HOUR = 60;
final int HOURLY_RATE = 40;
final String equipments[]={"jet ski","pontoon boat","rowboat","canoe","kayak","beach chair","umbrella","other"};
private String contractNumber;
private int hours;
private int minutes;
private int totalCost;
private String phoneNumber;
private int equipmentType;
public Rental()
{
}
public Rental(String idInput, int minInput)
{
this.setContractNumber(idInput);
this.setHoursAndMinutes(minInput);
}
public void setHoursAndMinutes(int minInput)
{
this.hours = minInput / 60;
this.minutes = minInput - (60 * this.hours);
this.totalCost = (40 * this.hours) + this.minutes;
//System.out.println("For the rental of " + this.hours + " hours " +
// this.minutes + " minutes, the price is $" + this.totalCost + ".");
}
public void setContractNumber(String idInput){
String firstletter = idInput.substring(0, 1).toUpperCase();
String newID = firstletter.concat(idInput.substring(1));
this.contractNumber = idInput;
}
public void setPhoneNumber(String phoneInput)
{
int inputLength = phoneInput.length();
String cleanInput = "";
for (int val = 0; val < inputLength; ++val)
{
char inChar = phoneInput.charAt(val);
//System.out.println(inChar);
if (Character.isDigit(inChar))
{
cleanInput = cleanInput.concat(Character.toString(inChar));
//System.out.println(Character.toString(inChar));
//System.out.println(cleanInput);
}
}
this.phoneNumber = cleanInput;
}
public void setEquipmentType(int type)
{
if(type>=equipments.length)
{
equipmentType=equipments.length-1;
}
else
{
equipmentType=type;
}
}
public String getcontractNumber()
{
return contractNumber;
}
public int getHours()
{
return hours;
}
public int getMinutes()
{
return minutes;
}
public int getTotalCost()
{
return totalCost;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public String getEquipmentType()
{
return equipments[equipmentType];
}
}
*** RentalDemo.java ***
public class RentalDemo
{
public static void main(String[] args)
{
Rental rentals[]=new Rental[3];
Rental contract1 = new Rental();
contract1.setContractNumber("a364");
contract1.setHoursAndMinutes(200);
contract1.setPhoneNumber("(240)-583-9274");
contract1.setEquipmentType(9);
Rental contract2 = new Rental();
contract2.setContractNumber("a365");
contract2.setHoursAndMinutes(300);
contract2.setPhoneNumber("(240)-583-9275");
contract2.setEquipmentType(2);
Rental contract3 = new Rental();
contract3.setContractNumber("A001");
contract3.setHoursAndMinutes(125);
contract3.setPhoneNumber("(240)-583-9274");
contract3.setEquipmentType(3);
rentals[0]=contract1;
rentals[1]=contract2;
rentals[2]=contract3;
for(int i=0;i
{
System.out.println("Contract Number: " + rentals[i].getcontractNumber());
System.out.println("Hours: " + rentals[i].getHours());
System.out.println("Minutes: " + rentals[i].getMinutes());
System.out.println("totalCost: " + rentals[i].getTotalCost());
System.out.println("equipment type: " + rentals[i].getEquipmentType());
System.out.println();
}
}
}
Explanation / Answer
Hi friend, you have not posted Rental class. I have implememted my own Rental classa and all three sorting.
import java.util.Scanner;
class Rental {
String contact;
String equipmentType;
double price;
public Rental(String contact, String equipmentType, double price) {
super();
this.contact = contact;
this.equipmentType = equipmentType;
this.price = price;
}
}
public class RentalDemo {
/* Prints the array */
static void printArray(Rental arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.println(arr[i].contact+" "+arr[i].equipmentType + " "+arr[i].price);
System.out.println();
}
static void sortByContact(Rental arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j].contact.compareToIgnoreCase(arr[j+1].contact) > 0)
{
// swap temp and arr[i]
Rental temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
static void sortByequipmentType(Rental arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j].equipmentType.compareToIgnoreCase(arr[j+1].equipmentType) > 0)
{
// swap temp and arr[i]
Rental temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
static void sortByPrice(Rental arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j].price > arr[j+1].price)
{
// swap temp and arr[i]
Rental temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Rental[] arr = {
new Rental("A", "FD", 43),
new Rental("F", "CD", 23),
new Rental("B", "Tr", 33),
new Rental("X", "RX", 993),
new Rental("N", "ZZ", 233),
new Rental("P", "LV", 893),
new Rental("S", "CC", 213),
new Rental("J", "MK", 773)};
while(true) {
System.out.println("Sort By: 1.Contact, 2. Equipment, 3.Price, 4. exit");
int op = sc.nextInt();
switch(op) {
case 1:
sortByContact(arr);
printArray(arr);
break;
case 2:
sortByequipmentType(arr);
printArray(arr);
break;
case 3:
sortByPrice(arr);
printArray(arr);
break;
case 4:
break;
default:
System.out.println("Invalid option");
}
if(op == 4)
break;
System.out.println();
}
sc.close();
}
}
/*
Sample run:
Sort By: 1.Contact, 2. Equipment, 3.Price, 4. exit
1
A FD 43.0
B Tr 33.0
F CD 23.0
J MK 773.0
N ZZ 233.0
P LV 893.0
S CC 213.0
X RX 993.0
Sort By: 1.Contact, 2. Equipment, 3.Price, 4. exit
2
S CC 213.0
F CD 23.0
A FD 43.0
P LV 893.0
J MK 773.0
X RX 993.0
B Tr 33.0
N ZZ 233.0
Sort By: 1.Contact, 2. Equipment, 3.Price, 4. exit
3
F CD 23.0
B Tr 33.0
A FD 43.0
S CC 213.0
N ZZ 233.0
J MK 773.0
P LV 893.0
X RX 993.0
Sort By: 1.Contact, 2. Equipment, 3.Price, 4. exit
4
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.