Advertisement
Guest User

Untitled

a guest
Feb 7th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.77 KB | None | 0 0
  1. package client;
  2.  
  3. import exceptions.*;
  4. import interfaces.BankInterface;
  5. import server.Statement;
  6. import server.Account;
  7.  
  8. import java.text.DateFormat;
  9. import java.rmi.RemoteException;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.rmi.registry.LocateRegistry;
  13. import java.rmi.registry.Registry;
  14. import java.util.Date;
  15.  
  16. //Client ATM Application
  17.  
  18. public class ATM {
  19.     static String operation, username, password;
  20.     static int server_address, server_port, account;
  21.     static double amount;
  22.     static long sessionID;
  23.     static BankInterface bigBank;
  24.     static Date startDate, endDate;
  25.  
  26.     public static void main (String args[]) {
  27.         try {
  28.             //Take the input from the command line
  29.             getArguments(args);
  30.             String name = "Bank";
  31.             //Create rmi registry and get bank object
  32.             Registry reg = LocateRegistry.getRegistry(server_port);
  33.             bigBank = (BankInterface) reg.lookup(name);
  34.             System.out.println("\nClient Connected\n________________________\n");
  35.         }
  36.         catch (InvalidArgumentException e){
  37.             e.printStackTrace();
  38.             System.out.println(e);
  39.         } catch (Exception ex){
  40.             ex.printStackTrace();
  41.             System.out.println(ex);
  42.         }
  43.        
  44.        double balance; //account balance
  45.  
  46.  
  47.  
  48.         if(operation.equals("login")){
  49.             try {
  50.                 //pass username + pass to bank object
  51.                 sessionID = bigBank.login(username, password);
  52.                 Account currentAccount = bigBank.accountDetails(id);
  53.                 //Log account overview
  54.                 System.out.println("\n\n\nAccount Details:\n________________________\n" +
  55.                                    "\nAccount Number: " + currentAccount.getAccountNumber() +
  56.                                    "\nUsername: " + currentAccount.getUserName() +
  57.                                    "\nSessionID: " + sessionID +
  58.                                    "\nBalance: " + currentAccount.getBalance() +
  59.                                    "\n________________________\n");
  60.                 System.out.println("ATM session will be active for 10 minutes");
  61.                 System.out.println("Use SessionID " + sessionID + " for all operations");
  62.             //Catch exceptions that can be thrown from the server
  63.             } catch (RemoteException e) {
  64.                 e.printStackTrace();
  65.             } catch (InvalidLoginException e) {
  66.                 e.printStackTrace();
  67.             } catch (InvalidSessionException e) {
  68.                 e.printStackTrace();
  69.             }
  70.         }
  71.  
  72.         else if(operation.equals("deposit")){
  73.             try {
  74.                 //Make bank deposit and get updated balance
  75.                 balance = bigBank.deposit(account, amount, sessionID);
  76.                 System.out.println("Successfully deposited E" + amount + " into account " + account);
  77.                 System.out.println("New balance: E" + balance);
  78.             //Catch exceptions that can be thrown from the server
  79.             } catch (RemoteException e) {
  80.                 e.printStackTrace();
  81.             } catch (InvalidSessionException e) {
  82.                 System.out.println(e.getMessage());
  83.             }
  84.         }
  85.  
  86.         else if(operation.equals("withdraw")){
  87.             try {
  88.                 //Make bank withdrawal and get updated balance
  89.                 balance = bigBank.withdraw(account, amount, sessionID);
  90.                 System.out.println("Successfully withdrew E" + amount + " from account " + account +
  91.                                    "\nRemaining Balance: E" + balance);
  92.             //Catch exceptions that can be thrown from the server
  93.             } catch (RemoteException e) {
  94.                 e.printStackTrace();
  95.             } catch (InvalidSessionException e) {
  96.                 System.out.println(e.getMessage());
  97.             } catch (InsufficientFundsException e) {
  98.                 System.out.println(e.getMessage());
  99.             }
  100.         }
  101.  
  102.         else if(operation.equals("inquiry")){
  103.             try {
  104.                 //Get account details from bank
  105.                 Account acc = bigBank.inquiry(account,sessionID);
  106.                 System.out.println("\n\n\nAccount Details:\\n________________________\n" +
  107.                         "Account Number: " + acc.getAccountNumber() +
  108.                         "\nUsername: " + acc.getUserName() +
  109.                         "\nBalance: €" + acc.getBalance() +
  110.                         "\n________________________\n");
  111.             //Catch exceptions that can be thrown from the server
  112.             } catch (RemoteException e) {
  113.                 e.printStackTrace();
  114.             } catch (InvalidSessionException e) {
  115.                 System.out.println(e.getMessage());
  116.             }
  117.            }
  118.  
  119.         else if(operation.equals("statement")){
  120.             Statement s = null;
  121.             try {
  122.                 //Get statement for required dates
  123.                 s = (Statement) bigBank.getStatement(account, startDate, endDate, sessionID);
  124.  
  125.                 //format statement for printing to the window
  126.                 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  127.                 System.out.print("________________________________________________________________________\n");
  128.                 System.out.println("Statement for Account " + account + " between " +
  129.                                    dateFormat.format(startDate) + " and " + dateFormat.format(endDate));
  130.                 System.out.print("________________________________________________________________________\n");
  131.                 System.out.println("Date\t\t\tTransaction Type\tAmount\t\tBalance");
  132.                 System.out.print("________________________________________________________________________\n");
  133.  
  134.                 for(Object t : s.getTransations()) {
  135.                     System.out.println(t);
  136.                 }
  137.                 System.out.print("________________________________________________________________________\n");
  138.             //Catch exceptions that can be thrown from the server
  139.             } catch (RemoteException e) {
  140.                 e.printStackTrace();
  141.             } catch (InvalidSessionException e) {
  142.                 System.out.println(e.getMessage());
  143.             } catch (StatementException e) {
  144.                 System.out.println(e.getMessage());
  145.             }
  146.         }
  147.  
  148.         else{
  149.             System.out.println("Operation not supported");
  150.         }
  151.    
  152.     }
  153.  
  154.     public static void getArguments(String args[]) throws InvalidArgumentException{
  155.         //Makes sure server, port and operation are entered as arguments
  156.         if(args.length < 4) {
  157.             throw new InvalidArgumentException();
  158.         }
  159.  
  160.         //Parses arguments from command line
  161.         //arguments are in different places based on operation, so switch needed here
  162.         server_port = Integer.parseInt(args[1]);
  163.         operation = args[2];
  164.         switch (operation){
  165.             case "login":
  166.                 username = args[3];
  167.                 password = args[4];
  168.                 break;
  169.                
  170.             case "inquiry":
  171.                 account = Integer.parseInt(args[3]);
  172.                 sessionID = Long.parseLong(args[4]);
  173.                 break;
  174.                
  175.             case "withdraw":
  176.             case "deposit":
  177.                 amount = Double.parseDouble(args[4]);
  178.                 account = Integer.parseInt(args[3]);
  179.                 sessionID = Long.parseLong(args[5]);
  180.                 break;
  181.  
  182.             case "statement":
  183.                 account = Integer.parseInt(args[3]);
  184.                 startDate = new Date(args[4]);
  185.                 endDate = new Date(args[5]);
  186.                 sessionID = Long.parseLong(args[6]);
  187.                 break;
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement