Purpose of these exercises: To understand conditional statements

advertisement
Purpose of these exercises: To understand conditional statements.
A wholesale company discounts the price of its products depending on the number of units bought and the
unit price per for each item. The rules for calculating discounts are shown in the table below.
Unit Price
Quantity
0 - $10.00
$10.01 - $20.00
$20.01 & over
1- 49
0%
1%
2%
50 - 99
1%
2%
3%
100 & over
2%
5%
10%
To solve this problem do the following:
1.
Complete the class Discount shown below. The class accepts the number of units and the
corresponding price for items sold. It validates the quantities and calculates the discount amount
where possible.
class Discount
{
int quantity;
double price;
Discount(int quantity, double price)
{
this.quantity = quantity;
this.price = price;
}
boolean quantityOutOfRange()
{
return quantity < 0;
}
boolean priceOutOfRange()
{
return price < 0;
}
void calculate()
{
if (quantity > NINETY_NINE)
{
if (price > TWENTY)
discount = TEN_PERCENT;
else if ( price > TEN)
discount = FIVE_PERCENT;
else
discount = TWO_PERCENT;
}
else if (quantity > FORTY_NINE)
{
// calculate discount
}
else
{
// calculate discount
}
discount_amount = discount * quantity * price;
}
}
2.
Write a test class called TestDiscount that uses the data given below to test class Discount.
(a) Enter the data one set at a time, since looping was not part of the lesson.
(b) You need to define the constants.
Use the following test data.
Quantity
Unit Price
10
$15.00
-10
$5.00
50
- $10.00
200
$25.00
The output must be of the form:
The quantity is: xx
The unit price is: $xx.xx
The discount is: $xx.xx
Or
The quantity is: -xx
Quantity is negative. Cannot compute discount
Or
The quantity is: 50
The unit price is: ($10.00) The money is negative, cannot
compute discount amount
Download