Text Copied below for indexing- 4 . This exercise will get you experiment the co
ID: 3675861 • Letter: T
Question
Text Copied below for indexing-
4
.
This
exercise will get you experiment the composition relationship of
“
has
-
a
”
in OOP.
Consider the following UML conceptual model in Figure 1
and w
rite class definitions for
each of the co
ncepts.
+getCost() : int
-upc : String
-quantity : int
-price : int
OrderItem
+addOrderItem() : Boolean
+getTotal() : int
+printOrderItems()
Order
+canAfford() : Boolean
+spendAmount()
-amount : int
Account
1
*
-uses
*
1
Figure
1
Conceptual
UML
Model for Exercise 4
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 a
ccount 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
Homework2_4
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 t
o 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. Yo
u 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 jus
t 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
Orderltem String int int otal0: int Figure 1 Conceptual UML Model for Exercise 4 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 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 Homework2 4 class as a test driver. It should execute as follows Create an Account object with a random amount between 100 to 200 inclusive Create an Order object and pass to it the account reference a. b. 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 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.Explanation / Answer
Please find the required program and output below :
--------------------------------------------------------------------------------
output :
Enter the UPC code for the item or 'done' to exit
asd
Enter the quantity
1
Enter the UPC code for the item or 'done' to exit
xs
Enter the quantity
2
OrderItem{upc='asd', quantity=1, price=61}
Total cost : 61
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.