dawrehxyz

ATM

Nov 30th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.79 KB | None | 0 0
  1. //////////CLIENT CLIENT CLIENT //////////////////////////////////
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Created by Dawood on 2016-11-29.
  8.  */
  9. public class ATMClient
  10. {
  11.     private static int connectionPort = 8999;
  12.  
  13.     public static void main(String[] args) throws Exception
  14.     {
  15.         String address = "127.0.0.1";
  16.         Socket ATMSocket = new Socket(address, connectionPort);
  17.         PrintWriter out = new PrintWriter(ATMSocket.getOutputStream(), true);
  18.         BufferedReader in = new BufferedReader(new InputStreamReader(ATMSocket.getInputStream()));
  19.         Scanner scanner = new Scanner(System.in);
  20.  
  21.         System.out.println("Contacting bank ... ");
  22.         System.out.println(in.readLine());
  23.         System.out.println(in.readLine());
  24.  
  25.         /*While we don't get code 1 from the server
  26.         we should tell the user that login failed and that
  27.         he can either quit or retry the login process.*/
  28.         while(true)
  29.         {
  30.             int temp = scanner.nextInt();
  31.             if(temp == -1) return;
  32.             out.println(temp);
  33.             out.println(scanner.nextInt());
  34.             if(Integer.parseInt(in.readLine()) == 1) break;
  35.             System.out.println("Login failed, retry or write '-1' to exit");
  36.         }
  37.  
  38.  
  39.         System.out.println("You have successfully logged in!");
  40.         printMenu(in);
  41.  
  42.         System.out.print("> ");
  43.         int menuOption = scanner.nextInt();
  44.         out.println(menuOption);
  45.  
  46.         int userInput;
  47.         while(menuOption < 4)
  48.         {
  49.             if(menuOption == 1)
  50.             {
  51.                 System.out.println(in.readLine());
  52.                 printMenu(in);
  53.                 System.out.print("> ");
  54.                 menuOption = scanner.nextInt();
  55.                 out.println(menuOption);
  56.             }
  57.             else if (menuOption > 3)
  58.             {
  59.                 break;
  60.             }
  61.             else
  62.             {
  63.                 if(menuOption == 2)
  64.                 {
  65.                     int verified = -1;
  66.                     while(verified != 1)
  67.                     {
  68.                         BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
  69.                         String temp;
  70.                         System.out.println(in.readLine());
  71.                         out.println(temp = BR.readLine());
  72.                         if(temp.equals("-1")) return;
  73.                         verified = Integer.parseInt(in.readLine());
  74.                         if(verified != 1) System.out.println("The one time code you entered is not valid!");
  75.                     }
  76.                 }
  77.                 System.out.println(in.readLine());
  78.                 userInput = scanner.nextInt();
  79.                 out.println(userInput);
  80.                 String str;
  81.                 str = in.readLine();
  82.                 System.out.println(str);
  83.                 printMenu(in);
  84.                 System.out.print("> ");
  85.                 menuOption = scanner.nextInt();
  86.                 out.println(menuOption);
  87.             }
  88.         }
  89.  
  90.         out.close();
  91.         in.close();
  92.         ATMSocket.close();
  93.     }
  94.  
  95.     private static void printMenu(BufferedReader BR) throws IOException
  96.     {
  97.         for(int a = 0; a < 4; a++)
  98.         {
  99.             System.out.println(BR.readLine());
  100.         }
  101.     }
  102. }
  103. ///////SERVER SERVER SERVER /////////////////////////////////////////
  104. import java.net.*;
  105.  
  106. /**
  107.  * Created by Dawood on 2016-11-29.
  108.  */
  109. public class ATMServer
  110. {
  111.     private static int connectionPort = 8999;
  112.  
  113.     public static void main(String[] args) throws Exception
  114.     {
  115.         boolean listening = true;
  116.         ServerSocket serverSocket = new ServerSocket(connectionPort);
  117.  
  118.         System.out.println("Bank started listening on port: " + connectionPort);
  119.  
  120.         while (listening) new ATMServerThread(serverSocket.accept()).start();
  121.  
  122.         serverSocket.close();
  123.     }
  124. }
  125.  
  126. //////SERVERTHREAD SERVERTHREAD /////////////////////////////////
  127. import java.io.*;
  128. import java.net.*;
  129. import java.nio.file.Files;
  130. import java.util.ArrayList;
  131. import java.util.List;
  132.  
  133. /**
  134.  * Created by Dawood on 2016-11-29.
  135.  */
  136. public class ATMServerThread extends Thread
  137. {
  138.     private Socket socket;
  139.     private BufferedReader BR;
  140.     private PrintWriter PW;
  141.  
  142.     //Variables for holding user information.
  143.     private int CARDNUMBER;
  144.     private int CODE;
  145.     private int BALANCE;
  146.     private String FILEPATH = "D:\\Users\\Dawood\\IdeaProjects\\LabInet\\src\\Accounts.txt";
  147.  
  148.     public ATMServerThread(Socket socket) throws Exception
  149.     {
  150.         super("ATMServerThread");
  151.         this.socket = socket;
  152.         PW = new PrintWriter(this.socket.getOutputStream(), true);
  153.         BR = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
  154.     }
  155.  
  156.     public void run()
  157.     {
  158.         try
  159.         {
  160.             String inputLine;
  161.             int value;
  162.  
  163.             PW.println("Welcome to Bank!");
  164.             PW.println("Please Enter Your Card Number Followed By A New Line Followed By The Code");
  165.  
  166.             verifyLogin();
  167.             sendMenu();
  168.  
  169.             inputLine = BR.readLine();
  170.             int choice = Integer.parseInt(inputLine);
  171.             while (choice != 4)
  172.             {
  173.                 int deposit = 1;
  174.                 switch (choice)
  175.                 {
  176.                     case 2:
  177.                         int returnCode;
  178.                         while((returnCode = withdrawCode()) != 1)
  179.                         {
  180.                             if(returnCode == -1)
  181.                                 return;
  182.                         }
  183.  
  184.                         deposit = -1;
  185.                     case 3:
  186.                         PW.println("Enter amount: ");
  187.                         inputLine = BR.readLine();
  188.                         value = Integer.parseInt(inputLine);
  189.                         BALANCE += deposit * value;
  190.                         modifyFile();
  191.                     case 1:
  192.                         PW.println("Current balance is " + BALANCE + " dollars");
  193.                         sendMenu();
  194.                         inputLine = BR.readLine();
  195.                         choice = Integer.parseInt(inputLine);
  196.                         break;
  197.                     default:
  198.                         break;
  199.                 }
  200.             }
  201.             PW.println("Good Bye");
  202.             PW.close();
  203.             BR.close();
  204.             socket.close();
  205.         }
  206.         catch (IOException e)
  207.         {
  208.             e.printStackTrace();
  209.         }
  210.     }
  211.  
  212.     private int withdrawCode()
  213.     {
  214.         try
  215.         {
  216.             int verified = -1;
  217.             while(verified != 1)
  218.             {
  219.                 PW.println("Please enter your one time code or enter '-1' to exit");
  220.                 String temp = BR.readLine();
  221.                 if(temp.equals("-1"))
  222.                     return -1;
  223.                 if(temp.length() == 2 && temp.charAt(0) >= '0' && temp.charAt(0) <= '9' && temp.charAt(1) >= '0' && temp.charAt(1) <= '9')
  224.                     verified = 1;
  225.                 PW.println(verified);
  226.             }
  227.         }
  228.         catch (IOException e)
  229.         {
  230.             e.printStackTrace();
  231.         }
  232.         return 1;
  233.     }
  234.  
  235.     private void sendMenu()
  236.     {
  237.         PW.println("(1)Balance");
  238.         PW.println("(2)Withdrawal");
  239.         PW.println("(3)Deposit");
  240.         PW.println("(4)Exit");
  241.     }
  242.  
  243.     private void verifyLogin()
  244.     {
  245.         try
  246.         {
  247.             boolean match = false;
  248.             while(!match)
  249.             {
  250.                 BufferedReader FileBR = new BufferedReader(new FileReader("D:\\Users\\Dawood\\IdeaProjects\\LabInet\\src\\Accounts.txt"));
  251.                 CARDNUMBER = Integer.parseInt(BR.readLine());
  252.                 CODE = Integer.parseInt(BR.readLine());
  253.                 String tempCARDNUMBER;
  254.                 String tempCODE;
  255.                 String tempBALANCE;
  256.                 while((tempCARDNUMBER = FileBR.readLine()) != null)
  257.                 {
  258.                     tempCODE = FileBR.readLine();
  259.                     tempBALANCE = FileBR.readLine();
  260.                     if(Integer.parseInt(tempCARDNUMBER) == CARDNUMBER && Integer.parseInt(tempCODE) == CODE)
  261.                     {
  262.                         BALANCE = Integer.parseInt(tempBALANCE);
  263.                         PW.println(1);
  264.                         match = true;
  265.                         break;
  266.                     }
  267.                 }
  268.                 if(!match) PW.println(-1);
  269.                 FileBR.close();
  270.             }
  271.         }
  272.         catch (IOException e)
  273.         {
  274.             e.printStackTrace();
  275.         }
  276.     }
  277.  
  278.     private void modifyFile()
  279.     {
  280.         try
  281.         {
  282.             ArrayList<String> rows = new ArrayList<>();
  283.             BufferedReader FileBR = new BufferedReader(new FileReader(FILEPATH));
  284.             String tempCARDNUMBER;
  285.             String tempCODE;
  286.             String tempBALANCE;
  287.             while((tempCARDNUMBER = FileBR.readLine()) != null)
  288.             {
  289.                 rows.add(tempCARDNUMBER);
  290.                 tempCODE = FileBR.readLine();
  291.                 rows.add(tempCODE);
  292.                 tempBALANCE = FileBR.readLine();
  293.  
  294.                 if(Integer.parseInt(tempCARDNUMBER) == CARDNUMBER && Integer.parseInt(tempCODE) == CODE)
  295.                     rows.add(Integer.toString(BALANCE));
  296.                 else
  297.                     rows.add(tempBALANCE);
  298.             }
  299.             FileBR.close();
  300.             BufferedWriter FileBW = new BufferedWriter(new FileWriter(FILEPATH));
  301.             for(int a = 0; a < rows.size(); a++)
  302.             {
  303.                 FileBW.write(rows.get(a));
  304.                 FileBW.newLine();
  305.             }
  306.             FileBW.close();
  307.         }
  308.         catch (IOException e)
  309.         {
  310.             e.printStackTrace();
  311.         }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment