Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA / COMPUTER NETWORKS Write a client-server program to work like an ATM machi

ID: 3747896 • Letter: J

Question

JAVA / COMPUTER NETWORKS

Write a client-server program to work like an ATM machine using UDP protocol. The client program should have the ability to choose operations, such as deposit, withdraw and balance check of the user if he is a valid user. And server program maintains a file with user details for authentication. For each row we would have user details including name, pin, and available balance. When a client requests to deposit or withdraw money, then the server should prompt them for their id and pin and after their validation, the server should allow them to deposit or withdraw money (On deposit add the money to the available balance and on withdraw deduct the available balance by the amount given and save this information to the file, and also provide some sentence to client program that the operation is successfully done). Test the operations from client program like deposit + balance check, withdraw + balance check, invalid user trying to withdraw/deposit/checking balance (show an error message that he is not a user) and provide screenshots of their output.

Explanation / Answer

Java Code :

Server:

import java.io.*;
import java.net.*;

public class ATMServer
{
private static String UserId;
private static String Pin;
private static String Amount;
public static void main(String args[]) throws Exception
{
DatagramSocket atmSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true) {
DatagramPacket receiveRequest = new DatagramPacket(receiveData, receiveData.length);
atmSocket.receive(receiveRequest);
String operation = new String(receiveRequest.getData());
System.out.println("RECEIVED: " + operation);
InetAddress requestAddress = receiveRequest.getAddress();
int port = receiveRequest.getPort();
String capitalizedSentence = operation.toUpperCase();
atmSocket.receive(receiveRequest);
UserId = new String(receiveRequest.getData());
if(!IsUserPresent(getDetails()))
{
String userPresent = "";
sendData = userPresent.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, requestAddress, port);
atmSocket.send(sendPacket);
continue;
}
String userPresent = "Present";
sendData = userPresent.getBytes();
DatagramPacket userValidPacket =
new DatagramPacket(sendData, sendData.length, requestAddress, port);
atmSocket.send(userValidPacket);
atmSocket.receive(receiveRequest);
Pin = new String(receiveRequest.getData());
if(!isValidUser()){
String userValid = "";
sendData = userValid.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, requestAddress, port);
atmSocket.send(sendPacket);
continue;
}
if (operation == "DEPOSIT" || operation == "WITHDRAW"){
atmSocket.receive(receiveRequest);
Amount = new String(receiveRequest.getData());
sendData = getResponse(operation).getBytes();
DatagramPacket amoountPack =
new DatagramPacket(sendData, sendData.length, requestAddress, port);
atmSocket.send(amoountPack);
continue;
}
if (operation == "BALANCE") {
sendData = getResponse(operation).getBytes();
DatagramPacket balancepack =
new DatagramPacket(sendData, sendData.length, requestAddress, port);
atmSocket.send(balancepack);
continue;
}

}
}

private static String getResponse(String operation){

switch (operation){
case "Authenticate":
if(isValidUser()) {
return "Valid User";
}
return "";
case "DEPOSIT":
String userDetails = getDetails();
String balance = userDetails.split(" ")[3];
int balanceInt = Integer.parseInt(balance);
int newbAL = balanceInt + Integer.parseInt(Amount);
String newContent = userDetails.replace(balance, Integer.toString(newbAL));
updateDetails(userDetails, newContent);
return "Success";
case "WITHDRAW":
String userDetails1 = getDetails();
String balance1 = userDetails1.split(" ")[3];
int balanceInt1 = Integer.parseInt(balance1);
if(balanceInt1 < Integer.parseInt(Amount)){
return "Not enOUGH bALANCE";
}
int newbAL1 = balanceInt1 - Integer.parseInt(Amount);
String newContent1 = userDetails1.replace(balance1, Integer.toString(newbAL1));
updateDetails(userDetails1, newContent1);
return "Success";
case "BALANCE":
String userDetails2 = getDetails();
String balance2 = userDetails2.split(" ")[3];
return balance2;
default:
return "Invalid Operation";
}
}

private static boolean isValidUser()
{
String userDetails = getDetails();
if (!IsUserPresent(userDetails)) return false;
String userPin = userDetails.split(" ")[2];
if (Pin == userPin){
return true;
}
return false;
}

private static boolean IsUserPresent(String userDetails) {
if(userDetails == null){
return false;
}
return true;
}

private static String getDetails() {
// file format
// id name pin balance
File detailsFile = new File("details.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(detailsFile));
String line = reader.readLine();
while (line != null) {
if (line.startsWith(UserId)) {
return line;
}
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}


private static void updateDetails(String oldData, String newData) {
File detailsFile = new File("details.txt");
String currentData = "";
BufferedReader reader = null;
FileWriter writer = null;
try {
reader = new BufferedReader(new FileReader(detailsFile));
String line = reader.readLine();
while (line != null) {
currentData = currentData + line + System.lineSeparator();
line = reader.readLine();
}

String updatedData = currentData.replaceAll(oldData, newData);
writer = new FileWriter(currentData);
writer.write(updatedData);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Clent:

import java.io.*;
import java.net.*;

public class ATMmachine {
public static void main(String args[]) throws Exception {
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
//read Transaction
displayInitialMenu();
String operation = userInput.readLine();
operation = getOption(operation);
if(operation == null){
System.out.println("Invalid option , exit and retry again");
clientSocket.close();
return;
}
sendData = operation.getBytes();
DatagramPacket sendOperation = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendOperation);
DatagramPacket operationPacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(operationPacket);
String operationResponse = new String(operationPacket.getData());
System.out.println(operationResponse);
//read id
String id = userInput.readLine();
sendData = id.getBytes();
DatagramPacket sendId = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendId);
DatagramPacket idPacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(idPacket);
String idResponse = new String(idPacket.getData());
if (idResponse == ""){
System.out.println("User not found, retry again from beginning");
clientSocket.close();
return;
}
System.out.println(idResponse);
String pin = userInput.readLine();
sendData = pin.getBytes();
DatagramPacket sendPin = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPin);
DatagramPacket sendPacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(sendPacket);
String pinResponse = new String(idPacket.getData());
if(pinResponse == ""){
System.out.println("Pin not valid, retry again from beginning");
clientSocket.close();
return;
}
if(operation == "DEPOSIT"){
System.out.println("Enter the amount to deposit");
ReadandSendAmount(userInput, clientSocket, IPAddress, receiveData);
clientSocket.close();
return;

}
if(operation == "WITHDRAW"){
System.out.println("Enter the amount to withdraw");
ReadandSendAmount(userInput, clientSocket, IPAddress, receiveData);
clientSocket.close();
return;
}
if(operation == "BALANCE"){
System.out.println(pinResponse);
clientSocket.close();
return;
}

clientSocket.close();
}

private static void ReadandSendAmount(BufferedReader userInput, DatagramSocket clientSocket, InetAddress IPAddress, byte[] receiveData) throws IOException {
byte[] sendData;
String amount = userInput.readLine();
sendData = amount.getBytes();
DatagramPacket sendAmount = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendAmount);
DatagramPacket amountPacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(amountPacket);
String amountResponse = new String(amountPacket.getData());
System.out.println(amountResponse);
}

private static void displayInitialMenu(){
System.out.println("ATM");
System.out.println("1. WITHDRAW");
System.out.println("2. DEPOSIT");
System.out.println("3. CHECK BALANCE");
System.out.println("4. EXIT");
}

private static String getOption(String val){
switch (val){
case "1":
return "WITHDRAW";
case "2":
return "DEPOSIT";
case "3":
return "BALANCE";
case "4":
return "EXIT";
default:
return null;
}

}

}

Output:

23 test1 1234 7878

25 test2 1235 7690