fork download
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. class ATMClient {
  5. private static final String SERVER_ADDRESS = "127.0.0.1";
  6. private static final int SERVER_PORT = 5000;
  7.  
  8. public static void main(String[] args) {
  9. try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
  10. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  11. PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
  12.  
  13. System.out.print(in.readLine()); // "Enter card number: "
  14. String cardNumber = userInput.readLine();
  15. out.println(cardNumber);
  16.  
  17. System.out.print(in.readLine()); // "Enter PIN: "
  18. String pin = userInput.readLine();
  19. out.println(pin);
  20.  
  21. String response = in.readLine();
  22. System.out.println(response);
  23.  
  24. if (response.startsWith("Welcome")) {
  25. boolean exit = false;
  26. while (!exit) {
  27. System.out.println(in.readLine()); // Display options
  28. String choice = userInput.readLine();
  29. out.println(choice);
  30.  
  31. if ("1".equals(choice)) {
  32. System.out.print(in.readLine()); // "Enter amount to withdraw: "
  33. String amount = userInput.readLine();
  34. out.println(amount);
  35. System.out.println(in.readLine()); // Withdrawal response
  36. } else if ("2".equals(choice)) {
  37. System.out.println(in.readLine()); // "Thank you for using..."
  38. exit = true;
  39. } else {
  40. System.out.println(in.readLine()); // Invalid choice message
  41. }
  42. }
  43. } else {
  44. System.out.println("Invalid card number or PIN. Connection closing.");
  45. }
  46.  
  47. } catch (IOException e) {
  48. System.err.println("Client error: " + e.getMessage());
  49. }
  50. }
  51. }
Success #stdin #stdout #stderr 0.14s 56060KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Client error: Network is unreachable (connect failed)