Guest User

Untitled

a guest
May 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. package ordersystem;
  2. import java.text.NumberFormat;
  3. import java.util.*;
  4.  
  5. public class OrderSystem {
  6. public static void main(String args[]) {
  7. OrderSystem os = new OrderSystem();
  8.  
  9. os.addCustomer(1, "Bill", "3 Red St", 1000);
  10. os.addCustomer(2, "Joe", "4 Blue St", 3000);
  11. os.addCustomer(3, "Dave", "4 Blue St", 3000);
  12.  
  13. os.addItem("P001", "Widget", 12.56);
  14. os.addItem("P002", "Sprocket", 15.99);
  15.  
  16. os.addOrder(1, 100, "P001", 5);
  17. os.addOrder(3, 101, "P002", 50);
  18. os.addOrder(3, 102, "P001", 7);
  19.  
  20. os.displayOrder(100);
  21. os.displayOrder(102);
  22.  
  23. os.displayAllOrders();
  24. os.displayItem("P002");
  25. os.deleteCustomer(3);
  26.  
  27. }
  28. }
  29.  
  30. class OrderSystem {
  31. private LinkedList<Customer> customers = new LinkedList<Customer>();
  32. private LinkedList<Item> items = new LinkedList<Item>();
  33. NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
  34.  
  35. public OrderSystem() {
  36.  
  37. }
  38.  
  39. private String padRight(String s, int l) {
  40. String output;
  41. output = s;
  42. for (int i = 0 ; i < l - s.length() ; i++) {
  43. output += " ";
  44. }
  45. return output;
  46. }
  47.  
  48. private String padIntRight(int n, int l) {
  49. String input = "" + n;
  50. String output;
  51. output = "" + input;
  52. for (int i = 0 ; i < l - input.length() ; i++) {
  53. output += " ";
  54. }
  55. return output;
  56. }
  57.  
  58. private String padDoubleRight(double n, int l) {
  59. String input = "" + n;
  60. String output;
  61. output = "" + input;
  62. for (int i = 0 ; i < l - input.length() ; i++) {
  63. output += " ";
  64. }
  65. return output;
  66. }
  67.  
  68.  
  69.  
  70. public Item searchItems(String itemID) {
  71. Item x = new Item("null", "null", -1);
  72. Item currentItem;
  73. for (int i = 0 ; i < items.size() ; i++) {
  74. currentItem = items.get(i);
  75. if (currentItem.getItemID().equals(itemID)) {
  76. x = currentItem;
  77. }
  78. }
  79. return x;
  80. }
  81.  
  82. //public int searchItems(String itemID) {
  83. // int x = -1;
  84. // for (Item currentItem : items) {
  85. // if (currentItem.getItemID().equals(itemID)) {
  86. // x = items.indexOf(currentItem);
  87. // }
  88. // }
  89. // return x;
  90. //}
  91.  
  92. public Customer searchCustomers(int customerID) {
  93. Customer x = new Customer(-1, "null", "null", -1);
  94. Customer currentCustomer;
  95. for (int i = 0 ; i < customers.size() ; i++) {
  96. currentCustomer = customers.get(i);
  97. if (currentCustomer.getCustomerID() == customerID) {
  98. x = currentCustomer;
  99. }
  100. }
  101. return x;
  102. }
  103.  
  104. public void addCustomer(int customerID, String name, String address, double credit) {
  105. Customer customer = new Customer(customerID, name, address, credit);
  106. customers.add(customer);
  107. System.out.println("Customer: " + customerID + " added.");
  108. System.out.println();
  109. }
  110.  
  111. public void addItem(String itemID, String description, double unitPrice) {
  112. Item item = new Item(itemID, description, unitPrice);
  113. items.add(item);
  114. System.out.println("Item: " + itemID + " added.");
  115. System.out.println();
  116. }
  117.  
  118. public void addOrder(int customerID, int orderID, String itemID, int quantity) {
  119. if (!searchCustomers(customerID).getName().equals("null")) {
  120. searchCustomers(customerID).addOrder(orderID, itemID, quantity);
  121. } else {
  122. System.out.println("Customer: " + customerID + " not found.");
  123. System.out.println();
  124. }
  125. }
  126.  
  127. public LinkedList<Order> getAllOrders() {
  128. LinkedList<Order> orders = new LinkedList<Order>();
  129. LinkedList<Order> allOrders = new LinkedList<Order>();
  130. System.out.println("Displaying all Orders: ");
  131. for (Customer customer: customers) {
  132. orders = customer.getOrders();
  133. for (Order order : orders) {
  134. allOrders.add(order);
  135. }
  136. }
  137. return allOrders;
  138. }
  139.  
  140. public void displayAllOrders() {
  141. for (Order order: getAllOrders()) {
  142. displayOrder(order.getOrderID());
  143. }
  144. }
  145.  
  146. public void displayItem(String itemID) {
  147. if (!searchItems(itemID).getDescription().equals("null")) {
  148. //if (!items.get(searchItems(itemID)).getDescription().equals("null")) {
  149. //Item item = items.get(searchItems(itemID));
  150. Item item = searchItems(itemID);
  151. System.out.println("Item ID: " + item.getItemID());
  152. System.out.println("Description: " + item.getDescription());
  153. System.out.println("Unit Price: " + defaultFormat.format(item.getUnitPrice()));
  154. System.out.println();
  155. } else {
  156. System.out.println("Item: " + itemID + " not found.");
  157. }
  158. }
  159.  
  160. public void deleteCustomer(int customerID) {
  161. if (!searchCustomers(customerID).getName().equals("null")) {
  162. System.out.println("Removing customer: " + customerID);
  163. customers.remove(searchCustomers(customerID));
  164. System.out.println("Customer removed");
  165. } else {
  166. System.out.println("Customer: " + customerID + " not found.");
  167. }
  168. System.out.println();
  169. }
  170.  
  171. public void displayOrder(int orderID) {
  172. Customer c = new Customer(-1, "null", "null", -1);
  173. Item item = new Item("null", "null", -1);
  174. Order o = new Order(-1, -1, "null");
  175. Boolean found = false;
  176. for (int i = 0 ; i < customers.size() ; i++) {
  177. LinkedList<Order> orders = customers.get(i).getOrders();
  178. for (int j = 0 ; j < orders.size() ; j++) {
  179. if (orders.get(j).getOrderID() == orderID) {
  180. c = customers.get(i);
  181. o = orders.get(j);
  182. item = searchItems(orders.get(j).getItemID());
  183. //item = items.get(searchItems(orders.get(j).getItemID()));
  184. found = true;
  185. }
  186. }
  187. }
  188. if (found) {
  189. System.out.println("Customer ID: " + c.getCustomerID());
  190. System.out.println("Name: " + c.getName());
  191. System.out.println("Address: " + c.getAddress());
  192. System.out.println("Order ID: " + orderID);
  193. System.out.println("Order date: " + o.getDate());
  194. System.out.println(padRight("Item", 10) + padRight("Description", 20) + padRight("Unit Price", 15) + padRight("Quantity", 10) + padRight("Cost", 6));
  195. System.out.println("---- ----------- ---------- -------- ---- ");
  196. 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));
  197. } else {
  198. System.out.println("Order: " + orderID + " not found.");
  199. }
  200. System.out.println();
  201. }
  202. }
  203.  
  204. class Order {
  205. private int m_orderID;
  206. private int m_quantity;
  207. private String m_itemID;
  208. private Date m_date;
  209.  
  210. public Order(int ID, int quantity, String itemID) {
  211. setOrderID(ID);
  212. setQuantity(quantity);
  213. setItemID(itemID);
  214. Date tempDate = new Date();
  215. setDate(tempDate);
  216. }
  217.  
  218. public void setOrderID(int ID) {
  219. m_orderID = ID;
  220. }
  221.  
  222. public int getOrderID() {
  223. return m_orderID;
  224. }
  225.  
  226. public void setQuantity(int quantity) {
  227. m_quantity = quantity;
  228. }
  229.  
  230. public int getQuantity() {
  231. return m_quantity;
  232. }
  233.  
  234. public void setItemID(String item) {
  235. m_itemID = item;
  236. }
  237.  
  238. public String getItemID() {
  239. return m_itemID;
  240. }
  241.  
  242. public void setDate(Date date) {
  243. m_date = date;
  244. }
  245.  
  246. public Date getDate() {
  247. return m_date;
  248. }
  249. }
  250.  
  251. class Customer {
  252. private int m_custID;
  253. private String m_name;
  254. private String m_address;
  255. private double m_creditRating;
  256. private LinkedList<Order> orders = new LinkedList<Order>();
  257.  
  258. public Customer(int custID, String name, String address, double creditRating) {
  259. setCustomerID(custID);
  260. setName(name);
  261. setAddress(address);
  262. setCreditRating(creditRating);
  263. }
  264.  
  265. public LinkedList<Order> getOrders() {
  266. return orders;
  267. }
  268.  
  269. public void addOrder(int orderID, String itemID, int quantity) {
  270. Order o = new Order(orderID, quantity, itemID);
  271. orders.add(o);
  272. System.out.println("Order: " + orderID + " added to Customer: " + getCustomerID());
  273. System.out.println();
  274. }
  275.  
  276. public void setCustomerID(int ID) {
  277. m_custID = ID;
  278. }
  279.  
  280. public int getCustomerID() {
  281. return m_custID;
  282. }
  283.  
  284. public void setName(String name) {
  285. m_name = name;
  286. }
  287.  
  288. public String getName() {
  289. return m_name;
  290. }
  291.  
  292. public void setAddress(String address) {
  293. m_address = address;
  294. }
  295.  
  296. public String getAddress() {
  297. return m_address;
  298. }
  299.  
  300. public void setCreditRating(double rating) {
  301. m_creditRating = rating;
  302. }
  303.  
  304. public double getCreditRating() {
  305. return m_creditRating;
  306. }
  307. }
  308.  
  309. class Item {
  310. private String m_itemID;
  311. private String m_description;
  312. private double m_unitPrice;
  313.  
  314. public Item(String ID, String description, double unitPrice) {
  315. setItemID(ID);
  316. setDescription(description);
  317. setUnitPrice(unitPrice);
  318. }
  319.  
  320. public void setItemID(String ID) {
  321. m_itemID = ID;
  322. }
  323.  
  324. public String getItemID() {
  325. return m_itemID;
  326. }
  327.  
  328. public void setDescription(String description) {
  329. m_description = description;
  330. }
  331.  
  332. public String getDescription() {
  333. return m_description;
  334. }
  335.  
  336. public void setUnitPrice(double unitPrice) {
  337. m_unitPrice = unitPrice;
  338. }
  339.  
  340. public double getUnitPrice() {
  341. return m_unitPrice;
  342. }
  343. }
Add Comment
Please, Sign In to add comment