Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hmb;
- import java.text.NumberFormat;
- import java.util.*;
- public class Hmb {
- 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", "5 Green St", 3000);
- os.addCD("P001", "Get Back", 12.56, "Beatles", 8);
- os.addDVD("P002", "Plenty", 15.99, "Joe Doe");
- os.addVideoGame("P003", "Lara Croft", 18.00, 1, "PC");
- os.addOrder(1, 100, "P001", 5);
- os.addOrder(3, 101, "P002", 50);
- os.addOrder(3, 102, "P001", 7);
- os.displayOrder(101);
- os.displayOrder(102);
- os.displayItem("P001");
- os.displayItem("P002");
- os.displayItem("P003");
- }
- }
- 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 DVD extends Item {
- private String m_director;
- public DVD(String itemID, String title, double unitPrice, String director) {
- setItemID(itemID);
- setTitle(title);
- setUnitPrice(unitPrice);
- setDirector(director);
- }
- public void setDirector(String director) {
- m_director = director;
- }
- public String getDirector() {
- return m_director;
- }
- @Override
- public void display() {
- System.out.println("Displaying Item: " + getItemID());
- System.out.println("--------------------------------------------------------------------------------------------------------------");
- System.out.println("Item " + "Type " + "Title " + "Director " + "Unit Price ");
- System.out.println("---- " + "---- " + "----- " + "-------- " + "---------- ");
- System.out.println(padRight(getItemID(), 10) + "DVD " + padRight(getTitle(), 20) + padRight(getDirector(), 40) + padRight(defaultFormat.format(getUnitPrice()), 15));
- }
- @Override
- public void display(int quantity) {
- System.out.println("Item " + "Type " + "Title " + "Director " + "Unit Price " + "Quantity " + "Cost ");
- System.out.println("---- " + "---- " + "----- " + "-------- " + "---------- " + "-------- " + "---- ");
- System.out.println(padRight(getItemID(), 10) + "DVD " + padRight(getTitle(), 20) + padRight(getDirector(), 40) + padRight(defaultFormat.format(getUnitPrice()), 15) + padRight(quantity, 10) + padRight(defaultFormat.format((getUnitPrice() * quantity)), 6));
- }
- }
- abstract class Item {
- private String m_itemID;
- private String m_title;
- private double m_unitPrice;
- NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
- public String padRight(String s, int l) {
- String output;
- output = s;
- for (int i = 0 ; i < l - s.length() ; i++) {
- output += " ";
- }
- return output;
- }
- public String padRight(int n, int l) {
- String input = "" + n;
- String output = input;
- for (int i = 0 ; i < l - input.length() ; i++) {
- output += " ";
- }
- return output;
- }
- public void setItemID(String ID) {
- m_itemID = ID;
- }
- public String getItemID() {
- return m_itemID;
- }
- public void setTitle(String title) {
- m_title = title;
- }
- public String getTitle() {
- return m_title;
- }
- public void setUnitPrice(double unitPrice) {
- m_unitPrice = unitPrice;
- }
- public double getUnitPrice() {
- return m_unitPrice;
- }
- public void display() {
- }
- public void display(int quantity) {
- }
- }
- 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 OrderSystem {
- private LinkedList<Customer> customers = new LinkedList<Customer>();
- private LinkedList<Item> items = new LinkedList<Item>();
- NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
- public OrderSystem() {
- }
- 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();
- }
- //USE CASE: ADD A CD
- public void addCD(String itemID, String title, double unitPrice, String artist, int numberOfTracks) {
- items.add(new CD(itemID, title, unitPrice, artist, numberOfTracks));
- System.out.println("CD: " + itemID + " added.");
- System.out.println();
- }
- //USE CASE: ADD A DVD
- public void addDVD(String itemID, String title, double unitPrice, String director) {
- items.add(new DVD(itemID, title, unitPrice, director));
- System.out.println("DVD: " + itemID + " added.");
- System.out.println();
- }
- //USE CASE: ADD A VIDEO GAME
- public void addVideoGame(String itemID, String title, double unitPrice, int numberOfPlayers, String platform) {
- items.add(new VideoGame(itemID, title, unitPrice, numberOfPlayers, platform));
- System.out.println("Video Game: " + 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>();
- for (Customer customer: customers) {
- orders = customer.getOrders();
- for (Order order : orders) {
- allOrders.add(order);
- }
- }
- return allOrders;
- }
- public void displayAllOrders() {
- System.out.println("Displaying all orders");
- System.out.println("----------------------------------------------------------------------------------------------------------------------------------");
- for (Order order: getAllOrders()) {
- displayOrder(order.getOrderID());
- }
- System.out.println("----------------------------------------------------------------------------------------------------------------------------------");
- System.out.println();
- }
- //USE CASE: DISPLAY DETAILS OF ITEM WITH A GIVEN ITEM ID
- public void displayItem(String itemID) {
- if (searchItems(itemID) != -1) {
- Item item = items.get(searchItems(itemID));
- item.display();
- 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);
- 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);
- found = true;
- }
- }
- }
- if (found) {
- System.out.println("Displaying Order: " + orderID);
- System.out.println("--------------------------------------------------------------------------------------------------------------");
- 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());
- items.get(searchItems(o.getItemID())).display(o.getQuantity());
- } else {
- System.out.println("Order: " + orderID + " not found.");
- }
- System.out.println();
- }
- }
- class VideoGame extends Item {
- private int m_numberOfPlayers;
- private String m_platform;
- public VideoGame(String itemID, String title, double unitPrice, int numberOfPlayers, String platform) {
- setItemID(itemID);
- setTitle(title);
- setUnitPrice(unitPrice);
- setNumberOfPlayers(numberOfPlayers);
- setPlatform(platform);
- }
- public void setNumberOfPlayers(int numberOfPlayers) {
- m_numberOfPlayers = numberOfPlayers;
- }
- public int getNumberOfPlayers() {
- return m_numberOfPlayers;
- }
- public void setPlatform(String platform) {
- m_platform = platform;
- }
- public String getPlatform() {
- return m_platform;
- }
- @Override
- public void display() {
- System.out.println("Displaying Item: " + getItemID());
- System.out.println("--------------------------------------------------------------------------------------------------------------");
- System.out.println("Item " + "Type " + "Title " + "No. of Players " + "Platform " + "Unit Price ");
- System.out.println("---- " + "---- " + "----- " + "-------------- " + "-------- " + "---------- ");
- System.out.println(padRight(getItemID(), 10) + "VG " + padRight(getTitle(), 20) + padRight(getNumberOfPlayers(), 16) + padRight(getPlatform(), 24) + padRight(defaultFormat.format(getUnitPrice()), 15));
- }
- @Override
- public void display(int quantity) {
- System.out.println("Item " + "Type " + "Title " + "No. of Players " + "Platform " + "Unit Price " + "Quantity " + "Cost ");
- System.out.println("---- " + "---- " + "----- " + "-------------- " + "-------- " + "---------- " + "-------- " + "---- ");
- System.out.println(padRight(getItemID(), 10) + "VG " + padRight(getTitle(), 20) + padRight(getNumberOfPlayers(), 16) + padRight(getPlatform(), 24) + padRight(defaultFormat.format(getUnitPrice()), 15) + padRight(quantity, 10) + padRight(defaultFormat.format((getUnitPrice() * quantity)), 6));
- }
- }
- class CD extends Item {
- private String m_artist;
- private int m_numberOfTracks;
- public CD(String itemID, String title, double unitPrice, String artist, int numberOfTracks) {
- setItemID(itemID);
- setTitle(title);
- setUnitPrice(unitPrice);
- setArtist(artist);
- setNumberOfTracks(numberOfTracks);
- }
- public void setArtist(String artist) {
- m_artist = artist;
- }
- public String getArtist() {
- return m_artist;
- }
- public void setNumberOfTracks(int numberOfTracks) {
- m_numberOfTracks = numberOfTracks;
- }
- public int getNumberOfTracks() {
- return m_numberOfTracks;
- }
- @Override
- public void display() {
- System.out.println("Displaying Item: " + getItemID());
- System.out.println("--------------------------------------------------------------------------------------------------------------");
- System.out.println("Item " + "Type " + "Title " + "Artist " + "No. of Tracks " + "Unit Price ");
- System.out.println("---- " + "---- " + "----- " + "------ " + "------------- " + "---------- ");
- System.out.println(padRight(getItemID(), 10) + "CD " + padRight(getTitle(), 20) + padRight(getArtist(), 25) + padRight(getNumberOfTracks(), 15) + padRight(defaultFormat.format(getUnitPrice()), 15));
- }
- @Override
- public void display(int quantity) {
- System.out.println("Item " + "Type " + "Title " + "Artist " + "No. of Tracks " + "Unit Price " + "Quantity " + "Cost ");
- System.out.println("---- " + "---- " + "----- " + "------ " + "------------- " + "---------- " + "-------- " + "---- ");
- System.out.println(padRight(getItemID(), 10) + "CD " + padRight(getTitle(), 20) + padRight(getArtist(), 25) + padRight(getNumberOfTracks(), 15) + padRight(defaultFormat.format(getUnitPrice()), 15) + padRight(quantity, 10) + padRight(defaultFormat.format((getUnitPrice() * quantity)), 6));
- }
- }
Add Comment
Please, Sign In to add comment