public Double calcProfit(Trade trade, Double brokerFeeFactor) { Double profit = 0.0; if (reversedOrder != null) { Double totalTradeAmount = trade.getAmount() * brokerFeeFactor; Double tradeAmountRatio = trade.getAmount() / reversedOrder.getBrokerAmount(); Double bruttoAmountEquivalent = reversedOrder.getAmount() * tradeAmountRatio; Double feeDeduction = (bruttoAmountEquivalent - totalTradeAmount) * rate; if (type.equals("sell")) { Double sellPrice = totalTradeAmount * rate; Double buyPrice = bruttoAmountEquivalent * reversedOrder.getRate(); System.out.println( "sell: " + totalTradeAmount + "*" + rate + ", " + bruttoAmountEquivalent + "*" + reversedOrder.getRate()); profit = (sellPrice - buyPrice) - feeDeduction; System.out.println("profit=" + profit); } else if (type.equals("buy")) { Double buyPrice = bruttoAmountEquivalent * rate; Double sellPrice = totalTradeAmount * reversedOrder.getRate(); System.out.println( "buy: " + totalTradeAmount + "*" + rate + ", " + bruttoAmountEquivalent + "*" + reversedOrder.getRate()); profit = (sellPrice - buyPrice) - feeDeduction; System.out.println("profit=" + profit); } } return profit; }
@Override public void onClick(View v) { // 使用工厂方法模式和策略模式模拟交易 // 初始化一张IC卡 Card card = initIC(); // 显示一下卡内信息 System.out.println("========初始卡信息:========="); showCard(card); // 是否停止运行标志 boolean flag = true; while (flag) { Trade trade = createTrade(); DeductionFacade.deduct(card, trade); // 交易成功,打印出成功处理消息 System.out.println("\n======交易凭证========"); System.out.println(trade.getTradeNo() + " 交易成功!"); System.out.println("本次发生的交易金额为:" + trade.getAmount() / 100.0 + " 元"); // 展示一下卡内信息 showCard(card); System.out.print("\n是否需要退出?(Y/N)"); if (getInput().equalsIgnoreCase("y")) { flag = false; // 退出; } } }