Skip to content
Snippets Groups Projects
Commit 1d167e11 authored by mariaeloi's avatar mariaeloi
Browse files

adiciona operação de transferência

parent 6d89f66f
No related branches found
No related tags found
1 merge request!2Adiciona operações de saque e transferência
Showing
with 187 additions and 8 deletions
......@@ -5,6 +5,7 @@ import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Account;
import ufrn.imd.domain.Client;
import ufrn.imd.utils.message.DepositMessage;
import ufrn.imd.utils.message.TransferMessage;
import ufrn.imd.utils.message.WithdrawalMessage;
import java.net.MalformedURLException;
......@@ -13,9 +14,7 @@ import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;
import static ufrn.imd.utils.NameSpace.HOST_BALANCE_SERVICE;
import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;
import static ufrn.imd.utils.NameSpace.HOST_WITHDRAWAL_SERVICE;
import static ufrn.imd.utils.NameSpace.*;
@Data
public class MachineState {
......@@ -25,6 +24,7 @@ public class MachineState {
public static final String DEPOSIT = "DEPOSIT";
public static final String BALANCE = "BALANCE";
public static final String WITHDRAWAL = "WITHDRAWAL";
public static final String TRANSFER = "TRANSFER";
public static final String START = "START";
public static final String INVALID_INPUT = "INVALID_INPUT";
......@@ -59,6 +59,8 @@ public class MachineState {
state = START;
} else if(state.equals(WITHDRAWAL)) {
state = START;
} else if(state.equals(TRANSFER)) {
state = START;
}
}
......@@ -69,7 +71,8 @@ public class MachineState {
!inputValue.equals(QUIT) &&
!inputValue.equals(DEPOSIT) &&
!inputValue.equals(BALANCE) &&
!inputValue.equals(WITHDRAWAL)) {
!inputValue.equals(WITHDRAWAL) &&
!inputValue.equals(TRANSFER)) {
inputValue = INVALID_INPUT;
}
} else if(state.equals(DEPOSIT)) {
......@@ -124,6 +127,30 @@ public class MachineState {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} else if(state.equals(TRANSFER)) {
String accountNumber = sc.nextLine();
System.out.println(">>> Please enter with the value that you want transfer");
String v = sc.nextLine();
Client client = new Client(1);
Account account = new Account(null, accountNumber, null);
TransferMessage transferMessage = new TransferMessage(client, account, Double.parseDouble(v));
Controller server = (Controller) Naming.lookup(HOST_TRANSFER_SERVICE);
ServerResponse response = server.getResponse(transferMessage);
if(response.getStatus() == 200) {
System.out.println(
String.format(">>> Your balance is: %.2f", ((Account)response.getBody()).getBalance())
);
} else if(response.getStatus() == 500) {
System.out.println(
String.format(">>> %s", response.getMessage())
);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
......@@ -135,9 +162,9 @@ public class MachineState {
} else if(state.equals(START)) {
System.out.println("These are the avaliable operations");
System.out.println("---------------------------------");
System.out.println(" - deposit \t - balance");
System.out.println(" - withdrawal \t - quit");
System.out.println(" - deposit \t\t - balance");
System.out.println(" - withdrawal \t - transfer");
System.out.println(" - quit");
System.out.println("---------------------------------");
Thread.sleep(1000);
} else if(state.equals(READ)) {
......@@ -151,6 +178,9 @@ public class MachineState {
} else if(state.equals(WITHDRAWAL)) {
System.out.println(">>> Starting withdrawal operation");
System.out.println(">>> Please enter the value you want to withdraw");
} else if(state.equals(TRANSFER)) {
System.out.println(">>> Starting transfer operation");
System.out.println(">>> Please enter with the recipient's account number");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
......
......@@ -4,7 +4,9 @@ public class NameSpace {
public final static Integer PORT_WITHDRAWAL_SERVICE = 1906;
public static final Integer PORT_DEPOSIT_SERVICE = 1907;
public static final Integer PORT_BALANCE_SERVICE = 1908;
public static final Integer PORT_TRANSFER_SERVICE = 1909;
public final static String HOST_WITHDRAWAL_SERVICE = String.format("rmi://127.0.0.1:%d/%s", PORT_WITHDRAWAL_SERVICE, "withdrawal");
public static final String HOST_DEPOSIT_SERVICE = String.format("rmi://127.0.0.1:%d/%s", PORT_DEPOSIT_SERVICE, "deposit");
public static final String HOST_BALANCE_SERVICE = String.format("rmi://127.0.0.1:%d/%s", PORT_BALANCE_SERVICE, "balance");
public final static String HOST_TRANSFER_SERVICE = String.format("rmi://127.0.0.1:%d/%s", PORT_TRANSFER_SERVICE, "transfer");
}
package ufrn.imd.utils.message;
import lombok.AllArgsConstructor;
import lombok.Data;
import ufrn.imd.domain.Account;
import ufrn.imd.domain.Client;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class TransferMessage implements Serializable {
private static final long serialVersionUID = 2L;
private Client from;
private Account to;
private Double value;
}
......@@ -3,10 +3,12 @@ package ufrn.imd;
import ufrn.imd.controller.Controller;
import ufrn.imd.controller.impl.BalanceControllerImpl;
import ufrn.imd.controller.impl.DepositControllerImpl;
import ufrn.imd.controller.impl.TransferControllerImpl;
import ufrn.imd.controller.impl.WithdrawalControllerImpl;
import ufrn.imd.repository.impl.ClientRepository;
import ufrn.imd.service.impl.BalanceServiceImpl;
import ufrn.imd.service.impl.DepositServiceImpl;
import ufrn.imd.service.impl.TransferServiceImpl;
import ufrn.imd.service.impl.WithdrawalServiceImpl;
import java.net.MalformedURLException;
......@@ -19,9 +21,11 @@ public class BankingServerApp {
DepositServiceImpl depositService = new DepositServiceImpl(clientRepository);
BalanceServiceImpl balanceService = new BalanceServiceImpl(clientRepository);
WithdrawalServiceImpl withdrawalService = new WithdrawalServiceImpl(clientRepository);
TransferServiceImpl transferService = new TransferServiceImpl(clientRepository);
Controller depositController = new DepositControllerImpl(depositService);
Controller balanceController = new BalanceControllerImpl(balanceService);
Controller withdrawalController = new WithdrawalControllerImpl(withdrawalService);
Controller transferController = new TransferControllerImpl(transferService);
}
}
package ufrn.imd.controller.impl;
import lombok.extern.java.Log;
import ufrn.imd.controller.Controller;
import ufrn.imd.service.impl.TransferServiceImpl;
import ufrn.imd.utils.ServerResponse;
import ufrn.imd.utils.message.TransferMessage;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Optional;
import static ufrn.imd.utils.NameSpace.HOST_TRANSFER_SERVICE;
import static ufrn.imd.utils.NameSpace.PORT_TRANSFER_SERVICE;
@Log
public class TransferControllerImpl extends UnicastRemoteObject implements Controller {
private final TransferServiceImpl service;
public TransferControllerImpl(TransferServiceImpl service) throws RemoteException, MalformedURLException, AlreadyBoundException {
super();
log.info("Starting Transfer service!");
this.service = service;
log.info(String.format("Initializing server in %s", HOST_TRANSFER_SERVICE));
LocateRegistry.createRegistry(PORT_TRANSFER_SERVICE);
Naming.bind(HOST_TRANSFER_SERVICE, this);
}
@Override
public ServerResponse getResponse(Object client) throws RemoteException {
TransferMessage transferMessage = (TransferMessage) client;
ServerResponse response;
try {
response = this.service.transfer(transferMessage.getValue(), Optional.of(transferMessage.getFrom()),
Optional.of(transferMessage.getTo()));
} catch (RuntimeException e) {
e.printStackTrace();
response = ServerResponse.builder()
.status(500)
.message(e.getMessage())
.body(transferMessage.getFrom())
.build();
}
return response;
}
}
......@@ -22,4 +22,10 @@ public class ClientRepository implements Repository<Client> {
public List<Client> findAll() {
return clients;
}
public Optional<Client> findByAccountNumber(String number) {
return clients.stream()
.filter(client -> number.equals(client.getAccount().getNumber()))
.findFirst();
}
}
package ufrn.imd.service.impl;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import ufrn.imd.domain.Account;
import ufrn.imd.domain.Client;
import ufrn.imd.repository.Repository;
import ufrn.imd.repository.impl.ClientRepository;
import ufrn.imd.service.Service;
import ufrn.imd.utils.ServerResponse;
import java.rmi.RemoteException;
import java.util.Optional;
@Log
@AllArgsConstructor
public class TransferServiceImpl implements Service {
private final Repository<Client> clientRepository;
public ServerResponse transfer(Double value, Optional<Client> from, Optional<Account> to) throws RemoteException, RuntimeException {
Client fromUser = from.orElseThrow(() -> new RuntimeException("Null user!"));
Client fromClient = clientRepository.find(fromUser.getId())
.orElseThrow(() -> new RuntimeException("Invalid user!"));
Account toUserAccount = to.orElseThrow(() -> new RuntimeException("Null account"));
Client toClient = ((ClientRepository) clientRepository).findByAccountNumber(toUserAccount.getNumber())
.orElseThrow(() -> new RuntimeException(
String.format("Customer not found with account number %s", toUserAccount.getNumber())));
if(fromClient.getId().equals(toClient.getId()))
throw new RuntimeException("Invalid account");
Account fromAccount = fromClient.getAccount();
if(value > fromAccount.getBalance()) throw new RuntimeException("Insufficient balance");
fromAccount.setBalance(fromAccount.getBalance() - value);
Account toAccount = toClient.getAccount();
toAccount.setBalance(toAccount.getBalance() + value);
log.info("Transfer concluded!");
log.info(String.format("Account balance: R$ %.2f", fromClient.getAccount().getBalance()));
return ServerResponse.builder()
.status(200)
.message("Operation sucessed!")
.body(fromClient.getAccount())
.build();
}
}
package ufrn.imd.utils;
public class NameSpace {
public static final Integer PORT_TRANSFER_SERVICE = 1909;
public final static Integer PORT_BALANCE_SERVICE = 1908;
public final static Integer PORT_DEPOSIT_SERVICE = 1907;
public final static Integer PORT_WITHDRAWAL_SERVICE = 1906;
......@@ -14,4 +14,7 @@ public class NameSpace {
public final static String HOST_WITHDRAWAL_SERVICE =
String.format("rmi://127.0.0.1:%d/%s", PORT_WITHDRAWAL_SERVICE, "withdrawal");
public final static String HOST_TRANSFER_SERVICE =
String.format("rmi://127.0.0.1:%d/%s", PORT_TRANSFER_SERVICE, "transfer");
}
package ufrn.imd.utils.message;
import lombok.AllArgsConstructor;
import lombok.Data;
import ufrn.imd.domain.Account;
import ufrn.imd.domain.Client;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class TransferMessage implements Serializable {
private static final long serialVersionUID = 2L;
private Client from;
private Account to;
private Double value;
}
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment