complete the code public class Customer { // the Customer class has three fields
ID: 3726414 • Letter: C
Question
complete the code
public class Customer {
// the Customer class has three fields
private int custId;
private String name;
private boolean goldDude;
// the following will be used to automatically generate customer ids
//so they will be unique
private static int nextNum = 100;
// empty constructor
public Customer()
{
custId = nextNum;
nextNum++;
}
// constructor for creating a new customer where the id is
// automatically created
public Customer(String n)
{
name = n;
custId = nextNum;
nextNum++;
}
// full constructor
public Customer( String n, boolean gcm)
{
custId = nextNum;
nextNum++;
name = n;
goldDude = gcm;
}
// toString method
public String toString()
{
return name + " (custID: " + custId + ")";
}
//getters and setters
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNextNum() {
return nextNum;
}
public void setNextNum(int nextNum) {
this.nextNum = nextNum;
}
public boolean isGoldCardMember()
{
return goldDude;
}
public void setGoldCardMember(boolean goldCardMember) {
this.goldDude = goldCardMember;
}
}
import java.text.NumberFormat;
public class Rental {
// the Rental class has four fields
private Customer cust;
private Auto auto;
private int numDays;
private double rentalCost;
private char discount; // N = none G = gold D = days B = both
// all cars rent for $15.00 per day
final private double COST_PER_DAY = 15.00;
// TO DO!!write an empty and a full constructor
// toString method
//remember that each class should print out its own fields
// so we let the Auto and Customer class print out theirs
// this class only prints out the number of days field
@Override
public String toString()
{
NumberFormat nf = NumberFormat.getCurrencyInstance();
String string;
string = cust.toString() + " rented " ;
string = string + auto.toString() + " for " + numDays + " days.";
string = string + " The cost was " + nf.format(rentalCost) + " ";
switch (discount) {
case 'B':
string = string + "This person was both a gold card memeber and rented over six days and received a a 25% discount";
break;
case 'G':
string = string + "This person was a gold card memeber and received a a 10% discount";
break;
case 'D':
string = string + "This person rented over six days and received a 15% discount";
break;
case 'N':
string = string + "This person did not qualify for a discount";
}
return string;
}
// TO DO!!!the setRentalCost is where the cost is set
public void setRentalCost()
{
// give them a 15% discount if they rent over 6 days. Also Gold card members get an
//extra 10% off the original cost regardless of the number of days
// note next line is just a place holder. Also set the discount code
}
// generate getters and setters
}
public class Auto {
// the Auto class has two fields
private int year;
private String make;
private String model;
private String vin;
private double miles;
// empty constructor
public Auto()
{
}
// full constructor
public Auto(int year, String make, String model, String vin, double miles) {
this.year = year;
this.make = make;
this.model = model;
this.vin = vin;
this.miles = miles;
}
// toString method (the wording does not make sense until you realize
// that you will be calling this after calling the customer toString method
public String toString()
{
return "a " + year + " " + make + " " + model +
" with a VIN# of " + vin + " and a mileage of " + miles;
//return "a car with a vin of " + vin + " and a mileage of " + miles;
}
// getters and setters
public double getMiles() {
return miles;
}
public void setMiles(double miles) {
this.miles = miles;
}
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
import java.util.*;
import java.text.*;
public class WreckDriver {
public static void main(String[] args)
{
}
public static void loadNewData(ArrayList<Auto> a, ArrayList<Customer> c)
{
a.add(new Auto(2009,"Ford" , "Mustang","ABC123", 1256.54));
a.add(new Auto(2010,"Chevy","Camero","QWI459", 33.98));
a.add(new Auto(1970,"Pink","Cadillac","950AKH", 212874.51));
a.add(new Auto(2007,"Lotus","Elise MkII","1A2D3F", 12859.90));
c.add(new Customer( "Brett Farve",false));
c.add(new Customer( "Bruce Springsteen",true));
c.add(new Customer( "Mickey Mouse", true));
c.add(new Customer( "Peyton Manning", true));
c.add(new Customer( "Donald Duck", true));
}
//Prints the cars that are available for rent
public static void showCars(ArrayList<Auto> a)
{
}
// prints all the rentals that have been made
public static void printRentals(ArrayList<Rental> rents)
{
}
}
Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)
Which customer? 2
This customer is a gold card member so we will treat him well!
The following cars are available to rent...
1. a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2. a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3. a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4. a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
Which auto? 3
How many days do you wish to have this beautiful vehicle? 10
Bruce Springsteen (custID: 101) rented a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51 for 10 days.
The cost was $112.50
This person was both a gold card member and rented over six days and received a a 25% discount
More rentals (true/false)? true
Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)
Which customer? 4
This customer is a gold card member so we will treat him well!
The following cars are available to rent...
1. a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2. a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3. a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4. a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
Which auto? 4
How many days do you wish to have this beautiful vehicle? 1
Peyton Manning (custID: 103) rented a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9 for 1 days.
The cost was $13.50
This person was a gold card member and received a a 10% discount
More rentals (true/false)? true
Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)
Which customer? 1
This customer is not a gold card member
The following cars are available to rent...
1. a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2. a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3. a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4. a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
Which auto? 1
How many days do you wish to have this beautiful vehicle? 15
Brett Farve (custID: 100) rented a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54 for 15 days.
The cost was $191.25
This person rented over six days and received a 15% discount
More rentals (true/false)? false
Rental summary:
Bruce Springsteen (custID: 101) rented a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51 for 10 days.
The cost was $112.50
This person was both a gold card member and rented over six days and received a a 25% discount
Peyton Manning (custID: 103) rented a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9 for 1 days.
The cost was $13.50
This person was a gold card member and received a a 10% discount
Brett Farve (custID: 100) rented a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54 for 15 days.
The cost was $191.25
This person rented over six days and received a 15% discount
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements.
1. Modified the Rentals class to complete the required constructors and setRentalCost() method.
2. Modified the WreckDriver class, implemented all methods to interact with the user, create and display rentals and
display a rental summary at the end of the program.
3. Comments are included wherever code has been modified, If you have any doubts, feel free to ask, Thanks
EDIT: getting character limit exceeded error while trying to paste without losing the format, so I’m pasting as plain
text, sorry if the code indentation and format is messed up.
// Rental.java
import java.text.NumberFormat;
public class Rental {
// the Rental class has four fields
private Customer cust;
private Auto auto;
private int numDays;
private double rentalCost;
private char discount; // N = none G = gold D = days B = both
// all cars rent for $15.00 per day
final private double COST_PER_DAY = 15.00;
//empty constructor
public Rental() {
cust = null;
auto = null;
numDays = 0;
rentalCost = 0;
discount = 'N';
}
//full constructor
public Rental(Customer cust, Auto auto, int numDays) {
this.cust = cust;
this.auto = auto;
this.numDays = numDays;
}
// toString method
// remember that each class should print out its own fields
// so we let the Auto and Customer class print out theirs
// this class only prints out the number of days field
@Override
public String toString()
{
NumberFormat nf = NumberFormat.getCurrencyInstance();
String string;
string = cust.toString() + " rented ";
string = string + auto.toString() + " for " + numDays + " days.";
string = string + " The cost was " + nf.format(rentalCost) + " ";
switch (discount) {
case 'B':
string = string
+ "This person was both a gold card memeber and rented over six days and received
a a 25% discount";
break;
case 'G':
string = string
+ "This person was a gold card memeber and received a a 10% discount";
break;
case 'D':
string = string
+ "This person rented over six days and received a 15% discount";
break;
case 'N':
string = string + "This person did not qualify for a discount";
}
return string;
}
/**
* implemented method to set the rental cost
*/
public void setRentalCost()
{
/**
* updating the discount variable as needed.
*/
if (cust.isGoldCardMember() && numDays >= 6) {
discount = 'B';
} else if (cust.isGoldCardMember()) {
discount = 'G';
} else if (numDays >= 6) {
discount = 'D';
} else {
discount = 'N';
}
/**
* finding the original cost
*/
double cost = COST_PER_DAY * numDays;
/**
* Calculating and setting the discounted cost (final rental cost)
*/
switch (discount) {
case 'B':
rentalCost = cost - (cost * 0.15 + cost * 0.10);
break;
case 'G':
rentalCost = cost - (cost * 0.10);
break;
case 'D':
rentalCost=cost-(cost*0.15);
break;
case 'N':
rentalCost=cost;
}
// give them a 15% discount if they rent over 6 days. Also Gold card
// members get an
// extra 10% off the original cost regardless of the number of days
// note next line is just a place holder. Also set the discount code
}
// getters and setters
public Customer getCust() {
return cust;
}
public void setCust(Customer cust) {
this.cust = cust;
}
public Auto getAuto() {
return auto;
}
public void setAuto(Auto auto) {
this.auto = auto;
}
public int getNumDays() {
return numDays;
}
public void setNumDays(int numDays) {
this.numDays = numDays;
}
public double getRentalCost() {
return rentalCost;
}
public char getDiscount() {
return discount;
}
public void setDiscount(char discount) {
this.discount = discount;
}
public double getCOST_PER_DAY() {
return COST_PER_DAY;
}
}
// WreckDriver.java
import java.util.*;
public class WreckDriver {
public static void main(String[] args)
{
/**
* Initializing the three required array lists
*/
ArrayList<Auto> autos = new ArrayList<Auto>();
ArrayList<Customer> customers = new ArrayList<Customer>();
ArrayList<Rental> rentals = new ArrayList<Rental>();
// Scanner for reading user input
Scanner scanner = new Scanner(System.in);
/**
* loading vehicles and customers
*/
loadNewData(autos, customers);
String choice = "true";
int custId, vehId, days;
/**
* loops until user chooses to quit
*/
while (choice.equalsIgnoreCase("true")) {
showCustomers(customers);
System.out.println(" Which customer:?");
custId = Integer.parseInt(scanner.nextLine());
custId--; // decrementing by 1 to make it as the ArrayList index
if (custId < 0 || custId >= customers.size()) {
/**
* Invalid customer
*/
System.out.println("Invalid choice");
} else {
if (!customers.get(custId).isGoldCardMember()) {
/**
* Displaying message that the customer isnt a gold card
* member
*/
System.out
.println("This customer is not a gold card member");
} else {
/**
* Displaying message that the customer is a gold card
* member
*/
System.out.println("This customer is a gold card member"
+ " so we will treat him well!");
}
/**
* displaying cars
*/
showCars(autos);
System.out.println("Which auto?");
vehId = Integer.parseInt(scanner.nextLine());
vehId--;// decrementing by 1 to make it as the ArrayList index
if (vehId < 0 || vehId >= autos.size()) {
System.out.println("Invalid choice");
} else {
System.out
.println("How many days do you wish to have this beautiful
vehicle? ");
days = Integer.parseInt(scanner.nextLine());
if (days < 0) {
System.out.println("Invalid number of days");
} else {
/**
* Defining a Rental
*/
Rental rental = new Rental(customers.get(custId),
autos.get(vehId), days);
/**
* setting the cost
*/
rental.setRentalCost();
/**
* Displaying the rental
*/
System.out.println(rental);
/**
* Adding to the rentals list
*/
rentals.add(rental);
}
}
}
System.out.println("More rentals (true/false)? ");
choice = scanner.nextLine();
}
/**
* Displaying rental summary
*/
printRentals(rentals);
}
public static void loadNewData(ArrayList<Auto> a, ArrayList<Customer> c)
{
a.add(new Auto(2009, "Ford", "Mustang", "ABC123", 1256.54));
a.add(new Auto(2010, "Chevy", "Camero", "QWI459", 33.98));
a.add(new Auto(1970, "Pink", "Cadillac", "950AKH", 212874.51));
a.add(new Auto(2007, "Lotus", "Elise MkII", "1A2D3F", 12859.90));
c.add(new Customer("Brett Farve", false));
c.add(new Customer("Bruce Springsteen", true));
c.add(new Customer("Mickey Mouse", true));
c.add(new Customer("Peyton Manning", true));
c.add(new Customer("Donald Duck", true));
}
// Prints the cars that are available for rent
public static void showCars(ArrayList<Auto> a)
{
System.out.println("The following cars are available to rent...");
/**
* Displaying all items in the array list
*/
for (int i = 0; i < a.size(); i++) {
System.out.println((i + 1) + " " + a.get(i));
}
}
// prints all the rentals that have been made
public static void printRentals(ArrayList<Rental> rents)
{
System.out.println("Rental Summary:");
/**
* Displaying all items in the array list
*/
for (Rental r : rents) {
System.out.println(r);
}
}
// Print the details of all customers
public static void showCustomers(ArrayList<Customer> c) {
System.out.println("Customers:");
/**
* Displaying all items in the array list
*/
for (int i = 0; i < c.size(); i++) {
System.out.println((i + 1) + " " + c.get(i));
}
}
}
// Auto.java
public class Auto {
// the Auto class has two fields
private int year;
private String make;
private String model;
private String vin;
private double miles;
// empty constructor
public Auto()
{
}
// full constructor
public Auto(int year, String make, String model, String vin, double miles) {
this.year = year;
this.make = make;
this.model = model;
this.vin = vin;
this.miles = miles;
}
// toString method (the wording does not make sense until you realize
// that you will be calling this after calling the customer toString method
public String toString()
{
return "a " + year + " " + make + " " + model +
" with a VIN# of " + vin + " and a mileage of " + miles;
// return "a car with a vin of " + vin + " and a mileage of " + miles;
}
// getters and setters
public double getMiles() {
return miles;
}
public void setMiles(double miles) {
this.miles = miles;
}
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
// Customer.java
public class Customer {
// the Customer class has three fields
private int custId;
private String name;
private boolean goldDude;
// the following will be used to automatically generate customer ids
// so they will be unique
private static int nextNum = 100;
// empty constructor
public Customer()
{
custId = nextNum;
nextNum++;
}
// constructor for creating a new customer where the id is
// automatically created
public Customer(String n)
{
name = n;
custId = nextNum;
nextNum++;
}
// full constructor
public Customer(String n, boolean gcm)
{
custId = nextNum;
nextNum++;
name = n;
goldDude = gcm;
}
// toString method
public String toString()
{
return name + " (custID: " + custId + ")";
}
// getters and setters
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNextNum() {
return nextNum;
}
public void setNextNum(int nextNum) {
this.nextNum = nextNum;
}
public boolean isGoldCardMember()
{
return goldDude;
}
public void setGoldCardMember(boolean goldCardMember) {
this.goldDude = goldCardMember;
}
}
/*OUTPUT*/
Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)
Which customer:?
2
This customer is a gold card member so we will treat him well!
The following cars are available to rent...
1 a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2 a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3 a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4 a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
Which auto?
3
How many days do you wish to have this beautiful vehicle?
10
Bruce Springsteen (custID: 101) rented a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51 for 10 days.
The cost was Rs.112.50
This person was both a gold card memeber and rented over six days and received a a 25% discount
More rentals (true/false)?
true
Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)
Which customer:?
4
This customer is a gold card member so we will treat him well!
The following cars are available to rent...
1 a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2 a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3 a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4 a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
Which auto?
4
How many days do you wish to have this beautiful vehicle?
1
Peyton Manning (custID: 103) rented a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9 for 1 days.
The cost was Rs.13.50
This person was a gold card memeber and received a a 10% discount
More rentals (true/false)?
true
Customers:
1 Brett Farve (custID: 100)
2 Bruce Springsteen (custID: 101)
3 Mickey Mouse (custID: 102)
4 Peyton Manning (custID: 103)
5 Donald Duck (custID: 104)
Which customer:?
1
This customer is not a gold card member
The following cars are available to rent...
1 a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54
2 a 2010 Chevy Camero with a VIN# of QWI459 and a mileage of 33.98
3 a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51
4 a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9
Which auto?
1
How many days do you wish to have this beautiful vehicle?
15
Brett Farve (custID: 100) rented a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54 for 15 days.
The cost was Rs.191.25
This person rented over six days and received a 15% discount
More rentals (true/false)?
false
Rental Summary:
Bruce Springsteen (custID: 101) rented a 1970 Pink Cadillac with a VIN# of 950AKH and a mileage of 212874.51 for 10 days.
The cost was Rs.112.50
This person was both a gold card memeber and rented over six days and received a a 25% discount
Peyton Manning (custID: 103) rented a 2007 Lotus Elise MkII with a VIN# of 1A2D3F and a mileage of 12859.9 for 1 days.
The cost was Rs.13.50
This person was a gold card memeber and received a a 10% discount
Brett Farve (custID: 100) rented a 2009 Ford Mustang with a VIN# of ABC123 and a mileage of 1256.54 for 15 days.
The cost was Rs.191.25
This person rented over six days and received a 15% discount
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.