/** * Exchange money calculator * * @param from * @param to * @param amount * @return */ public String exchange(String from, String to, double amount) { JSONObject json = new JSONObject(); try { json.put("success", true); json.put("result", converter.convert(from, to, amount).toString()); } catch (JSONException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } return json.toString(); }
/** * Transfer money from current to another card * * @param destCardId * @param amount * @param r * @return result in JSON format */ @Transactional public String transferMoney(Integer destCardId, double amount, HttpServletRequest r) { Card toCard = cardService.getCard(destCardId); double currentAmount = currentCard.getAmount(); JSONObject json = new JSONObject(); if (currentAmount < amount) { try { json.put("sucess", false); json.put("result", "Недостаточно средств."); } catch (JSONException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } else { currentCard.setAmount(currentAmount - amount); double newAmount = converter.convert(currentCard.getCurrency(), toCard.getCurrency(), amount); toCard.setAmount(toCard.getAmount() + newAmount); Invoice invoice = currentCard.createInvoice(); invoice.setToCard(destCardId); invoice.setAmount(newAmount); String date = new SimpleDateFormat("dd.MM.yyyy-hh.mm-a") .format(GregorianCalendar.getInstance().getTime()); invoice.setDate(date); invoice.setCurrency(toCard.getCurrency()); invoiceService.addInvoice(invoice); try { json.put("sucess", true); json.put("result", "Успешно."); } catch (JSONException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } cardService.update(currentCard); r.getSession().setAttribute("Card", currentCard); cardService.update(toCard); return json.toString(); }