Guest User

Untitled

a guest
May 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.69 KB | None | 0 0
  1. package hmb;
  2. import java.text.NumberFormat;
  3. import java.util.*;
  4.  
  5. public class Hmb {
  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", "5 Green St", 3000);
  12.  
  13. os.addCD("P001", "Get Back", 12.56, "Beatles", 8);
  14. os.addDVD("P002", "Plenty", 15.99, "Joe Doe");
  15. os.addVideoGame("P003", "Lara Croft", 18.00, 1, "PC");
  16.  
  17. os.addOrder(1, 100, "P001", 5);
  18. os.addOrder(3, 101, "P002", 50);
  19. os.addOrder(3, 102, "P001", 7);
  20.  
  21. os.displayOrder(101);
  22. os.displayOrder(102);
  23.  
  24. os.displayItem("P001");
  25. os.displayItem("P002");
  26. os.displayItem("P003");
  27. }
  28. }
  29.  
  30. class Customer {
  31. private int m_custID;
  32. private String m_name;
  33. private String m_address;
  34. private double m_creditRating;
  35. private LinkedList<Order> orders = new LinkedList<Order>();
  36.  
  37. public Customer(int custID, String name, String address, double creditRating) {
  38. setCustomerID(custID);
  39. setName(name);
  40. setAddress(address);
  41. setCreditRating(creditRating);
  42. }
  43.  
  44. public LinkedList<Order> getOrders() {
  45. return orders;
  46. }
  47.  
  48. public void addOrder(int orderID, String itemID, int quantity) {
  49. Order o = new Order(orderID, quantity, itemID);
  50. orders.add(o);
  51. System.out.println("Order: " + orderID + " added to Customer: " + getCustomerID());
  52. System.out.println();
  53. }
  54.  
  55. public void setCustomerID(int ID) {
  56. m_custID = ID;
  57. }
  58.  
  59. public int getCustomerID() {
  60. return m_custID;
  61. }
  62.  
  63. public void setName(String name) {
  64. m_name = name;
  65. }
  66.  
  67. public String getName() {
  68. return m_name;
  69. }
  70.  
  71. public void setAddress(String address) {
  72. m_address = address;
  73. }
  74.  
  75. public String getAddress() {
  76. return m_address;
  77. }
  78.  
  79. public void setCreditRating(double rating) {
  80. m_creditRating = rating;
  81. }
  82.  
  83. public double getCreditRating() {
  84. return m_creditRating;
  85. }
  86. }
  87.  
  88. class DVD extends Item {
  89. private String m_director;
  90.  
  91. public DVD(String itemID, String title, double unitPrice, String director) {
  92. setItemID(itemID);
  93. setTitle(title);
  94. setUnitPrice(unitPrice);
  95. setDirector(director);
  96. }
  97.  
  98. public void setDirector(String director) {
  99. m_director = director;
  100. }
  101.  
  102. public String getDirector() {
  103. return m_director;
  104. }
  105.  
  106. @Override
  107. public void display() {
  108. System.out.println("Displaying Item: " + getItemID());
  109. System.out.println("--------------------------------------------------------------------------------------------------------------");
  110. System.out.println("Item " + "Type " + "Title " + "Director " + "Unit Price ");
  111. System.out.println("---- " + "---- " + "----- " + "-------- " + "---------- ");
  112. System.out.println(padRight(getItemID(), 10) + "DVD " + padRight(getTitle(), 20) + padRight(getDirector(), 40) + padRight(defaultFormat.format(getUnitPrice()), 15));
  113. }
  114.  
  115. @Override
  116. public void display(int quantity) {
  117.  
  118. System.out.println("Item " + "Type " + "Title " + "Director " + "Unit Price " + "Quantity " + "Cost ");
  119. System.out.println("---- " + "---- " + "----- " + "-------- " + "---------- " + "-------- " + "---- ");
  120. 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));
  121. }
  122. }
  123.  
  124. abstract class Item {
  125. private String m_itemID;
  126. private String m_title;
  127. private double m_unitPrice;
  128. NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
  129.  
  130. public String padRight(String s, int l) {
  131. String output;
  132. output = s;
  133. for (int i = 0 ; i < l - s.length() ; i++) {
  134. output += " ";
  135. }
  136. return output;
  137. }
  138.  
  139. public String padRight(int n, int l) {
  140. String input = "" + n;
  141. String output = input;
  142. for (int i = 0 ; i < l - input.length() ; i++) {
  143. output += " ";
  144. }
  145. return output;
  146. }
  147.  
  148. public void setItemID(String ID) {
  149. m_itemID = ID;
  150. }
  151.  
  152. public String getItemID() {
  153. return m_itemID;
  154. }
  155.  
  156. public void setTitle(String title) {
  157. m_title = title;
  158. }
  159.  
  160. public String getTitle() {
  161. return m_title;
  162. }
  163.  
  164. public void setUnitPrice(double unitPrice) {
  165. m_unitPrice = unitPrice;
  166. }
  167.  
  168. public double getUnitPrice() {
  169. return m_unitPrice;
  170. }
  171.  
  172. public void display() {
  173. }
  174.  
  175. public void display(int quantity) {
  176. }
  177. }
  178.  
  179. class Order {
  180. private int m_orderID;
  181. private int m_quantity;
  182. private String m_itemID;
  183. private Date m_date;
  184.  
  185. public Order(int ID, int quantity, String itemID) {
  186. setOrderID(ID);
  187. setQuantity(quantity);
  188. setItemID(itemID);
  189. Date tempDate = new Date();
  190. setDate(tempDate);
  191. }
  192.  
  193. public void setOrderID(int ID) {
  194. m_orderID = ID;
  195. }
  196.  
  197. public int getOrderID() {
  198. return m_orderID;
  199. }
  200.  
  201. public void setQuantity(int quantity) {
  202. m_quantity = quantity;
  203. }
  204.  
  205. public int getQuantity() {
  206. return m_quantity;
  207. }
  208.  
  209. public void setItemID(String item) {
  210. m_itemID = item;
  211. }
  212.  
  213. public String getItemID() {
  214. return m_itemID;
  215. }
  216.  
  217. public void setDate(Date date) {
  218. m_date = date;
  219. }
  220.  
  221. public Date getDate() {
  222. return m_date;
  223. }
  224. }
  225.  
  226. class OrderSystem {
  227. private LinkedList<Customer> customers = new LinkedList<Customer>();
  228. private LinkedList<Item> items = new LinkedList<Item>();
  229. NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
  230.  
  231. public OrderSystem() {
  232. }
  233.  
  234. public int searchItems(String itemID) {
  235. int x = -1;
  236. for (Item currentItem : items) {
  237. if (currentItem.getItemID().equals(itemID)) {
  238. x = items.indexOf(currentItem);
  239. }
  240. }
  241. return x;
  242. }
  243.  
  244. public Customer searchCustomers(int customerID) {
  245. Customer x = new Customer(-1, "null", "null", -1);
  246. Customer currentCustomer;
  247. for (int i = 0 ; i < customers.size() ; i++) {
  248. currentCustomer = customers.get(i);
  249. if (currentCustomer.getCustomerID() == customerID) {
  250. x = currentCustomer;
  251. }
  252. }
  253. return x;
  254. }
  255.  
  256. public void addCustomer(int customerID, String name, String address, double credit) {
  257. Customer customer = new Customer(customerID, name, address, credit);
  258. customers.add(customer);
  259. System.out.println("Customer: " + customerID + " added.");
  260. System.out.println();
  261. }
  262.  
  263. //USE CASE: ADD A CD
  264. public void addCD(String itemID, String title, double unitPrice, String artist, int numberOfTracks) {
  265. items.add(new CD(itemID, title, unitPrice, artist, numberOfTracks));
  266. System.out.println("CD: " + itemID + " added.");
  267. System.out.println();
  268. }
  269.  
  270. //USE CASE: ADD A DVD
  271. public void addDVD(String itemID, String title, double unitPrice, String director) {
  272. items.add(new DVD(itemID, title, unitPrice, director));
  273. System.out.println("DVD: " + itemID + " added.");
  274. System.out.println();
  275. }
  276.  
  277. //USE CASE: ADD A VIDEO GAME
  278. public void addVideoGame(String itemID, String title, double unitPrice, int numberOfPlayers, String platform) {
  279. items.add(new VideoGame(itemID, title, unitPrice, numberOfPlayers, platform));
  280. System.out.println("Video Game: " + itemID + " added.");
  281. System.out.println();
  282. }
  283.  
  284. public void addOrder(int customerID, int orderID, String itemID, int quantity) {
  285. if (!searchCustomers(customerID).getName().equals("null")) {
  286. searchCustomers(customerID).addOrder(orderID, itemID, quantity);
  287. } else {
  288. System.out.println("Customer: " + customerID + " not found.");
  289. System.out.println();
  290. }
  291. }
  292.  
  293. public LinkedList<Order> getAllOrders() {
  294. LinkedList<Order> orders = new LinkedList<Order>();
  295. LinkedList<Order> allOrders = new LinkedList<Order>();
  296. for (Customer customer: customers) {
  297. orders = customer.getOrders();
  298. for (Order order : orders) {
  299. allOrders.add(order);
  300. }
  301. }
  302. return allOrders;
  303. }
  304.  
  305. public void displayAllOrders() {
  306. System.out.println("Displaying all orders");
  307. System.out.println("----------------------------------------------------------------------------------------------------------------------------------");
  308. for (Order order: getAllOrders()) {
  309. displayOrder(order.getOrderID());
  310. }
  311. System.out.println("----------------------------------------------------------------------------------------------------------------------------------");
  312. System.out.println();
  313. }
  314.  
  315. //USE CASE: DISPLAY DETAILS OF ITEM WITH A GIVEN ITEM ID
  316. public void displayItem(String itemID) {
  317. if (searchItems(itemID) != -1) {
  318. Item item = items.get(searchItems(itemID));
  319. item.display();
  320. System.out.println();
  321. } else {
  322. System.out.println("Item: " + itemID + " not found.");
  323. }
  324. }
  325.  
  326. public void deleteCustomer(int customerID) {
  327. if (!searchCustomers(customerID).getName().equals("null")) {
  328. System.out.println("Removing customer: " + customerID);
  329. customers.remove(searchCustomers(customerID));
  330. System.out.println("Customer removed");
  331. } else {
  332. System.out.println("Customer: " + customerID + " not found.");
  333. }
  334. System.out.println();
  335. }
  336.  
  337. public void displayOrder(int orderID) {
  338. Customer c = new Customer(-1, "null", "null", -1);
  339. Order o = new Order(-1, -1, "null");
  340. Boolean found = false;
  341. for (int i = 0 ; i < customers.size() ; i++) {
  342. LinkedList<Order> orders = customers.get(i).getOrders();
  343. for (int j = 0 ; j < orders.size() ; j++) {
  344. if (orders.get(j).getOrderID() == orderID) {
  345. c = customers.get(i);
  346. o = orders.get(j);
  347. found = true;
  348. }
  349. }
  350. }
  351. if (found) {
  352. System.out.println("Displaying Order: " + orderID);
  353. System.out.println("--------------------------------------------------------------------------------------------------------------");
  354. System.out.println("Customer ID: " + c.getCustomerID());
  355. System.out.println("Name: " + c.getName());
  356. System.out.println("Address: " + c.getAddress());
  357. System.out.println("Order ID: " + orderID);
  358. System.out.println("Order date: " + o.getDate());
  359. items.get(searchItems(o.getItemID())).display(o.getQuantity());
  360. } else {
  361. System.out.println("Order: " + orderID + " not found.");
  362. }
  363. System.out.println();
  364. }
  365. }
  366.  
  367. class VideoGame extends Item {
  368. private int m_numberOfPlayers;
  369. private String m_platform;
  370.  
  371. public VideoGame(String itemID, String title, double unitPrice, int numberOfPlayers, String platform) {
  372. setItemID(itemID);
  373. setTitle(title);
  374. setUnitPrice(unitPrice);
  375. setNumberOfPlayers(numberOfPlayers);
  376. setPlatform(platform);
  377. }
  378.  
  379. public void setNumberOfPlayers(int numberOfPlayers) {
  380. m_numberOfPlayers = numberOfPlayers;
  381. }
  382.  
  383. public int getNumberOfPlayers() {
  384. return m_numberOfPlayers;
  385. }
  386.  
  387. public void setPlatform(String platform) {
  388. m_platform = platform;
  389. }
  390.  
  391. public String getPlatform() {
  392. return m_platform;
  393. }
  394.  
  395. @Override
  396. public void display() {
  397. System.out.println("Displaying Item: " + getItemID());
  398. System.out.println("--------------------------------------------------------------------------------------------------------------");
  399. System.out.println("Item " + "Type " + "Title " + "No. of Players " + "Platform " + "Unit Price ");
  400. System.out.println("---- " + "---- " + "----- " + "-------------- " + "-------- " + "---------- ");
  401. System.out.println(padRight(getItemID(), 10) + "VG " + padRight(getTitle(), 20) + padRight(getNumberOfPlayers(), 16) + padRight(getPlatform(), 24) + padRight(defaultFormat.format(getUnitPrice()), 15));
  402. }
  403.  
  404. @Override
  405. public void display(int quantity) {
  406. System.out.println("Item " + "Type " + "Title " + "No. of Players " + "Platform " + "Unit Price " + "Quantity " + "Cost ");
  407. System.out.println("---- " + "---- " + "----- " + "-------------- " + "-------- " + "---------- " + "-------- " + "---- ");
  408. 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));
  409. }
  410. }
  411.  
  412. class CD extends Item {
  413. private String m_artist;
  414. private int m_numberOfTracks;
  415.  
  416. public CD(String itemID, String title, double unitPrice, String artist, int numberOfTracks) {
  417. setItemID(itemID);
  418. setTitle(title);
  419. setUnitPrice(unitPrice);
  420. setArtist(artist);
  421. setNumberOfTracks(numberOfTracks);
  422. }
  423.  
  424. public void setArtist(String artist) {
  425. m_artist = artist;
  426. }
  427.  
  428. public String getArtist() {
  429. return m_artist;
  430. }
  431.  
  432. public void setNumberOfTracks(int numberOfTracks) {
  433. m_numberOfTracks = numberOfTracks;
  434. }
  435.  
  436. public int getNumberOfTracks() {
  437. return m_numberOfTracks;
  438. }
  439.  
  440. @Override
  441. public void display() {
  442. System.out.println("Displaying Item: " + getItemID());
  443. System.out.println("--------------------------------------------------------------------------------------------------------------");
  444. System.out.println("Item " + "Type " + "Title " + "Artist " + "No. of Tracks " + "Unit Price ");
  445. System.out.println("---- " + "---- " + "----- " + "------ " + "------------- " + "---------- ");
  446. System.out.println(padRight(getItemID(), 10) + "CD " + padRight(getTitle(), 20) + padRight(getArtist(), 25) + padRight(getNumberOfTracks(), 15) + padRight(defaultFormat.format(getUnitPrice()), 15));
  447. }
  448.  
  449. @Override
  450. public void display(int quantity) {
  451.  
  452. System.out.println("Item " + "Type " + "Title " + "Artist " + "No. of Tracks " + "Unit Price " + "Quantity " + "Cost ");
  453. System.out.println("---- " + "---- " + "----- " + "------ " + "------------- " + "---------- " + "-------- " + "---- ");
  454. 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));
  455. }
  456. }
Add Comment
Please, Sign In to add comment