/** * Pay * * @param amount * @param to * @param r * @return */ public String pay(Double amount, Integer to, HttpServletRequest r) { JSONObject json = new JSONObject(); double currentAmount = currentCard.getAmount(); if (currentAmount < amount) { try { json.put("sucess", false); json.put("result", "No enough money.Choose anothe opperation."); } catch (JSONException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } else { currentCard.setAmount(currentAmount - amount); Invoice invoice = currentCard.createInvoice(); invoice.setToCard(to); invoice.setAmount(amount); String date = new SimpleDateFormat("dd.MM.yyyy-hh.mm-a") .format(GregorianCalendar.getInstance().getTime()); invoice.setDate(date); invoice.setCurrency(currentCard.getCurrency()); invoiceService.addInvoice(invoice); try { json.put("success", true); } catch (JSONException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } cardService.update(currentCard); r.getSession().setAttribute("Card", currentCard); return json.toString(); }
/** * @param request * @return if user is authorized redirect to operations page else redirect to index */ public String opperations(HttpServletRequest request) { try { this.currentCard = (Card) request.getSession().getAttribute("Card"); this.transferCards = utils.getTransfer(cardService.getCards(), currentCard.getId()); } catch (Exception e) { return "redirect:/"; } return "redirect:bank/operations"; }
/** * 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(); }