Skip to content

Instantly share code, notes, and snippets.

@JavaCS3
Last active April 11, 2019 14:43
Show Gist options
  • Save JavaCS3/b29026ef55232fcb070c97802e1146f9 to your computer and use it in GitHub Desktop.
Save JavaCS3/b29026ef55232fcb070c97802e1146f9 to your computer and use it in GitHub Desktop.
Interview Quizz

Interview Quizz: Cashier

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.

Q1: Simple Printing

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

Q2: Sales Promotion

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

Q3: More Than One Discount Strategy

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

Q4: Complex Discount Strategy

Now we have 2 discount strategies:

  1. All product get 10% off
  2. 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:

  1. All product get 10% off
  2. Milk get 50% off
  3. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment