Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ordersystem;
- import java.text.NumberFormat;
- import java.util.*;
- public class OrderSystem {
- public static void main(String args[]) {
- OrderSystem os = new OrderSystem();
- os.addCustomer(1, "Bill", "3 Red St", 1000);
- os.addCustomer(2, "Joe", "4 Blue St", 3000);
- os.addCustomer(3, "Dave", "4 Blue St", 3000);
- os.addItem("P001", "Widget", 12.56);
- os.addItem("P002", "Sprocket", 15.99);
- os.addOrder(1, 100, "P001", 5);
- os.addOrder(3, 101, "P002", 50);
- os.addOrder(3, 102, "P001", 7);
- os.displayOrder(100);
- os.displayOrder(102);
- os.displayAllOrders();
- os.displayItem("P002");
- os.deleteCustomer(3);
- }
- }
- class OrderSystem {
- private LinkedList<Customer> customers = new LinkedList<Customer>();
- private LinkedList<Item> items = new LinkedList<Item>();
- NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
- public OrderSystem() {
- }
- private String padRight(String s, int l) {
- String output;
- output = s;
- for (int i = 0 ; i < l - s.length() ; i++) {
- output += " ";
- }
- return output;
- }
- private String padIntRight(int n, int l) {
- String input = "" + n;
- String output;
- output = "" + input;
- for (int i = 0 ; i < l - input.length() ; i++) {
- output += " ";
- }
- return output;
- }
- private String padDoubleRight(double n, int l) {
- String input = "" + n;
- String output;
- output = "" + input;
- for (int i = 0 ; i < l - input.length() ; i++) {
- output += " ";
- }
- return output;
- }
- public Item searchItems(String itemID) {
- Item x = new Item("null", "null", -1);
- Item currentItem;
- for (int i = 0 ; i < items.size() ; i++) {
- currentItem = items.get(i);
- if (currentItem.getItemID().equals(itemID)) {
- x = currentItem;
- }
- }
- return x;
- }
- //public int searchItems(String itemID) {
- // int x = -1;
- // for (Item currentItem : items) {
- // if (currentItem.getItemID().equals(itemID)) {
- // x = items.indexOf(currentItem);
- // }
- // }
- // return x;
- //}
- public Customer searchCustomers(int customerID) {
- Customer x = new Customer(-1, "null", "null", -1);
- Customer currentCustomer;
- for (int i = 0 ; i < customers.size() ; i++) {
- currentCustomer = customers.get(i);
- if (currentCustomer.getCustomerID() == customerID) {
- x = currentCustomer;
- }
- }
- return x;
- }
- public void addCustomer(int customerID, String name, String address, double credit) {
- Customer customer = new Customer(customerID, name, address, credit);
- customers.add(customer);
- System.out.println("Customer: " + customerID + " added.");
- System.out.println();
- }
- public void addItem(String itemID, String description, double unitPrice) {
- Item item = new Item(itemID, description, unitPrice);
- items.add(item);
- System.out.println("Item: " + itemID + " added.");
- System.out.println();
- }
- public void addOrder(int customerID, int orderID, String itemID, int quantity) {
- if (!searchCustomers(customerID).getName().equals("null")) {
- searchCustomers(customerID).addOrder(orderID, itemID, quantity);
- } else {
- System.out.println("Customer: " + customerID + " not found.");
- System.out.println();
- }
- }
- public LinkedList<Order> getAllOrders() {
- LinkedList<Order> orders = new LinkedList<Order>();
- LinkedList<Order> allOrders = new LinkedList<Order>();
- System.out.println("Displaying all Orders: ");
- for (Customer customer: customers) {
- orders = customer.getOrders();
- for (Order order : orders) {
- allOrders.add(order);
- }
- }
- return allOrders;
- }
- public void displayAllOrders() {
- for (Order order: getAllOrders()) {
- displayOrder(order.getOrderID());
- }
- }
- public void displayItem(String itemID) {
- if (!searchItems(itemID).getDescription().equals("null")) {
- //if (!items.get(searchItems(itemID)).getDescription().equals("null")) {
- //Item item = items.get(searchItems(itemID));
- Item item = searchItems(itemID);
- System.out.println("Item ID: " + item.getItemID());
- System.out.println("Description: " + item.getDescription());
- System.out.println("Unit Price: " + defaultFormat.format(item.getUnitPrice()));
- System.out.println();
- } else {
- System.out.println("Item: " + itemID + " not found.");
- }
- }
- public void deleteCustomer(int customerID) {
- if (!searchCustomers(customerID).getName().equals("null")) {
- System.out.println("Removing customer: " + customerID);
- customers.remove(searchCustomers(customerID));
- System.out.println("Customer removed");
- } else {
- System.out.println("Customer: " + customerID + " not found.");
- }
- System.out.println();
- }
- public void displayOrder(int orderID) {
- Customer c = new Customer(-1, "null", "null", -1);
- Item item = new Item("null", "null", -1);
- Order o = new Order(-1, -1, "null");
- Boolean found = false;
- for (int i = 0 ; i < customers.size() ; i++) {
- LinkedList<Order> orders = customers.get(i).getOrders();
- for (int j = 0 ; j < orders.size() ; j++) {
- if (orders.get(j).getOrderID() == orderID) {
- c = customers.get(i);
- o = orders.get(j);
- item = searchItems(orders.get(j).getItemID());
- //item = items.get(searchItems(orders.get(j).getItemID()));
- found = true;
- }
- }
- }
- if (found) {
- System.out.println("Customer ID: " + c.getCustomerID());
- System.out.println("Name: " + c.getName());
- System.out.println("Address: " + c.getAddress());
- System.out.println("Order ID: " + orderID);
- System.out.println("Order date: " + o.getDate());
- System.out.println(padRight("Item", 10) + padRight("Description", 20) + padRight("Unit Price", 15) + padRight("Quantity", 10) + padRight("Cost", 6));
- System.out.println("---- ----------- ---------- -------- ---- ");
- System.out.println(padRight(item.getItemID(), 10) + padRight(item.getDescription(), 20) + padRight(defaultFormat.format(item.getUnitPrice()), 15) + padIntRight(o.getQuantity(), 10) + padRight(defaultFormat.format((item.getUnitPrice() * o.getQuantity())), 6));
- } else {
- System.out.println("Order: " + orderID + " not found.");
- }
- System.out.println();
- }
- }
- class Order {
- private int m_orderID;
- private int m_quantity;
- private String m_itemID;
- private Date m_date;
- public Order(int ID, int quantity, String itemID) {
- setOrderID(ID);
- setQuantity(quantity);
- setItemID(itemID);
- Date tempDate = new Date();
- setDate(tempDate);
- }
- public void setOrderID(int ID) {
- m_orderID = ID;
- }
- public int getOrderID() {
- return m_orderID;
- }
- public void setQuantity(int quantity) {
- m_quantity = quantity;
- }
- public int getQuantity() {
- return m_quantity;
- }
- public void setItemID(String item) {
- m_itemID = item;
- }
- public String getItemID() {
- return m_itemID;
- }
- public void setDate(Date date) {
- m_date = date;
- }
- public Date getDate() {
- return m_date;
- }
- }
- class Customer {
- private int m_custID;
- private String m_name;
- private String m_address;
- private double m_creditRating;
- private LinkedList<Order> orders = new LinkedList<Order>();
- public Customer(int custID, String name, String address, double creditRating) {
- setCustomerID(custID);
- setName(name);
- setAddress(address);
- setCreditRating(creditRating);
- }
- public LinkedList<Order> getOrders() {
- return orders;
- }
- public void addOrder(int orderID, String itemID, int quantity) {
- Order o = new Order(orderID, quantity, itemID);
- orders.add(o);
- System.out.println("Order: " + orderID + " added to Customer: " + getCustomerID());
- System.out.println();
- }
- public void setCustomerID(int ID) {
- m_custID = ID;
- }
- public int getCustomerID() {
- return m_custID;
- }
- public void setName(String name) {
- m_name = name;
- }
- public String getName() {
- return m_name;
- }
- public void setAddress(String address) {
- m_address = address;
- }
- public String getAddress() {
- return m_address;
- }
- public void setCreditRating(double rating) {
- m_creditRating = rating;
- }
- public double getCreditRating() {
- return m_creditRating;
- }
- }
- class Item {
- private String m_itemID;
- private String m_description;
- private double m_unitPrice;
- public Item(String ID, String description, double unitPrice) {
- setItemID(ID);
- setDescription(description);
- setUnitPrice(unitPrice);
- }
- public void setItemID(String ID) {
- m_itemID = ID;
- }
- public String getItemID() {
- return m_itemID;
- }
- public void setDescription(String description) {
- m_description = description;
- }
- public String getDescription() {
- return m_description;
- }
- public void setUnitPrice(double unitPrice) {
- m_unitPrice = unitPrice;
- }
- public double getUnitPrice() {
- return m_unitPrice;
- }
- }
Add Comment
Please, Sign In to add comment