-
Problem threading?
This is what I'm trying to do "You will develop a multithreaded client/server application. The client will send an Account object to the server. The server will calculate the interest for the object (5%), set the balance to the new amount, and return the object to the client. The client will then print out the account number and balance. The account class has attributes name, account number, and balance and must adhere to our coding standards.
Use ExecutorService and provide output statements showing the progress of the transaction on both the client and server sides."
I keep getting an error when I try and compile my ThreadServer about the Account class - about an invalid method declaration; return method type required..
I posted my Account class and client and server codes below..
Any help would be appreciated...
Thanks,
Code:
import java.io.*;
public class Account implements Serializable
{
private String name;
private int accountNumber;
private double balance;
public class Account()
{
setName("");
setAccountNumber(0);
setBalance(0);
}
public class Account(String n, int a, double b)
{
setName(n);
setAccountNumber(a);
setBalance(b);
}
public void setName(String n)
{
name = n;
}
public void setAccountNumber(int a)
{
accountNumber = a;
}
public void setBalance(double b)
{
balance = b;
}
public String getName()
{
return name;
}
public int getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
public String toString()
{
return( " Name " + name + " Account Number " + accountNumber + " Balance " + balance);
}
}
Code:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Scanner;
import java.util.Formatter;
import java.util.concurrent.*;
public class Client2
{
public static void main(String args[])
{
Scanner myInput = new Scanner(System.in);
Socket connection;
ObjectInputStream input;
ObjectOutputStream output;
String name;
int accountNumber;
double balance;
Object obj;
try
{
connection = new Socket("localhost", 8000);
input = new ObjectInputStream(connection.getInputStream());
output = new ObjectOutputStream(connection.getOutputStream());
System.out.println("Enter Account Holder's Name");
name = myInput.nextLine();
System.out.println("Enter Account Number");
accountNumber = myInput.nextInt();
System.out.println("Enter Account Holder's Name");
balance = myInput.nextDouble();
obj = (Object) input.readObject();
output.writeObject(obj);
output.flush();
//obj = (Object) input.readObject();
System.out.print(obj.toString());
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
Code:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Scanner;
import java.util.Formatter;
import java.util.concurrent.*;
//.The server will calculate the interest for the object (5%),
//set the balance to the new amount, and return the object to the client.
public class ThreadServer
{
public static void main(String args[])
{
ServerSocket serverSocket;
Socket connection;
try
{
serverSocket = new ServerSocket(8000);
ExecutorService threadExecutor = Executors.newCachedThreadPool();
int clientNo = 1;
while (true)
{
connection = serverSocket.accept();
System.out.println("Start thread for client" + clientNo);
HandleAClient thread = new HandleAClient(connection, clientNo);
threadExecutor.execute(thread);
clientNo++;
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
class HandleAClient implements Runnable
{
ServerSocket serverSocket;
ObjectInputStream input;
ObjectOutputStream output;
Socket connection;
//Formatter output;
int clientNo;
double balance;
Object obj;
public HandleAClient(Socket connection, int clientNo)
{
this.connection = connection;
this.clientNo = clientNo;
}
public void run()
{
try
{
serverSocket = new ServerSocket(8000);
connection = serverSocket.accept();
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
obj = (Object)input.readObject();
balance = ((Account)obj).getBalance()*1.05;
System.out.println(obj.toString());
output.writeObject(obj);
output.flush();
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
-
Re: Problem threading?
Hint: a constructor doesn't have a return type.
A method does, but it must be a recognizable return type. The keyword void is allowed for methods that don't return anything. The keyword class isn't.
db