Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.ut.math.tvt.salessystem.dao;
- import ee.ut.math.tvt.salessystem.SalesSystemException;
- import ee.ut.math.tvt.salessystem.dataobjects.SoldItem;
- import ee.ut.math.tvt.salessystem.dataobjects.StockItem;
- import ee.ut.math.tvt.salessystem.dataobjects.PurchaseItem;
- import java.time.LocalDate;
- import java.util.ArrayList;
- import java.util.List;
- public class InMemorySalesSystemDAO implements SalesSystemDAO {
- private final List<StockItem> stockItemList;
- private final List<SoldItem> soldItemList;
- private final List<PurchaseItem> purchaseList;
- public InMemorySalesSystemDAO() {
- this.stockItemList = new ArrayList<StockItem>();
- this.soldItemList = new ArrayList<>();
- this.purchaseList = new ArrayList<>();
- }
- @Override
- public void beginTransaction() {
- }
- @Override
- public void rollbackTransaction() {
- }
- @Override
- public void commitTransaction() {
- }
- @Override
- public void addStockItem(String strBarcode, String name, String strPrice, String strQuantity) throws SalesSystemException{
- try {
- long id = Integer.parseInt(strBarcode);
- double price = Long.parseLong(strPrice);
- int quantity = Integer.parseInt(strQuantity);
- StockItem item = findStockItem(id);
- if (id < 1 || id > 1000)
- throw new SalesSystemException("Barcode is less than 1 or more than 1000!");
- if (name.length() < 3 || name.length() > 30)
- throw new SalesSystemException("Item name length is less than 3 or more than 30!");
- if (price < 0 || price > 100000)
- throw new SalesSystemException("Price must be between 0 to 100000!");
- if (quantity > 1000 || quantity < 0)
- throw new SalesSystemException("Quantity is less than 1 or more than 1000");
- //checks if item exists and if so, updates it's quantity
- if (item != null) {
- increaseStockItem(item, quantity);
- } else {
- StockItem newItem = new StockItem(id, name, "", price, quantity);
- saveStockItem(newItem);
- }
- }
- catch(SalesSystemException e) {
- throw new SalesSystemException(e.getMessage());
- }
- catch(Exception e) {
- throw new SalesSystemException("You entered invalid input!");
- }
- }
- @Override
- public void increaseStockItem(StockItem item, int quantity) {
- item.setQuantity(item.getQuantity() + quantity);
- }
- @Override
- public void decreaseStockItem(StockItem item, int quantity) {
- item.setQuantity(item.getQuantity() - quantity);
- }
- @Override
- public void saveStockItem(StockItem item) {
- stockItemList.add(item);
- }
- /**sold items*/
- @Override
- public void saveSoldItem(SoldItem item) {
- soldItemList.add(item);
- }
- @Override
- public List<SoldItem> getSoldItems() {
- return soldItemList;
- }
- @Override
- public void increaseSoldItem(SoldItem item, int quantity) {
- item.setQuantity(item.getQuantity() + quantity);
- }
- @Override
- public List<StockItem> findStockItems() {
- return stockItemList;
- }
- @Override
- public StockItem findStockItem(long id) {
- for (StockItem item : stockItemList) {
- if (item.getId() == id)
- return item;
- }
- return null;
- }
- /**purchase list*/
- @Override
- public void savePurchaseList(List<SoldItem> items) {
- purchaseList.add(new PurchaseItem(items));
- }
- @Override
- public List<PurchaseItem> getPurchaseList(LocalDate startDate, LocalDate endDate, int itemsAmount) {
- return purchaseList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement