Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////CLIENT CLIENT CLIENT //////////////////////////////////
- import java.io.*;
- import java.net.*;
- import java.util.Scanner;
- /**
- * Created by Dawood on 2016-11-29.
- */
- public class ATMClient
- {
- private static int connectionPort = 8999;
- public static void main(String[] args) throws Exception
- {
- String address = "127.0.0.1";
- Socket ATMSocket = new Socket(address, connectionPort);
- PrintWriter out = new PrintWriter(ATMSocket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(new InputStreamReader(ATMSocket.getInputStream()));
- Scanner scanner = new Scanner(System.in);
- System.out.println("Contacting bank ... ");
- System.out.println(in.readLine());
- System.out.println(in.readLine());
- /*While we don't get code 1 from the server
- we should tell the user that login failed and that
- he can either quit or retry the login process.*/
- while(true)
- {
- int temp = scanner.nextInt();
- if(temp == -1) return;
- out.println(temp);
- out.println(scanner.nextInt());
- if(Integer.parseInt(in.readLine()) == 1) break;
- System.out.println("Login failed, retry or write '-1' to exit");
- }
- System.out.println("You have successfully logged in!");
- printMenu(in);
- System.out.print("> ");
- int menuOption = scanner.nextInt();
- out.println(menuOption);
- int userInput;
- while(menuOption < 4)
- {
- if(menuOption == 1)
- {
- System.out.println(in.readLine());
- printMenu(in);
- System.out.print("> ");
- menuOption = scanner.nextInt();
- out.println(menuOption);
- }
- else if (menuOption > 3)
- {
- break;
- }
- else
- {
- if(menuOption == 2)
- {
- int verified = -1;
- while(verified != 1)
- {
- BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
- String temp;
- System.out.println(in.readLine());
- out.println(temp = BR.readLine());
- if(temp.equals("-1")) return;
- verified = Integer.parseInt(in.readLine());
- if(verified != 1) System.out.println("The one time code you entered is not valid!");
- }
- }
- System.out.println(in.readLine());
- userInput = scanner.nextInt();
- out.println(userInput);
- String str;
- str = in.readLine();
- System.out.println(str);
- printMenu(in);
- System.out.print("> ");
- menuOption = scanner.nextInt();
- out.println(menuOption);
- }
- }
- out.close();
- in.close();
- ATMSocket.close();
- }
- private static void printMenu(BufferedReader BR) throws IOException
- {
- for(int a = 0; a < 4; a++)
- {
- System.out.println(BR.readLine());
- }
- }
- }
- ///////SERVER SERVER SERVER /////////////////////////////////////////
- import java.net.*;
- /**
- * Created by Dawood on 2016-11-29.
- */
- public class ATMServer
- {
- private static int connectionPort = 8999;
- public static void main(String[] args) throws Exception
- {
- boolean listening = true;
- ServerSocket serverSocket = new ServerSocket(connectionPort);
- System.out.println("Bank started listening on port: " + connectionPort);
- while (listening) new ATMServerThread(serverSocket.accept()).start();
- serverSocket.close();
- }
- }
- //////SERVERTHREAD SERVERTHREAD /////////////////////////////////
- import java.io.*;
- import java.net.*;
- import java.nio.file.Files;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Dawood on 2016-11-29.
- */
- public class ATMServerThread extends Thread
- {
- private Socket socket;
- private BufferedReader BR;
- private PrintWriter PW;
- //Variables for holding user information.
- private int CARDNUMBER;
- private int CODE;
- private int BALANCE;
- private String FILEPATH = "D:\\Users\\Dawood\\IdeaProjects\\LabInet\\src\\Accounts.txt";
- public ATMServerThread(Socket socket) throws Exception
- {
- super("ATMServerThread");
- this.socket = socket;
- PW = new PrintWriter(this.socket.getOutputStream(), true);
- BR = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
- }
- public void run()
- {
- try
- {
- String inputLine;
- int value;
- PW.println("Welcome to Bank!");
- PW.println("Please Enter Your Card Number Followed By A New Line Followed By The Code");
- verifyLogin();
- sendMenu();
- inputLine = BR.readLine();
- int choice = Integer.parseInt(inputLine);
- while (choice != 4)
- {
- int deposit = 1;
- switch (choice)
- {
- case 2:
- int returnCode;
- while((returnCode = withdrawCode()) != 1)
- {
- if(returnCode == -1)
- return;
- }
- deposit = -1;
- case 3:
- PW.println("Enter amount: ");
- inputLine = BR.readLine();
- value = Integer.parseInt(inputLine);
- BALANCE += deposit * value;
- modifyFile();
- case 1:
- PW.println("Current balance is " + BALANCE + " dollars");
- sendMenu();
- inputLine = BR.readLine();
- choice = Integer.parseInt(inputLine);
- break;
- default:
- break;
- }
- }
- PW.println("Good Bye");
- PW.close();
- BR.close();
- socket.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- private int withdrawCode()
- {
- try
- {
- int verified = -1;
- while(verified != 1)
- {
- PW.println("Please enter your one time code or enter '-1' to exit");
- String temp = BR.readLine();
- if(temp.equals("-1"))
- return -1;
- if(temp.length() == 2 && temp.charAt(0) >= '0' && temp.charAt(0) <= '9' && temp.charAt(1) >= '0' && temp.charAt(1) <= '9')
- verified = 1;
- PW.println(verified);
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- return 1;
- }
- private void sendMenu()
- {
- PW.println("(1)Balance");
- PW.println("(2)Withdrawal");
- PW.println("(3)Deposit");
- PW.println("(4)Exit");
- }
- private void verifyLogin()
- {
- try
- {
- boolean match = false;
- while(!match)
- {
- BufferedReader FileBR = new BufferedReader(new FileReader("D:\\Users\\Dawood\\IdeaProjects\\LabInet\\src\\Accounts.txt"));
- CARDNUMBER = Integer.parseInt(BR.readLine());
- CODE = Integer.parseInt(BR.readLine());
- String tempCARDNUMBER;
- String tempCODE;
- String tempBALANCE;
- while((tempCARDNUMBER = FileBR.readLine()) != null)
- {
- tempCODE = FileBR.readLine();
- tempBALANCE = FileBR.readLine();
- if(Integer.parseInt(tempCARDNUMBER) == CARDNUMBER && Integer.parseInt(tempCODE) == CODE)
- {
- BALANCE = Integer.parseInt(tempBALANCE);
- PW.println(1);
- match = true;
- break;
- }
- }
- if(!match) PW.println(-1);
- FileBR.close();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- private void modifyFile()
- {
- try
- {
- ArrayList<String> rows = new ArrayList<>();
- BufferedReader FileBR = new BufferedReader(new FileReader(FILEPATH));
- String tempCARDNUMBER;
- String tempCODE;
- String tempBALANCE;
- while((tempCARDNUMBER = FileBR.readLine()) != null)
- {
- rows.add(tempCARDNUMBER);
- tempCODE = FileBR.readLine();
- rows.add(tempCODE);
- tempBALANCE = FileBR.readLine();
- if(Integer.parseInt(tempCARDNUMBER) == CARDNUMBER && Integer.parseInt(tempCODE) == CODE)
- rows.add(Integer.toString(BALANCE));
- else
- rows.add(tempBALANCE);
- }
- FileBR.close();
- BufferedWriter FileBW = new BufferedWriter(new FileWriter(FILEPATH));
- for(int a = 0; a < rows.size(); a++)
- {
- FileBW.write(rows.get(a));
- FileBW.newLine();
- }
- FileBW.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment