Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What will be the customer class.spend class and customerspendingtestprogram for this exercise in java? 5.In the Customer class, create a procedure called spend(float amount) that

image text in transcribedimage text in transcribed

What will be the customer class.spend class and customerspendingtestprogram for this exercise in java?

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

6.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 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); } }

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

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

More Books

Students also viewed these Databases questions

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago