Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client;
- import exceptions.*;
- import interfaces.BankInterface;
- import server.Statement;
- import server.Account;
- import java.text.DateFormat;
- import java.rmi.RemoteException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.util.Date;
- //Client ATM Application
- public class ATM {
- static String operation, username, password;
- static int server_address, server_port, account;
- static double amount;
- static long sessionID;
- static BankInterface bigBank;
- static Date startDate, endDate;
- public static void main (String args[]) {
- try {
- //Take the input from the command line
- getArguments(args);
- String name = "Bank";
- //Create rmi registry and get bank object
- Registry reg = LocateRegistry.getRegistry(server_port);
- bigBank = (BankInterface) reg.lookup(name);
- System.out.println("\nClient Connected\n________________________\n");
- }
- catch (InvalidArgumentException e){
- e.printStackTrace();
- System.out.println(e);
- } catch (Exception ex){
- ex.printStackTrace();
- System.out.println(ex);
- }
- double balance; //account balance
- if(operation.equals("login")){
- try {
- //pass username + pass to bank object
- sessionID = bigBank.login(username, password);
- Account currentAccount = bigBank.accountDetails(id);
- //Log account overview
- System.out.println("\n\n\nAccount Details:\n________________________\n" +
- "\nAccount Number: " + currentAccount.getAccountNumber() +
- "\nUsername: " + currentAccount.getUserName() +
- "\nSessionID: " + sessionID +
- "\nBalance: " + currentAccount.getBalance() +
- "\n________________________\n");
- System.out.println("ATM session will be active for 10 minutes");
- System.out.println("Use SessionID " + sessionID + " for all operations");
- //Catch exceptions that can be thrown from the server
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (InvalidLoginException e) {
- e.printStackTrace();
- } catch (InvalidSessionException e) {
- e.printStackTrace();
- }
- }
- else if(operation.equals("deposit")){
- try {
- //Make bank deposit and get updated balance
- balance = bigBank.deposit(account, amount, sessionID);
- System.out.println("Successfully deposited E" + amount + " into account " + account);
- System.out.println("New balance: E" + balance);
- //Catch exceptions that can be thrown from the server
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (InvalidSessionException e) {
- System.out.println(e.getMessage());
- }
- }
- else if(operation.equals("withdraw")){
- try {
- //Make bank withdrawal and get updated balance
- balance = bigBank.withdraw(account, amount, sessionID);
- System.out.println("Successfully withdrew E" + amount + " from account " + account +
- "\nRemaining Balance: E" + balance);
- //Catch exceptions that can be thrown from the server
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (InvalidSessionException e) {
- System.out.println(e.getMessage());
- } catch (InsufficientFundsException e) {
- System.out.println(e.getMessage());
- }
- }
- else if(operation.equals("inquiry")){
- try {
- //Get account details from bank
- Account acc = bigBank.inquiry(account,sessionID);
- System.out.println("\n\n\nAccount Details:\\n________________________\n" +
- "Account Number: " + acc.getAccountNumber() +
- "\nUsername: " + acc.getUserName() +
- "\nBalance: €" + acc.getBalance() +
- "\n________________________\n");
- //Catch exceptions that can be thrown from the server
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (InvalidSessionException e) {
- System.out.println(e.getMessage());
- }
- }
- else if(operation.equals("statement")){
- Statement s = null;
- try {
- //Get statement for required dates
- s = (Statement) bigBank.getStatement(account, startDate, endDate, sessionID);
- //format statement for printing to the window
- SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
- System.out.print("________________________________________________________________________\n");
- System.out.println("Statement for Account " + account + " between " +
- dateFormat.format(startDate) + " and " + dateFormat.format(endDate));
- System.out.print("________________________________________________________________________\n");
- System.out.println("Date\t\t\tTransaction Type\tAmount\t\tBalance");
- System.out.print("________________________________________________________________________\n");
- for(Object t : s.getTransations()) {
- System.out.println(t);
- }
- System.out.print("________________________________________________________________________\n");
- //Catch exceptions that can be thrown from the server
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (InvalidSessionException e) {
- System.out.println(e.getMessage());
- } catch (StatementException e) {
- System.out.println(e.getMessage());
- }
- }
- else{
- System.out.println("Operation not supported");
- }
- }
- public static void getArguments(String args[]) throws InvalidArgumentException{
- //Makes sure server, port and operation are entered as arguments
- if(args.length < 4) {
- throw new InvalidArgumentException();
- }
- //Parses arguments from command line
- //arguments are in different places based on operation, so switch needed here
- server_port = Integer.parseInt(args[1]);
- operation = args[2];
- switch (operation){
- case "login":
- username = args[3];
- password = args[4];
- break;
- case "inquiry":
- account = Integer.parseInt(args[3]);
- sessionID = Long.parseLong(args[4]);
- break;
- case "withdraw":
- case "deposit":
- amount = Double.parseDouble(args[4]);
- account = Integer.parseInt(args[3]);
- sessionID = Long.parseLong(args[5]);
- break;
- case "statement":
- account = Integer.parseInt(args[3]);
- startDate = new Date(args[4]);
- endDate = new Date(args[5]);
- sessionID = Long.parseLong(args[6]);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement