Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the Customer class, create a procedure called spend(float amount) that allows the customer to spend the amount of money indicated in the parameter. If

In the Customer class, create a procedure called spend(float amount) that allows the customer to spend the amount of money indicated in the parameter. If the customer has enough money, the money should be spent and the customer should have that amount of money less. If the Customer did not have enough money, no spending should take place. Test your code by adding the following to the bottom of your test program:

c2.spend(3); System.out.println("Dottie's money remaining is $" + c2.money);

If you did the first one right, then try this one:

c2.spend(-80); System.out.println("Dottie's money remaining is $" + c2.money);

When we call the spend() method, we do not really know if the operation was ignored (due to insufficient funds) or if the operation was successful. It is better to give some kind of "feedback" to the test method to indicate whether or not the spending operation was successful. One way of doing this is to simply examine the customer's money variable before the call and then after the call and compare them to see if there was a change ... but this is cumbersome. A better way to provide feedback to the caller of the method (i.e., the test program) would be to return a value from the method to indicate whether or not it worked. We can return a boolean indicating whether or not the spending was successful. Modify your spend() method so that it returns true if the spending was successful and false otherwise. Create, save, compile and run the following test program to test your new code:

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);

}

}

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));

Here are my codes:

public class Customer { String name; int age; char gender; float money; public Customer(String n){ name = n; age = 0; gender = 'M'; money = 0.0f; } public Customer(String n, int a, char g){ name = n; age = 0; gender = 'M'; } public Customer(String n, int a, char g, float m){ name = n; age = 0; gender = 'M'; money = 0.0f; } public Customer(){ } //4  public float computeFee() { if((age >= 18) && (age < 65)) { return 12.75f; } if(age <= 3) { return 0; } if(age >= 65) { return 6.375f; } if((age >= 4) && (age <= 17)) { return 8.50f; } else{ return 0; } } public void spend(float amount){ } } 

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', 10); c3 = new Customer("Jane"); c4 = new Customer(); 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 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions