Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package ee.ut.math.tvt.salessystem.dao;
  2.  
  3. import ee.ut.math.tvt.salessystem.SalesSystemException;
  4. import ee.ut.math.tvt.salessystem.dataobjects.SoldItem;
  5. import ee.ut.math.tvt.salessystem.dataobjects.StockItem;
  6. import ee.ut.math.tvt.salessystem.dataobjects.PurchaseItem;
  7.  
  8. import java.time.LocalDate;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class InMemorySalesSystemDAO implements SalesSystemDAO {
  13.  
  14. private final List<StockItem> stockItemList;
  15. private final List<SoldItem> soldItemList;
  16. private final List<PurchaseItem> purchaseList;
  17.  
  18.  
  19. public InMemorySalesSystemDAO() {
  20. this.stockItemList = new ArrayList<StockItem>();
  21. this.soldItemList = new ArrayList<>();
  22. this.purchaseList = new ArrayList<>();
  23. }
  24.  
  25. @Override
  26. public void beginTransaction() {
  27. }
  28.  
  29. @Override
  30. public void rollbackTransaction() {
  31. }
  32.  
  33. @Override
  34. public void commitTransaction() {
  35. }
  36.  
  37. @Override
  38. public void addStockItem(String strBarcode, String name, String strPrice, String strQuantity) throws SalesSystemException{
  39. try {
  40. long id = Integer.parseInt(strBarcode);
  41. double price = Long.parseLong(strPrice);
  42. int quantity = Integer.parseInt(strQuantity);
  43.  
  44. StockItem item = findStockItem(id);
  45. if (id < 1 || id > 1000)
  46. throw new SalesSystemException("Barcode is less than 1 or more than 1000!");
  47. if (name.length() < 3 || name.length() > 30)
  48. throw new SalesSystemException("Item name length is less than 3 or more than 30!");
  49. if (price < 0 || price > 100000)
  50. throw new SalesSystemException("Price must be between 0 to 100000!");
  51. if (quantity > 1000 || quantity < 0)
  52. throw new SalesSystemException("Quantity is less than 1 or more than 1000");
  53.  
  54. //checks if item exists and if so, updates it's quantity
  55. if (item != null) {
  56. increaseStockItem(item, quantity);
  57. } else {
  58. StockItem newItem = new StockItem(id, name, "", price, quantity);
  59. saveStockItem(newItem);
  60. }
  61. }
  62. catch(SalesSystemException e) {
  63. throw new SalesSystemException(e.getMessage());
  64. }
  65. catch(Exception e) {
  66. throw new SalesSystemException("You entered invalid input!");
  67. }
  68. }
  69.  
  70. @Override
  71. public void increaseStockItem(StockItem item, int quantity) {
  72. item.setQuantity(item.getQuantity() + quantity);
  73. }
  74.  
  75. @Override
  76. public void decreaseStockItem(StockItem item, int quantity) {
  77. item.setQuantity(item.getQuantity() - quantity);
  78. }
  79.  
  80. @Override
  81. public void saveStockItem(StockItem item) {
  82. stockItemList.add(item);
  83. }
  84.  
  85. /**sold items*/
  86. @Override
  87. public void saveSoldItem(SoldItem item) {
  88. soldItemList.add(item);
  89. }
  90.  
  91. @Override
  92. public List<SoldItem> getSoldItems() {
  93. return soldItemList;
  94. }
  95.  
  96.  
  97. @Override
  98. public void increaseSoldItem(SoldItem item, int quantity) {
  99. item.setQuantity(item.getQuantity() + quantity);
  100. }
  101.  
  102.  
  103. @Override
  104. public List<StockItem> findStockItems() {
  105. return stockItemList;
  106. }
  107.  
  108. @Override
  109. public StockItem findStockItem(long id) {
  110. for (StockItem item : stockItemList) {
  111. if (item.getId() == id)
  112. return item;
  113. }
  114. return null;
  115. }
  116.  
  117. /**purchase list*/
  118. @Override
  119. public void savePurchaseList(List<SoldItem> items) {
  120. purchaseList.add(new PurchaseItem(items));
  121. }
  122.  
  123. @Override
  124. public List<PurchaseItem> getPurchaseList(LocalDate startDate, LocalDate endDate, int itemsAmount) {
  125. return purchaseList;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement