public void action() { try { ContentElement content = getContentManager().extractContent(request); CreateAccount ca = (CreateAccount) ((Action) content).getAction(); Account acc = new Account(); String id = generateId(); acc.setId(id); acc.setName(ca.getName()); Result result = new Result((Action) content, acc); ACLMessage reply = request.createReply(); reply.setPerformative(ACLMessage.INFORM); getContentManager().fillContent(reply, result); send(reply); accounts.put(id, acc); operations.put(id, new ArrayList()); System.out.println("Account [" + acc.getName() + " # " + acc.getId() + "] created!"); } catch (Exception ex) { ex.printStackTrace(); } }
Object processOperation(MakeOperation mo) { // ------------------------------------------- Account acc = (Account) accounts.get(mo.getAccountId()); if (acc == null) return newProblem(ACCOUNT_NOT_FOUND); if (mo.getAmount() <= 0) return newProblem(ILLEGAL_OPERATION); if (mo.getType() != DEPOSIT && mo.getType() != WITHDRAWAL) return null; if (mo.getType() == DEPOSIT) acc.setBalance(acc.getBalance() + mo.getAmount()); else if (mo.getType() == WITHDRAWAL) { if (mo.getAmount() > acc.getBalance()) return newProblem(NOT_ENOUGH_MONEY); acc.setBalance(acc.getBalance() - mo.getAmount()); } Operation op = new Operation(); op.setType(mo.getType()); op.setAmount(mo.getAmount()); op.setAccountId(acc.getId()); op.setDate(new java.util.Date()); List l = (List) operations.get(acc.getId()); l.add(op); operations.put(acc.getId(), l); return acc; }
Object processInformation(Information info) { // ------------------------------------------- Account acc = (Account) accounts.get(info.getAccountId()); if (acc == null) return newProblem(ACCOUNT_NOT_FOUND); java.util.Date date = new java.util.Date(); Operation op = new Operation(); // <-- Apply admin charge op.setType(ADMIN); op.setAmount(info.getType() == BALANCE ? BAL_CHARGE : OPER_CHARGE); acc.setBalance(acc.getBalance() - op.getAmount()); op.setBalance(acc.getBalance()); op.setAccountId(acc.getId()); op.setDate(date); List l = (List) operations.get(acc.getId()); l.add(op); operations.put(acc.getId(), l); if (info.getType() == BALANCE) return acc; if (info.getType() == OPERATIONS) return l; return null; }