lab2 practice

 avatar
user_3924995
plain_text
6 months ago
3.6 kB
4
Indexable
package oe.wd;

public class Item {
    private String name;
    private double price;
    private int quantity;

    public Item(String name, double price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Item{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", quantity=" + quantity +
                '}';
    }
}


package oe.wd;

import java.util.ArrayList;

public class Order {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    //    private int total;

    ArrayList<Item> items;

    public Order(int id, ArrayList<Item> items) {
        this.id = id;
        this.items = new ArrayList<>();
    }

    //Calculate discount
    public double calculateDiscount(){

        double total = 0;
        if(total > 200){
            total += 100 * 0.05;
        }
        return total;
    }

}


package oe.wd;

import java.util.ArrayList;
import java.util.List;

public class Customer implements Comparable{
    private String name;
    private int age;
    private String address;

    List<Order> orders;

    public Customer(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        return Integer.compare(age, ((Customer)o).getAge());
    }
}
package oe.wd;

import java.util.PriorityQueue;

public class MainClass {
    public static void main(String[] args) {

        Customer c1 = new Customer("Alex", 19, "Madagascar 1");
        Customer c2 = new Customer("Martin", 23, "Madagascar 2");
        Customer c3 = new Customer("Gloria", 20, "Madagascar 3");
        Customer c4 = new Customer("Melman", 26, "Madagascar 4");

        PriorityQueue<Customer> customers = new PriorityQueue<>();
        customers.add(c1);
        customers.add(c2);
        customers.add(c3);
        customers.add(c4);


        for(Customer customer : customers){
        System.out.println(customer);
}
    }
}
Editor is loading...
Leave a Comment