A cashier in a shop need a program that will print out a receipt for a given order. And you're going to implement that.
We're going to use some objects interaction to represent a payment process, and then print a receipt.
# python version
class Order(object):
def __init__(self, *products):
# Not implemented
pass
def statement(self):
# Not implemented
pass
class Product(object):
def __init__(self, name, price):
self.name = name
self.price = price
def __repr__(self):
return '<{0} {1:.2f}>'.format(self.name, self.price)
class Water(Product):
def __init__(self):
super(Water, self).__init__('water', 5)
class Coffee(Product):
def __init__(self):
super(Coffee, self).__init__('coffee', 20.0)
class Milk(Product):
def __init__(self):
super(Milk, self).__init__('milk', 15.0)
// java version
public static class Product {
public String name;
public double price;
public String toString() {
return String.format("%s: %2.2f", this.name, this.price);
}
}
public static class Water extends Product {
public Water() {
this.name = "water";
this.price = 5.0;
}
}
public static class Coffee extends Product {
public Coffee() {
this.name = "coffee";
this.price = 20.0;
}
}
public static class Milk extends Product {
public Milk() {
this.name = "milk";
this.price = 15.0;
}
}
public static class Order {
public Order(Product... products) {
// Not implemented
}
public double charge() {
// Not implemented
return 0.0;
}
public String statement() {
// Not implemented
return "Not implemented";
}
}
Here's how we represent an order and get the receipt
# python version
o = Order(
Water(),
Milk(),
Water(),
Milk(),
Coffee(),
Coffee(),
Coffee(),
)
print(o.statement())
// java version
Order o = new Order(
new Water(),
new Milk(),
new Water(),
new Milk(),
new Coffee(),
new Coffee(),
new Coffee()
);
System.out.println(o.statement());
Output shoud looks like this, the order of products must be the same as input: water
, milk
, coffee
(water is the first item and then milk then coffee)
NAME PRICE
water 5.00 x 2 = 10.00
milk 15.00 x 2 = 30.00
coffee 20.00 x 3 = 60.00
SUM 100.00
One day this shop want to have a sales promotion, all products will have 10% off.
Try to change your program to make the output looks like this.
NAME PRICE
water 5.00 x 2 x 90.00%(10% off) = 9.00
milk 15.00 x 2 x 90.00%(10% off) = 27.00
coffee 20.00 x 3 x 90.00%(10% off) = 54.00
SUM 90.00
The milk in this shop is going to past-date soon. It's better to sell milk out as fast as possible. So the shop decided to have milk 50% off and 10% off discount is still available.
Try to change your program to make the output looks like this.
NAME PRICE
water 5.00 x 2 x 90.00%(10% off) = 9.00
milk 15.00 x 2 x 50.00%(milk 50% off) x 90.00%(10% off) = 13.50
coffee 20.00 x 3 x 90.00%(10% off) = 54.00
SUM 76.50
Now we have 2 discount strategies:
- All product get 10% off
- Milk get 50% off
Let's add one more discount strategy
If an order have both water and milk all of them get 70% off, but the "Milk get 50% off" is not available now.
So the final discount strategy is as follow:
- All product get 10% off
- Milk get 50% off
- If an order have both water and milk all of them get 70% off, But the "#2: Milk get 50% off" is not available
if the order is
# python version
o = Order(
Water(),
Milk(),
Water(),
Milk(),
Coffee(),
Coffee(),
Coffee(),
)
// java version
Order o = new Order(
new Water(),
new Milk(),
new Water(),
new Milk(),
new Coffee(),
new Coffee(),
new Coffee()
);
System.out.println(o.statement());
the output should be
NAME PRICE
water 5.00 x 2 x 30.00%(milk&water 70% off) x 90.00%(10% off) = 2.70
milk 15.00 x 2 x 30.00%(milk&water 70% off) x 90.00%(10% off) = 8.10
coffee 20.00 x 3 x 90.00%(10% off) = 54.00
SUM 64.80
if the order is
# python version
o = Order(
Milk(),
Milk(),
Coffee(),
Coffee(),
Coffee(),
)
// java version
Order o = new Order(
new Milk(),
new Milk(),
new Coffee(),
new Coffee(),
new Coffee()
);
the output should be
NAME PRICE
milk 15.00 x 2 x 50.00%(milk 50% off) x 90.00%(10% off) = 13.50
coffee 20.00 x 3 x 90.00%(10% off) = 54.00
SUM 67.50