Untitled
unknown
plain_text
a year ago
2.6 kB
10
Indexable
import java.util.ArrayList; import java.util.Date; import java.util.List; class Order { private Date date; private String status; private int quantity; private int unitPrice; public Order(Date date, String status, int quantity, int unitPrice) { this.date = date; this.status = status; this.quantity = quantity; this.unitPrice = unitPrice; } public int calcTotal() { return unitPrice * quantity; } @Override public String toString() { return "Order[date=" + date + ", status=" + status + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]"; } } class OrderDetails { private String quality; private boolean taxStatus; public OrderDetails(String quality, boolean taxStatus) { this.quality = quality; this.taxStatus = taxStatus; } @Override public String toString() { return "OrderDetails[quality=" + quality + ", taxStatus=" + (taxStatus ? "Paid" : "Unpaid") + "]"; } } class Customer { private String name; private String address; private List<Order> orders; public Customer(String name, String address) { this.name = name; this.address = address; this.orders = new ArrayList<>(); } public void addOrder(Order order) { orders.add(order); } public int calcTotalOrders() { int total = 0; for (Order order : orders) { total += order.calcTotal(); } return total; } @Override public String toString() { return "Customer[name=" + name + ", address=" + address + ", orders=" + orders + "]"; } } class Main { public static void main(String[] args) { // Creating OrderDetails object OrderDetails orderDetails = new OrderDetails("High Quality", true); // Creating Order object Date orderDate = new Date(); Order order = new Order(orderDate, "confirmed", 2, 50); // Adding OrderDetails to Order orderDetails.toString(); order.toString(); // Creating Customer object Customer customer = new Customer("John Doe", "123 Main Street"); // Adding Order to Customer customer.addOrder(order); // Calculating and displaying total orders int totalOrders = customer.calcTotalOrders(); System.out.println("Total orders for customer " + customer.toString() + ": " + totalOrders); } }
Editor is loading...
Leave a Comment