Question
In the Customer class, create a method called hasMoreMoneyThan(Customer c) that returns a boolean value of true if the customer has more money than the
In the Customer class, create a method called hasMoreMoneyThan(Customer c) that returns a boolean value of true if the customer has more money than the customer passed in as parameter c and false otherwise. Add the following two lines of code to the bottom of your CustomerSpendingTestProgram to make sure that the answers are false and true, respectively:
System.out.println("Bob has more money than Dottie: " + c1.hasMoreMoneyThan(c2)); System.out.println("Dottie has more money than Jane: " + c2.hasMoreMoneyThan(c3));
Now we will modify our test code to make use of our computeFee() method to gain admission into an event (i.e., such as a circus or movie). Below is the test code that we would like to have working. Create that test program, but it will not yet compile. Notice that there is a new method called payAdmission() which simulates a person paying admission to the circus.
public class CustomerAdmissionTestProgram { public static void main(String args[]) { Customer c1, c2, c3, c4; c1 = new Customer("Bob", 17, 'M', 100); c2 = new Customer("Dottie", 3, 'F', 10); c3 = new Customer("Jane", 24, 'F', 40); c4 = new Customer("Sam", 72, 'M', 5); System.out.println("Here is the money before going into the circus:"); System.out.println(" Bob has $" + c1.money); System.out.println(" Dottie has $" + c2.money); System.out.println(" Jane has $" + c3.money); System.out.println(" Sam has $" + c4.money); // Simulate people going into the circus c1.payAdmission(); c2.payAdmission(); c3.payAdmission(); c4.payAdmission(); System.out.println("Here is the money after going into the circus:"); System.out.println(" Bob has $" + c1.money); System.out.println(" Dottie has $" + c2.money); System.out.println(" Jane has $" + c3.money); System.out.println(" Sam has $" + c4.money); } }
Write the payAdmission() method. It should have a void return type. Your method should be one line long ... making use of the computeFee() and spend() methods that you wrote earlier. Compile your code, test it by running the CustomerAdmissionTestProgram and then examine the output to make sure that it is correct.
We will now adjust our Customer object so that we can keep track of who has paid for their admission and who has not. Add a boolean attribute (i.e., instance variable) to the Customer class called admitted. Set this attribute to false in ALL of your constructors. Modify your payAdmission() method so that this variable is set to true when the customer has successfully paid the proper fee amount. Adjust your CustomerAdmissionTestProgram code to display this new attribute after each call to payAdmission() so that you can see if it is set properly. Here is the portion of code to change:
public class CustomerAdmissionTestProgram { public static void main(String args[]) { Customer c1, c2, c3, c4; c1 = new Customer("Bob", 17, 'M', 100); c2 = new Customer("Dottie", 3, 'F', 10); c3 = new Customer("Jane", 24, 'F', 40); c4 = new Customer("Sam", 72, 'M', 5); System.out.println("Here is the money before going into the circus:"); System.out.println(" Bob has $" + c1.money); System.out.println(" Dottie has $" + c2.money); System.out.println(" Jane has $" + c3.money); System.out.println(" Sam has $" + c4.money); // Simulate people going into the circus c1.payAdmission(); c2.payAdmission(); c3.payAdmission(); c4.payAdmission(); System.out.println("Here is the money after going into the circus:"); System.out.println(" Bob has $" + c1.money); System.out.println(" Dottie has $" + c2.money); System.out.println(" Jane has $" + c3.money); System.out.println(" Sam has $" + c4.money); } } // Simulate people going into the circus System.out.println(" Bob has been admitted ... " + c1.admitted); System.out.println(" Dottie has been admitted ... " + c2.admitted); System.out.println(" Jane has been admitted ... " + c3.admitted); System.out.println(" Sam has been admitted ... " + c4.admitted); c1.payAdmission(); System.out.println("Bob has been admitted ... " + c1.admitted); c2.payAdmission(); System.out.println("Dottie has been admitted ... " + c2.admitted); c3.payAdmission(); System.out.println("Jane has been admitted ... " + c3.admitted); c4.payAdmission(); System.out.println("Sam has been admitted ... " + c4.admitted); System.out.println(" Bob has $" + c1.money); System.out.println(" Dottie has $" + c2.money); System.out.println(" Jane has $" + c3.money); System.out.println(" Sam has $" + c4.money);
Now we will adjust how a Customer object appears when displayed. We saw earlier that a Customer object appears like this when displayed: Customer@140e19d . We change the "look" of the customer by defining the String that will be returned when the customer is displayed. We do this through the toString() method. Write a toString() method so that customers appear in this format when displayed:
Customer Bob: a 17 year old male with $0.0 who has not been admitted Customer Dottie: a 3 year old female with $10.0 who has been admitted
Test your code by adding these two lines to the bottom of your CustomerAdmissionTestProgram:
System.out.println(c1); System.out.println(c2); System.out.println(c3); System.out.println(c4);
Create a static/class variable in the Customer class called CUSTOMER_NUMBER. Give it an initial value of 1. Add an appropriate instance variable to the Customer class called id. Change each constructor so that the id is properly set to be a unique number each time a Customer object is created. Alter the CustomerConstructorTestProgram so that it displays the id attribute for each Customer object as well. Test your code to make sure that each Customer gets a unique id, starting with 1.
Here are my codes:
public class Customer { String name; int age; char gender; float money; public Customer(String n) { this.name = n; this.age = 0; this.gender = 'M'; this.money = 0.0f; } public Customer(String n, int a, char g) { this.name = n; this.age = a; this.gender = g; this.money = 0.0f; } public Customer(String n, int a, char g, float m) { this.name = n; this.age = a; this.gender = g; this.money = m; } public Customer() { } // 4 public float computeFee() { if ((this.age >= 18) && (this.age < 65)) { return 12.75f; } if (this.age <= 3) { return 0; } if (this.age >= 65) { return 6.375f; } if ((this.age >= 4) && (this.age <= 17)) { return 8.50f; } else { return 0; } } public boolean spend(float amount) { if (this.money > amount && amount > 0) { this.money = this.money - amount; return true; } return false; } }
public class CustomerConstructorTestProgram { public static void main(String args[]) { Customer c1, c2, c3, c4; c1 = new Customer("Bob", 17, 'M'); c2 = new Customer("Dottie", 3, 'F', 10f); c3 = new Customer("Jane"); c4 = new Customer(); c2.spend(3); System.out.println("Dottie's money remaining is $" + c2.money); c2.spend(-80); System.out.println("Dottie's money remaining is $" + c2.money); System.out.println("Bob looks like this: " + c1.name + ", " + c1.age + ", " + c1.gender + ", " + c1.money); System.out.println("Dottie looks like this: " + c2.name + ", " + c2.age + ", " + c2.gender + ", " + c2.money); System.out.println("Jane looks like this: " + c3.name + ", " + c3.age + ", " + c3.gender + ", " + c3.money); System.out .println("Customer 4 looks like this: " + c4.name + ", " + c4.age + ", " + c4.gender + ", " + c4.money); System.out.println("Bob's fee is $" + c1.computeFee()); System.out.println("Dottie's fee is $" + c2.computeFee()); c3.age = 23; System.out.println("Jane's fee is $" + c3.computeFee()); c4.age = 67; System.out.println("No Name's fee is $" + c4.computeFee()); } }
public class CustomerSpendingTestProgram { public static void main(String args[]) { Customer c; c = new Customer("Bob", 25, 'M', 50.00f); System.out.println(c.money); if (!c.spend(10.00f)) System.out.println("Unable to spend $10"); if (!c.spend(4.75f)) System.out.println("Unable to spend $4.75"); if (!c.spend(15.25f)) System.out.println("Unable to spend $15.25"); if (!c.spend(100.00f)) System.out.println("Unable to spend $100"); System.out.println(c.money); } }
public class CustomerTestProgram { public static void main(String args[]) { Customer c; c = new Customer("Bob"); c.name = "Bob"; System.out.println(c.name); System.out.println(c.age); System.out.println(c.money); System.out.println(c.gender); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started