@Override public OrderAck submitOrder(Order order) { // Create an order ack OrderAck orderAck = new OrderAck().setOrderId(order.getOrderId()); // Check the inventory try { Item orderItem = _inventory.lookupItem(order.getItemId()); // Check quantity on hand and generate the ack if (orderItem.getQuantity() >= order.getQuantity()) { orderAck.setAccepted(true).setStatus("Order Accepted"); // $NON-NLS-1$ } else { orderAck.setAccepted(false).setStatus("Insufficient Quantity"); // $NON-NLS-1$ } } catch (ItemNotFoundException infEx) { orderAck.setAccepted(false).setStatus("Item Not Available"); // $NON-NLS-1$ } return orderAck; }
private void attemptTrade( OrderResult result, IUserBalance balanceService, IStockInfo stockService, Date date, double dayLow, double dayHigh, int volume) { Order order = result.getOrder(); float quantity = order.getQuantity(); if (quantity > volume) { return; } try { if (order.getAction() == OrderAction.BUY) { if (order.getLimitPrice() < dayLow) { // price was too high to buy today return; } double orderTotal = quantity * dayLow; double accountBalance = balanceService.getBalance(order.getUserName()); if (accountBalance <= orderTotal) { quantity = (int) (accountBalance / dayLow); orderTotal = quantity * dayLow; result.setNote("Reduced quantity from " + order.getQuantity() + " to " + quantity); } if (quantity > 0 && accountBalance >= orderTotal) { try { balanceService.updateBalance(order.getUserName(), accountBalance - orderTotal); result.setCompleted(true); result.setQuantity(quantity); result.setSharePrice(dayLow); result.setDateTime(date); } catch (Exception ex) { // ignore } } else { // account balance is zero or not enough to purchase even one share result.setCancelled(true); result.setDateTime(date); result.setNote("Insufficient funds"); } } else { if (order.getLimitPrice() > dayHigh) { // price was too low to sell today return; } double orderTotal = quantity * dayHigh; double accountBalance = balanceService.getBalance(order.getUserName()); try { balanceService.updateBalance(order.getUserName(), accountBalance + orderTotal); result.setCompleted(true); result.setQuantity(quantity); result.setSharePrice(dayHigh); result.setDateTime(date); } catch (Exception ex) { // ignore } } } finally { // these apply on the first historical trade attempt (should be the same day the order was // submitted) if (!result.getCompleted() && !result.getCancelled()) { if ((order.getTimeInForce() == TimeInForce.DAY || order.getTimeInForce() == TimeInForce.FILLORKILL) && (today.after(date) || (today.equals(date) && !stockService.isWithinTradingHours()))) { // Cancel order result.setCancelled(true); result.setDateTime(date); result.setNote("Market closed"); } if (order.getTimeInForce() == TimeInForce.IMMEDIATEORCANCEL) { // Cancel order result.setCancelled(true); result.setDateTime(date); result.setNote("Unable to fill immediately"); } if (order.getTimeInForce() == TimeInForce.GOODUNTILCANCELED) { long diffInDays = (date.getTime() - order.getDateSubmitted().getTime()) / (1000 * 60 * 60 * 24); if (diffInDays > 120) { result.setCancelled(true); result.setDateTime(date); result.setNote("120 day limit reached"); } } } } }
public void finishOrder(ActionEvent actionEvent) { StringBuilder orderString = new StringBuilder(); // create new alert Alert alert = new Alert(Alert.AlertType.INFORMATION, "", ButtonType.OK); // get date Date date = new Date(); DateFormat format1 = new SimpleDateFormat("MM/dd/yy hh:mm:ss a z"); DateFormat format2 = new SimpleDateFormat("yyMMddhhmmss"); orderString.append( String.format( "%s\n\nNumber of line items:%d\n\nItem#/ID/Title/Price/Qty/Disc%%/Subtotal:\n\n%s\n\nOrder Subtotal:$%.2f\n\n" + "Tax Rate:\t6%%\n\nTax Amount:$%.2f\n\nOrder Total:$%.2f\n\nThanks for shopping!", format1.format(date), myOrders.size(), orderArrayToString(), Order.getRunningTotal(), Order.getRunningTotal() * .06, Order.getRunningTotal() + Order.getRunningTotal() * .06)); // print order array // print subtotal // print 6% tax // add tax and generate new total // show alert // call new order alert.setHeaderText("Order Receipt"); alert.setResizable(true); alert.getDialogPane().setPrefWidth(alert.getDialogPane().getWidth() * 2); alert.setContentText(orderString.toString()); alert.show(); // output transactions.txt OpenOption[] options = { StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND }; Path file = Paths.get("transaction.txt"); try { BufferedWriter writer = Files.newBufferedWriter(file, Charset.defaultCharset(), options); int i = 0; for (Order order : myOrders) { // String[] strArray = order.toString().split(" "); // System.out.println(strArray[0] + strArray[1]); writer.write( String.format( "%s, %s, %s, %s, %s, %s, %s, %s\n", format2.format(date), order.getOrderedBook().getBookID(), order.getOrderedBook().getBookTitle(), order.getOrderedBook().getPrice(), order.getQuantity(), order.getDiscount(), order.getFinalPrice(), format1.format(date))); } writer.close(); } catch (IOException e) { System.out.println("OOPS"); e.printStackTrace(); } newOrder(new ActionEvent()); }