The Artima Developer Community
Sponsored Link

Java Answers Forum
ATM client server

1 reply on 1 page. Most recent reply: Dec 17, 2005 8:10 AM by sama j

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
sama j

Posts: 2
Nickname: lavender
Registered: Nov, 2005

ATM client server Posted: Nov 27, 2005 1:59 PM
Reply to this message Reply
Advertisement
Hallo

I develop an ATM java code I have put everything that i have learned into it and it just seem not to work

the code is about client and server where the server replay to client requests based on a list of choices (check balance, withdrawal,deposit and transfer) the server is connected to a database accessed by protocol through threads

here is the code in case some one wanna help :P

-------------------client
package AtmClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ATMClient {
public static void main(String[] args) throws IOException {

Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
kkSocket = new Socket("Hostname", 1212);
// System.err.println("Enter the choice and the acount");
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: Hostname");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to:Hostname");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;

while ((fromServer = in.readLine()) != null) {
System.out.println(fromServer);
if (fromServer.equals("Bye."))
break;

fromUser = stdIn.readLine();
if (fromUser != null) {
//System.out.println("Client: " + fromUser);

out.println(fromUser);
}
}

out.close();
in.close();
stdIn.close();
kkSocket.close();

}
}


---------------------server


package AtmServer;

import java.io.IOException;
import java.net.ServerSocket;


public class ATMMulti {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;
boolean listening = true;

try {
serverSocket = new ServerSocket(1212);
} catch (IOException e) {
System.err.println("Could not listen on port: 1212.");
System.exit(-1);
}

while (listening)
new MultiServerThread(serverSocket.accept()).start();

serverSocket.close();
}

}



--------------------thread

package AtmServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import Protocol.Protocol;

public class MultiServerThread extends Thread {
private Socket socket = null;

public MultiServerThread(Socket socket) {
super("MultiServerThread");
this.socket = socket;
}

public void run() {

try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader Ch = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));

String inputLine, outputLine="",Choice;
out.println(outputLine);
while ((inputLine = Ch.readLine()) != null) {
Protocol kkp = new Protocol();
out.println("Please select a choice 1 Check Balance,2 Withdrawal,3 Deposit,4 Account Transfer,5 Exit ");


while ((Choice = Ch.readLine()) != null)
{
if (Choice.equals("1"))
{

out.println("Enter Your Card Number");
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
inputLine=in.readLine();
outputLine = kkp.processInput(Choice+inputLine,"");
if(outputLine.substring(0,1).equals("1"))
{
out.println("Your Current Balance Is"+ outputLine.substring(0,1));

}
else if(outputLine.substring(0,1).equals("3"))
{
out.println("Invalid ID card");
}
else if(outputLine.substring(0,1).equals("4"))
{
out.println("Unknown Error");
}
out.println("Please select a choice 1 Check Balance,2 Withdrawal,3 Deposit,4 Account Transfer,5 Exit ");
//in.close();
}
else if (Choice.equals("2"))
{
out.println("Enter Your Card Number");
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
inputLine=in.readLine();
out.println("Enter The Amount For Withdrawal");
BufferedReader Amt = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String Amount=Amt.readLine();
outputLine = kkp.processInput(Choice+inputLine+Amount,"");
if(outputLine.substring(0,1).equa ls("1"))
{
out.println("Your Current Balance Is"+ outputLine.substring(1));
}
else if(outputLine.substring(0,1).equals("3"))
{
out.println("Your ID is Not Correct");
}
else if(outputLine.substring(0,1).equals("4"))
{
out.println("Unknown Error");
}
else if(outputLine.substring(0,1).equals("2"))
{
out.println("The Amount Exceeds The limit");
}
out.println("Please select a choice 1 Check Balance,2 Withdrawal,3 Deposit,4 Account Transfer,5 Exit ");
//in.close();
}
else if (Choice.equals("3"))
{
out.println("Enter Your Card Number");
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
inputLine=in.readLine();
out.println("Enter The Amount For Deposit");
BufferedReader Amt = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String Amount=Amt.readLine();
outputLine = kkp.processInput(Choice+inputLine+Amount,"");

if(outputLine.substring(0,1).equ als("1"))
{
out.println("Your Current Balance Is"+ outputLine.substring(1));
}
else if(outputLine.substring(0,1).equals("3"))
{
out.println("Your ID is Not Correct");
}
else if(outputLine.substring(0,1).equals("4"))
{
out.println("Unknown Error");
}
else if(outputLine.substring(0,1).equals("2"))
{
out.println("The Amount Exceeds The limit");
}
out.println("Please select a choice 1 Check Balance,2 Withdrawal,3 Deposit,4 Account Transfer,5 Exit ");
//in.close();
}
else if (Choice.equals("4"))
{
out.println("Enter Your Card Number");
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
inputLine=in.readLine();
out.println("Enter The Amount For ");
BufferedReader Amt = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String Amount=Amt.readLine();
out.println("Enter The Account No to which to be transferred");
BufferedReader Acc2 = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String SecondAccount=Acc2.readLine();
outputLine = kkp.processInput(Choice+inputLine+Amount,SecondAccount);
if(outputLine.substrin g(0,1).equals("1"))
{
out.println("Your Current Balance Is"+ outputLine.substring(1));
}
else if(outputLine.substring(0,1).equals("3"))
{
out.println("Your ID is Not Correct");
}
else if(outputLine.substring(0,1).equals("4"))
{
out.println("Unknown Error");
}
else if(outputLine.substring(0,1).equals("2"))
{
out.println("The Amount Exceeds The limit");
}
out.println("Please select a choice 1 Check Balance,2 Withdrawal,3 Deposit,4 Account Transfer,5 Exit ");
//in.close();

}

else if (Choice.equals("0"))
break;

}
}
out.close();

socket.close();

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}




-------------------protocol


package Protocol;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Protocol {

public String processInput(String theInput,String SecondAccount) {
String theOutput = "4";
Connection con = null ;

try
{
/* MySQL */

String databaseURL = "jdbc:mysql://localhost/ATM1" ;
String driverName = "com.mysql.jdbc.Driver" ;
String user = "" ;
String password = "" ;

String ID = null;


Class.forName ( driverName ).newInstance();

con = DriverManager.getConnection ( databaseURL , user , password );
ID=theInput;
String selection="";
selection=ID.substring(0,1);
Statement statement ;
statement = con.createStatement();
if (selection.equals("1"))
{
String Card=ID.substring(1);
String selectQuery = "SELECT * FROM customer where Card = '" + Card + "'" ;
ResultSet resultSet = statement.executeQuery ( selectQuery );
if ( resultSet != null ) // Succes
{
while ( resultSet.next() )
{
String Balance = ( String ) resultSet.getObject ( "Balance" ) ;
return theOutput = "1 " +Balance ;
}
}
if(!resultSet.first())
{ resultSet.close();
return theOutput ="3";
}


if ( con != null )
{
con.close();
statement.close();
}


}
else if (selection.equals("2"))
{
String Card=ID.substring(1,17);
float Bal=0;
float Amount= Float.parseFloat((ID.substring(17)));
String selectQuery = "SELECT * FROM customer where Card = '" + Card + "'" ;
ResultSet resultSet = statement.executeQuery ( selectQuery );
if ( resultSet.next() )
{
Bal= Float.parseFloat((String) resultSet.getObject ( "Balance" )) ;
if((Bal-Amount)>=0)
{
String updateQuery = "update customer set Balance=Balance-" + Amount +" where Card ='"+ Card +"'" ;
int temp= statement.executeUpdate(updateQuery);
if (temp==1)
{
return theOutput = "1" + (Bal-Amount) ;
}
else
{
return theOutput = "4"+Bal ;
}
}

else
{
return theOutput = "2"+Bal ;
}
}

else if (!resultSet.next())
{ resultSet.close();
return theOutput ="3";
}

statement.close();

if ( con != null )
{
con.close();

}

if(!resultSet.first())
{ resultSet.close();
return theOutput ="3";
}


if ( con != null )
{
con.close();
statement.close();
}
}
else if (selection.equals("3"))
{
String Card=ID.substring(1,17);
float Bal=0;
float Amount= Float.parseFloat((ID.substring(17)));
String selectQuery = "SELECT * FROM customer where Card = '" + Card + "'" ;
ResultSet resultSet = statement.executeQuery ( selectQuery );
if ( resultSet != null ) // Succes
{
if ( resultSet.next() )
{
Bal= Float.parseFloat((String) resultSet.getObject ( "Balance" )) ;
String updateQuery = "update customer set Balance=Balance+" + Amount +" where Card ='"+ Card +"'" ;
int temp= statement.executeUpdate(updateQuery);
if (temp==1)
{
return theOutput = "1" + (Bal+Amount) ;
}
else
{
return theOutput = "4"+Bal ;
}

}


}
if(!resultSet.first())
{ resultSet.close();
return theOutput ="3";
}

statement.close();

if ( con != null )
{
con.close();

}

if(!resultSet.first())
{ resultSet.close();
return theOutput ="3";
}


if ( con != null )
{
con.close();
statement.close();
}
}
else if (selection.equals("4"))
{
String Card=ID.substring(1,17);
float Bal=0;
float Amount= Float.parseFloat((ID.substring(17)));
String selectQuery = "SELECT * FROM customer where Card = '" + Card + "'" ;
ResultSet resultSet = statement.executeQuery ( selectQuery );
//String selectQuery1 = "SELECT * FROM customer where AcountID = '" + SecondAccount + "'" ;
//ResultSet resultSet1 = statement.executeQuery ( selectQuery1);
if ( resultSet != null ) // Succes
{
if ( resultSet.next() )
{
Bal= Float.parseFloat((String) resultSet.getObject ( "Balance" )) ;
if((Bal-Amount)>=0)
{
String updateQuery = "update customer set Balance=Balance+" + Amount +" where AcountID ='"+ SecondAccount +"'" ;

int temp= statement.executeUpdate(updateQuery);
updateQuery = "update customer set Balance=Balance-" + Amount +" where Card ='"+ Card +"'" ;

temp= statement.executeUpdate(updateQuery);
if (temp==1)
{
return theOutput = "1" + (Bal-Amount) ;
}
else
{
return theOutput = "4"+Bal ;
}
}
else
{
return theOutput = "2"+Bal ;
}

}


}
if(!resultSet.first())
{ resultSet.close();
return theOutput ="3";
}

statement.close();

if ( con != null )
{
con.close();

}

if(!resultSet.first())
{ resultSet.close();
return theOutput ="3";
}


if ( con != null )
{
con.close();
statement.close();
}
}

}

catch ( Exception e )
{
System.err.println ( "Exception: " + e.getMessage() );
}


return theOutput;
}
}


sama j

Posts: 2
Nickname: lavender
Registered: Nov, 2005

Re: ATM client server Posted: Dec 17, 2005 8:10 AM
Reply to this message Reply
No one !!!!!!!!!!

Flat View: This topic has 1 reply on 1 page
Topic: JAR Question Previous Topic   Next Topic Topic: Connecting to File server on network

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use