/** Returns the total sum that needs to be paid for all the items in the basket. */ public double getTotalPrice() { double price = 0.0; for (SoldItem item : getRows()) { price += item.getSum(); } return price; }
public SoldItem getForStockItem(long stockItemId) { for (SoldItem item : getRows()) { if (item.getStockItem().getId().equals(stockItemId)) { return item; } } return null; }
@Override protected Object getColumnValue(SoldItem item, int columnIndex) { switch (columnIndex) { case 0: return item.getId(); case 1: return item.getName(); case 2: return item.getPrice(); case 3: return item.getQuantity(); case 4: return item.getSum(); } throw new IllegalArgumentException("Column index out of range"); }
@Override public String toString() { final StringBuffer buffer = new StringBuffer(); for (int i = 0; i < headers.length; i++) buffer.append(headers[i] + "\t"); buffer.append("\n"); for (final SoldItem item : getRows()) { buffer.append(item.getId() + "\t"); buffer.append(item.getName() + "\t"); buffer.append(item.getPrice() + "\t"); buffer.append(item.getQuantity() + "\t"); buffer.append(item.getSum() + "\t"); buffer.append("\n"); } return buffer.toString(); }
@Test public void testGetSumWithZeroQuantity() { sold_item = new SoldItem(stock_item, 0); assertEquals(sold_item.getSum(), 0, 0.0001); }
@Test public void testGetSum() { sold_item = new SoldItem(stock_item, 10); assertEquals(sold_item.getSum(), (price * 10), 0.0001); }
/** Add new StockItem to table. */ public void addItem(final SoldItem soldItem) throws SalesSystemException { StockItem stockItem = soldItem.getStockItem(); long stockItemId = stockItem.getId(); SoldItem existingItem = getForStockItem(stockItemId); if (existingItem != null) { int totalQuantity = existingItem.getQuantity() + soldItem.getQuantity(); validateQuantityInStock(stockItem, totalQuantity); existingItem.setQuantity(totalQuantity); log.debug( "Found existing item " + soldItem.getName() + " increased quantity by " + soldItem.getQuantity()); } else { validateQuantityInStock(soldItem.getStockItem(), soldItem.getQuantity()); getRows().add(soldItem); log.debug("Added " + soldItem.getName() + " quantity of " + soldItem.getQuantity()); } fireTableDataChanged(); }