Question
Account class has an integer instance variable, amount, to represent the available fund. The canAfford() method takes an integer parameter being a cost of an
Account class has an integer instance variable, amount, to represent the available fund. The canAfford() method takes an integer parameter being a cost of an order item and returns if there is enough fund in the account to afford that. The spendAmount() method takes also an integer parameter being a cost of an order item and deducts the amount by that cost. Order class uses an object of Account class. It should have a list of OrderItem objects. The addOrderItem() method takes an object of OrderItem as the parameter. It calls the account object to check if there is enough fund to cover the cost of the order item. If so, it invokes the spendAmount() on the account object, stores the order item in a list and returns true. Otherwise, it returns false. The getTotal() method returns the total cost of all order items in the order. The printOrderItems() method prints information about each order item. OrderItem class has a String upc, an integer quantity and an integer price. The getCost() method returns the multiplication of its quantity and price. Introduce a Project2 class as a test driver. It should execute as follows:
a. Create an Account object with a random amount between 100 to 200 inclusive. b. Create an Order object and pass to it the account reference. i. Prompt the user to enter the UPC code for the item or done to quit. ii. While the user enter a UPC (anything other than done), prompt for and read the quantity. You may assume the user enters a valid input, that is, an integer. iii. Generate a random number from 50 to 100 for the price. iv. Create an OrderItem object with UPC, quantity and price. v. Invoke the addOrderItem() method on the order reference with the order item just created. If it returns false, it means there is not enough fund in the account, so break the loop. vi. Continue the loop until either user enters done or account can no longer afford a new order item.
c. Print out all the order items and the total cost.
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