test 2 2017/2018

 avatar
unknown
plain_text
2 years ago
4.7 kB
7
Indexable
import java.util.ArrayList;

// Class representing a coffee item
class Coffee {
    private String coffeeId;
    private String flavour;
    private double pricePerUnit;

    // Constructor to initialize coffee attributes
    public Coffee(String id, String n, double p) {
        coffeeId = id;
        flavour = n;
        pricePerUnit = p;
    }

    // Getter method for coffee ID
    public String getId() {
        return coffeeId;
    }

    // Getter method for coffee flavour
    public String getFlavour() {
        return flavour;
    }

    // Getter method for coffee price
    public double getPrice() {
        return pricePerUnit;
    }

    // Display method to print coffee details
    public void display() {
        System.out.printf("%-15s%-15s%-10.2f\n", coffeeId, flavour, pricePerUnit);
    }
}

// Class representing a discounted coffee item, extending Coffee class
class DiscountedItem extends Coffee {
    private double discRate;

    // Constructor to initialize discounted item attributes
    public DiscountedItem(String id, String n, double p, double r) {
        super(id, n, p); // Call superclass constructor
        discRate = r;
    }

    // Override getPrice method to apply discount
    @Override
    public double getPrice() {
        return super.getPrice() - (super.getPrice() * discRate);
    }
}

// Class representing a customer
class Customer {
    private String customerName;

    // Constructor to initialize customer name
    public Customer(String n) {
        customerName = n;
    }

    // Getter method for customer name
    public String getName() {
        return customerName;
    }
}

// Class representing a coffee order
class CoffeeOrder {
    private int orderId, unit;
    private Customer customer;
    private Coffee item;

    // Constructor to initialize coffee order attributes
    public CoffeeOrder(int id, Customer c, Coffee i, int u) {
        orderId = id;
        customer = c;
        item = i;
        unit = u;
    }

    // Method to calculate total price of the coffee order
    public double calcTotalPrice() {
        return item.getPrice() * unit;
    }

    // Getter method for customer name
    public String getName() {
        return customer.getName();
    }

    // Getter method for coffee flavour
    public String getFlavour() {
        return item.getFlavour();
    }

    // Override toString method to display coffee order details
    public String toString() {
        return "\nOrder ID: " + orderId + "\nCustomer Name: " + getName() + "\nFlavour: " + getFlavour()
                + "\nQuantity: " + unit + "\nPrice per Unit: " + item.getPrice() + "\nTotal Price: "
                + calcTotalPrice();
    }
}

// Main class for testing the coffee order system
public class TestOrder {
    public static void main(String[] args) {
        // Create instances of Coffee and DiscountedItem
        Coffee myCoffee = new Coffee("CC001", "Caramel", 10.00);
        DiscountedItem yourCoffee = new DiscountedItem("CC008", "Latte", 8.00, 0.10);
        Coffee hisCoffee = new Coffee("CC009", "Mocha", 9.00);

        // Display header for the coffee menu
        System.out.println("\n ITEM DESCRIPTION");
        System.out.println("********************");
        System.out.printf("%-15s%-15s%-25s\n", "Coffee ID", "Flavour", "Price");
        System.out.printf("%-15s%-15s%-25s\n", "*********", "********", "******");

        // Create ArrayList to store coffee items
        ArrayList<Coffee> menu = new ArrayList<>();
        menu.add(myCoffee);
        menu.add(yourCoffee);
        menu.add(hisCoffee);

        // Display coffee menu items
        for (int i = 0; i < menu.size(); i++) {
            menu.get(i).display();
        }

        System.out.println("************************************");

        // Create an array of customers
        Customer[] c = new Customer[2];
        c[0] = new Customer("Arif");
        c[1] = new Customer("Hakim");

        // Create ArrayList to store coffee orders
        ArrayList<CoffeeOrder> order = new ArrayList<>();
        // Add coffee orders to the list
        order.add(new CoffeeOrder(1, c[0], myCoffee, 2));
        order.add(new CoffeeOrder(2, c[1], yourCoffee, 3));
        order.add(new CoffeeOrder(3, c[1], hisCoffee, 1));

        // Display the list of coffee orders
        System.out.println("List of orders :\n");
        for (int i = 0; i < order.size(); i++) {
            System.out.println(order.get(i).toString());
        }
    }
}
Editor is loading...
Leave a Comment