الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

BankAccount code

مغلق
بدأه دلوعة الجافا في 14 أبريل 2006 · 9 رد · 902 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

مرحبا اخواني

عندي برنامج BankAccount

الاستاذ طلب مننا نسويه و انا حاولت اسويه و طرشته الاستاذ و مسح اشياء من الكود و كتبلي complete

المهم انتمنى احد يساعدني بهذا الكود راح احطي الاكواد الاربعه لانه فيه

client.java

server.java

BankAccount.java

ConnectionHandler.java

و اتمنى احد يساعدني

لاني حاولت بس الاستاذ حذف اشياء و كتبلي Complete

و ما عرفت شنو اسوي كل ما احاول اعدل يطلعي error

هذا كود BankAccount

public class BankAccount{
 private float balance;
 public BankAccount() {
  balance = 0;
 }
 public float deposit(float amount) {
  balance += amount;

  return balance;
 }
 public boolean withdraw(double amount) {
 if (balance < amount)
   {
    System.out.println("BankAccount: withdrawal " + amount +
         " but we only have " + balance +
         " in the account!");
   return false;
   }
   balance -= amount;

   System.out.println("BankAccount: withdrawal " + amount +
         "  balance = " + balance);
  return true;
 }
 public float getbalance() {
 // complete ...
  return balance;
 }
}

و هذا كود Client

import java.io.*;
import java.net.*;
public class Client{
 static Socket conn = null;
 static PrintWriter writer = null;
 static BufferedReader reader = null;
 // Method which connect the client to the service (identified by port)
 // provided by the server (identified by ipaddr).
 // If the connection is successful, it return true.
 public static boolean connect(String ipaddr, int port) {
  try { // Connect to server
   conn = new Socket(ipaddr, port);
  } catch (IOException e) {
   System.err.println("Cannot connect to " + ipaddr +"/"+ port);
   System.exit(1);
  }
  try { // Get a reading and writing channels to the connection.
   writer = new PrintWriter(conn.getOutputStream(), true);
   reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  } catch (IOException e) {
   System.err.println("Couldn't get reading/writing channels to socket");
   System.exit(1);
  }
  return true;
 }
 public static void main(String[] args) throws IOException {
  String request = null; // Used to send request messages to server
  String response = null;// Used to receive response messages from server
  // Get reading channel from standard input (i.e., keyboard)
  BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
  String command = "";
  boolean connected = false;
  while (command != null) {
   System.out.print("> ");
   command = keyboard.readLine();
   if (command.equals("connect")) {
    connected = connect(args[0], Integer.parseInt(args[1]));
    request = "hello";
   } else if (command.equals("disconnect")) {

   // complete ...
    connected = false;
   } else if (command.equals("exit")) {
   // complete ...
   } else if (command.equals("balance")) {
   // complete ...
   } else if (command.startsWith("deposit")) {
   // complete ...
   } else if (command.startsWith("withdraw")) {
   // complete ...
   } else { // this is the case where the command entered is not valid
   // complete ...
    continue; // go back to the while loop. NOT to the if (connected) ...
   }
   // send request and read response (if we are connected)
   if (connected == true) { // do the following only if connected
    writer.println(request);  // send the request prepared above
    response = reader.readLine(); // receive the response to it.
    System.out.println("\t"+response); // print out the response
   }
  }
  // close keyboard channel
  keyboard.close();
 }
}

و هذا كود serevr

import java.net.*;
import java.io.*;
class Server{
 public static void main(String[] args) {
  int port = Integer.parseInt(args[0]);
  ServerSocket server;
  try {
   server = new ServerSocket(Integer.parseInt(args[0]));
  } catch (IOException ioe) {
   System.err.println("Couldn't run server on port " + port);
   return;
  }
  while(true) {
   try {
    Socket connection = server.accept();
    ConnectionHandler handler = new ConnectionHandler(connection);
    new Thread(handler).start();
   } catch (IOException ioe) {
   }
  }
 }
}

و هذا كود ConnectionHandler

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

class ConnectionHandler implements Runnable {
 // The connection with the client
 private Socket conn = null;
 PrintWriter writer = null;
 BufferedReader reader = null;
 String request = null;
 String response = null;
 BankAccount account = new BankAccount();
 public ConnectionHandler(Socket conn) {
  this.conn = conn;
 }
 public void run() {
  try {
   writer = new PrintWriter(conn.getOutputStream(), true);
   reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
   String ipaddr = conn.getInetAddress().getHostName();
   int port = conn.getPort();
   System.out.println("Got connection from: " + ipaddr + ":" + port);
   while ((request = reader.readLine()) != null) {
    System.out.println(ipaddr + ":" + port + ": <-\t"+ request);
    if (request.equals("hello")) {
     response = "hello";
    } else if (request.equals("balance")) {
      response = Float.toString(account.balance());
     // complete...
    } else if (request.startsWith("deposit")) {
     // startsWith(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
     // split(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
     // trim(): http://java.sun.com/j2se/1.4.2/docs/...ring.html#trim()
     String amount = request.split(" ")[1].trim();
     // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
     float f = Float.valueOf(amount).floatValue();
     response = Float.toString(account.deposit(f));
    } else if (request.startsWith("withdraw")) {
    // complete  ...
    } else { // this is the case none of the above
    // complete ...
    }
    writer.println(response);
    System.out.println(ipaddr + ":" + port + ": ->\t"+ response);
   }
  } catch (IOException ioe) {
  }
 }
}
#2

مشكور يالغالي على هذه الكود الجميل اللي في الحقيقة الكل في حاجته

وتقبل خالص الشكر مني والتقدير

أخوكم المباركي

#3

هذا التعديل انا بشك فية والله أعلم علشان لازم نحل مشكلة Race Condition اللى ممكن تحصل على ال Balance

public boolean syncronized withdraw(double amount) {
if (balance < amount)
  {
   System.out.println("BankAccount: withdrawal " + amount +
        " but we only have " + balance +
        " in the account!");
  return false;
  }
  balance -= amount;

  System.out.println("BankAccount: withdrawal " + amount +
        "  balance = " + balance);
 return true;
}

انا الحل اللى بقول علية علية دة logiclly وجايز ما يحلش المشكلة اللى واقفة امام الاخت

التوفيق من الله

الخطأ منى ومن الشيطان

www.tanahy.blogspot.com

#4
اقتباس
مشكور يالغالي على هذه الكود الجميل اللي في الحقيقة الكل في حاجته

وتقبل خالص الشكر مني والتقدير

أخوكم المباركي

:blink: :wacko:

بصراحو وددت أن أرد على هذا التعليق لكن أترك الرد لصاحب(ة) الموضوع .... :angry:

فحنوت ألثم جرحه الرعافَ فانتكأت جراحي..

وهمت على خدي الدموع فقلت يا روحي وراحي..

هلّا رحمتَ قلوبنا .. وعدلت عن هذا الرّواحِ ِ ..؟

فأجابني البطل المسجّى هازئاً بيَ .. باقتراحي

كفكف دموعك ليس في عبراتك الحرّى ارتياحي

هذي سبيلي .. إن صدقت محبتي .. فاحمل سلاحي

Sun Certified Java Programmer

#5

مشكور اخويه Eng.AhmedEltanahy على الرد بس جربت الكود الي انت محطنها فيطلع لي error

بالنسبه للاخ almobarki لا تعليق لانه شكله ما قرى حتى الموضوع

و اتمنى احد يرد عليه و يساعدني

و شكرا

#6

هذه الكلاسات كاملة بعد التعديل

public class BankAccount {
  private float balance;
  public BankAccount() {
    balance = 0;
  }

  public float deposit(float amount) {
    balance += amount;

    return balance;
  }

  public boolean withdraw(double amount) {
    if (balance < amount) {
      System.out.println("BankAccount: withdrawal " + amount +
                         " but we only have " + balance +
                         " in the account!");
      return false;
    }
    balance -= amount;

    System.out.println("BankAccount: withdrawal " + amount +
                       "  balance = " + balance);
    return true;
  }

  public float getbalance() {
// complete ...
    if (balance < 0)
      return 0;
    else
      return balance;
  }
}

import java.io.*;
import java.net.*;
public class Client{
static Socket conn = null;
static PrintWriter writer = null;
static BufferedReader reader = null;
// Method which connect the client to the service (identified by port)
// provided by the server (identified by ipaddr).
// If the connection is successful, it return true.
public static boolean connect(String ipaddr, int port) {
 try
 { // Connect to server
  conn = new Socket(ipaddr, port);
 } catch (IOException e) {
  System.err.println("Cannot connect to " + ipaddr +"/"+ port);
  System.exit(1);
 }
 try { // Get a reading and writing channels to the connection.
  writer = new PrintWriter(conn.getOutputStream(), true);
  reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 } catch (IOException e) {
  System.err.println("Couldn't get reading/writing channels to socket");
  System.exit(1);
 }
 return true;
}
public static void main(String[] args) throws IOException {
 String request = null; // Used to send request messages to server
 String response = null;// Used to receive response messages from server
 // Get reading channel from standard input (i.e., keyboard)
 BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
 String command = "null";
 boolean connected = false;
 if(args.length==0)
 {
   System.out.println("You must add the server name and server port to the command line");
      System.out.println("Program will be abborted");
      System.exit(0);
 }
 while (command != null) {
  System.out.print("> ");
  if(command.equals("null"))
    command = keyboard.readLine();
  if (command.equals("connect")) {
   connected = connect(args[0], Integer.parseInt(args[1]));
   request = "hello";
  } else if (command.equals("disconnect")) 
  {
    if(!connected)
    {
      System.out.println("No Connection, you should connect first");
      command="null";
      continue;
    }
    System.out.print("Are you sure you want to disconnect (y,n)?: ");
    String ans = keyboard.readLine();
    if(ans.equalsIgnoreCase("y"))
    {
      connected = false;
      command="null";
      conn.close();
      System.out.println("Connection has been disconeccted");
    }
      
  // complete ...
   
  } else if (command.equals("exit")) 
  {
    System.out.print("Are you sure you want to exit (y,n)?: ");
    String ans = keyboard.readLine();
    if(ans.equalsIgnoreCase("y"))
    {
      connected = false;
      if(conn!=null)
        conn.close();
      System.out.printf("Progarm will end");
      System.exit(0);
    }

  // complete ...
  } else if (command.equals("balance"))
  {
    if(!connected)
  {
    System.out.println("No Connection, you should connect first");
    command="null";
    continue;
  }

    writer.println("balance");  
   response = reader.readLine(); // receive the response to it.
   System.out.println("\t"+response); // print out the response

  // complete ...
  } else if (command.startsWith("deposit")) 
  {
    if(!connected)
  {
    System.out.println("No Connection, you should connect first");
    command="null";
    continue;
  }

    System.out.print("Enter the amount you want to deposit to your Account: ");
    try
    {
      float am = Float.parseFloat(keyboard.readLine());
      writer.println("deposit "+am);  // send the request prepared above
   response = reader.readLine(); // receive the response to it.
   System.out.println("\t"+response); // print out the response

    }
    catch(Exception ex)
    {
      System.out.println("Invalid amount plz try again");
      continue;
    }
  // complete ...
  } else if (command.startsWith("withdraw")) 
  {
    if(!connected)
  {
    System.out.println("No Connection, you should connect first");
    command="null";
    continue;
  }

    System.out.print("Enter the amount you want to withdraw from your Account: ");
    try
    {
      float am = Float.parseFloat(keyboard.readLine());
      writer.println("withdraw "+am);  // send the request prepared above
   response = reader.readLine(); // receive the response to it.
   System.out.println("\t"+response); // print out the response

    }
    catch(Exception ex)
    {
      System.out.println("Invalid amount plz try again");
      continue;
    }

  // complete ...
  } else 
  { // this is the case where the command entered is not valid
  // complete ...
  System.out.println("Invalid command, please try again (connect, balance,deposit,disconnect,exit)");
   continue; // go back to the while loop. NOT to the if (connected) ...
  }
  // send request and read response (if we are connected)
  if (connected == true) 
  { // do the following only if connected
   writer.println(request);  // send the request prepared above
   response = reader.readLine(); // receive the response to it.
   System.out.println("\t"+response); // print out the response
  }
  command="null";
 }
 // close keyboard channel
 keyboard.close();
}
}

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

class ConnectionHandler
    implements Runnable {
// The connection with the client
  private Socket conn = null;
  PrintWriter writer = null;
  BufferedReader reader = null;
  String request = null;
  String response = null;
  BankAccount account = new BankAccount();
  public ConnectionHandler(Socket conn) {
    this.conn = conn;
  }

  public void run() {
    try {
      writer = new PrintWriter(conn.getOutputStream(), true);
      reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String ipaddr = conn.getInetAddress().getHostName();
      int port = conn.getPort();
      System.out.println("Got connection from: " + ipaddr + ":" + port);
      while ( (request = reader.readLine()) != null) {
        System.out.println(ipaddr + ":" + port + ": <-\t" + request);
        if (request.equals("hello")) {
          response = "Server Respond";
        }
        else if (request.equals("balance")) {
          float f = account.getbalance();
          response ="your current balance is: "+ f;
          // complete...
        }
        else if (request.startsWith("deposit")) {
          // startsWith(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          // split(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          // trim(): http://java.sun.com/j2se/1.4.2/docs/...ring.html#trim()
          String amount = request.split(" ")[1].trim();
          // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          float f = Float.valueOf(amount).floatValue();
          response = Float.toString(account.deposit(f));
        }
        else if (request.startsWith("withdraw")) {
          String amount = request.split(" ")[1].trim();
          // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          float f = Float.valueOf(amount).floatValue();
          boolean c= account.withdraw(f);
          if(c)
            response = "withdraw Succeeded";
          else
            response = "withdraw failed>> withdraw amount is too big";
        }
        else { 
            response = "Cannot understnad this command";
            // this is the case none of the above
          // complete ...
        }
        writer.println(response);
        System.out.println(ipaddr + ":" + port + ": ->\t" + response);
      }
    }
    catch (IOException ioe) {
    }
  }
}

import java.net.*;
import java.io.*;
class Server{
public static void main(String[] args) {
 int port = Integer.parseInt(args[0]);
 ServerSocket server;
 try {
  server = new ServerSocket(Integer.parseInt(args[0]));
 } catch (IOException ioe) {
  System.err.println("Couldn't run server on port " + port);
  return;
 }
 while(true) {
  try {
   Socket connection = server.accept();
   ConnectionHandler handler = new ConnectionHandler(connection);
   new Thread(handler).start();
  } catch (IOException ioe) {
  }
 }
}
}

لا بد من صنعاء وان طال السفر

#7

مشكوره san3aniJava

الحمدلله الحين صار يشتغل

الله يجزيك الف خير

#8

السلام عليكم ورحمة الله وبركاته

عندي سؤال كيف اسوي لكل client

4 اكاونت

و اسوي transfer between this accounts and every account have unique ID

و شكرا

و الله يجزيكم خير

#9

تم تعديل جميع الكلاسات بالمطلوب الجديد

وتم اضافاة امر transfer الى قائمة الاوامر السابقة

واكواد الكلاسات هي

1- BanckAccount

public class BankAccount
{
  private float balance;
  private String id;
  public BankAccount(String i)
  {
    balance = 0;
    id = i;
  }

  public float deposit(float amount)
  {
    balance += amount;

    return balance;
  }

  public boolean withdraw(double amount)
  {
    if (balance < amount)
    {
      System.out.println("BankAccount: withdrawal " + amount +
                         " but we only have " + balance +
                         " in the account!");
      return false;
    }
    balance -= amount;

    System.out.println("BankAccount: withdrawal " + amount +
                       "  balance = " + balance);
    return true;
  }

  public float getbalance()
  {
// complete ...
    if (balance < 0)
    {
      return 0;
    }
    else
    {
      return balance;
    }
  }
}

2- Client

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

public class Client
{
  static Socket conn = null;
  static PrintWriter writer = null;
  static BufferedReader reader = null;

  
// Method which connect the client to the service (identified by port)
// provided by the server (identified by ipaddr).
// If the connection is successful, it return true.
  public static boolean connect(String ipaddr, int port)
  {
    try
    { // Connect to server
      conn = new Socket(ipaddr, port);
    }
    catch (IOException e)
    {
      System.err.println("Cannot connect to " + ipaddr + "/" + port);
      System.exit(1);
    }
    try
    { // Get a reading and writing channels to the connection.
      writer = new PrintWriter(conn.getOutputStream(), true);
      reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    }
    catch (IOException e)
    {
      System.err.println("Couldn't get reading/writing channels to socket");
      System.exit(1);
    }
    return true;
  }

  public static void main(String[] args) throws IOException
  {
    String request = null; // Used to send request messages to server
    String response = null; // Used to receive response messages from server
// Get reading channel from standard input (i.e., keyboard)
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.
        in));
    String command = "null";
    boolean connected = false;
    if (args.length == 0)
    {
      System.out.println(
          "You must add the server name and server port to the command line");
      System.out.println("Program will be abborted");
      System.exit(0);
    }
    while (command != null)
    {
      System.out.print("> ");
      if (command.equals("null"))
      {
        command = keyboard.readLine();
      }
      if (command.equals("connect"))
      {
        connected = connect(args[0], Integer.parseInt(args[1]));
        //request = "hello";
        writer.println("hello");
        response = reader.readLine(); // receive the response to it.
        System.out.println("\t" + response); // print out the response
      }
      else if (command.equals("disconnect"))
      {
        if (!connected)
        {
          System.out.println("No Connection, you should connect first");
          command = "null";
          continue;
        }
        System.out.print("Are you sure you want to disconnect (y,n)?: ");
        String ans = keyboard.readLine();
        if (ans.equalsIgnoreCase("y"))
        {
          connected = false;
          command = "null";
          conn.close();
          System.out.println("Connection has been disconeccted");
        }

        // complete ...

      }
      else if (command.equals("exit"))
      {
        System.out.print("Are you sure you want to exit (y,n)?: ");
        String ans = keyboard.readLine();
        if (ans.equalsIgnoreCase("y"))
        {
          connected = false;
          if (conn != null)
          {
            conn.close();
          }
          System.out.printf("Progarm will end");
          System.exit(0);
        }

        // complete ...
      }
      else if (command.equals("balance"))
      {
        if (!connected)
        {
          System.out.println("No Connection, you should connect first");
          command = "null";
          continue;
        }
        System.out.print("Enter the Account id(1-4): ");
        String id = keyboard.readLine();
        writer.println("balance "+id);
        response = reader.readLine(); // receive the response to it.
        System.out.println("\t" + response); // print out the response

        // complete ...
      }
      else if (command.startsWith("deposit"))
      {
        if (!connected)
        {
          System.out.println("No Connection, you should connect first");
          command = "null";
          continue;
        }
        System.out.print("Enter the Account id(1-4): ");
        String id = keyboard.readLine();
        System.out.print(
            "Enter the amount you want to deposit to your Account: ");
        try
        {
          float am = Float.parseFloat(keyboard.readLine());
          writer.println("deposit " + am+ " " + id); // send the request prepared above
          response = reader.readLine(); // receive the response to it.
          System.out.println("\t" + response); // print out the response

        }
        catch (Exception ex)
        {
          System.out.println("Invalid amount plz try again");
          continue;
        }
        // complete ...
      }
      else if (command.startsWith("withdraw"))
      {
        if (!connected)
        {
          System.out.println("No Connection, you should connect first");
          command = "null";
          continue;
        }
        System.out.print("Enter the Account id(1-4): ");
        String id = keyboard.readLine();
        System.out.print(
            "Enter the amount you want to withdraw from your Account: ");
        try
        {
          float am = Float.parseFloat(keyboard.readLine());
          writer.println("withdraw " + am+" " +id); // send the request prepared above
          response = reader.readLine(); // receive the response to it.
          System.out.println("\t" + response); // print out the response

        }
        catch (Exception ex)
        {
          System.out.println("Invalid amount plz try again");
          continue;
        }

        // complete ...
      }
      else if (command.startsWith("transfer"))
      {
        if (!connected)
        {
          System.out.println("No Connection, you should connect first");
          command = "null";
          continue;
        }
        System.out.print("Enter the Account id you want to transfer from (1-4): ");
        String id1 = keyboard.readLine();
        System.out.print("Enter the Account id you want to transfer to (1-4): ");
        String id2 = keyboard.readLine();
        System.out.print(
            "Enter the amount you want to transfer: ");
        try
        {
          float am = Float.parseFloat(keyboard.readLine());
          writer.println("transfer " + am+" " +id1 +" "+ id2); // send the request prepared above
          response = reader.readLine(); // receive the response to it.
          System.out.println("\t" + response); // print out the response

        }
        catch (Exception ex)
        {
          System.out.println("Invalid amount plz try again");
          continue;
        }

        // complete ...
      }

      else
      { // this is the case where the command entered is not valid
        // complete ...
        System.out.println(
            "Invalid command, please try again (connect, balance,deposit,transfer,disconnect,exit)");
        command="null";
        continue; // go back to the while loop. NOT to the if (connected) ...
      }
      // send request and read response (if we are connected)
      
      command = "null";
    }
// close keyboard channel
    keyboard.close();
  }
}

3- ConnectionHandler

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

class ConnectionHandler
    implements Runnable
{
// The connection with the client
  private Socket conn = null;
  PrintWriter writer = null;
  BufferedReader reader = null;
  String request = null;
  String response = null;
  static int id;
  static String ipaddr;
  static int port;
   BankAccount[] ba = new BankAccount[4];
  public ConnectionHandler(Socket conn)
  {
    this.conn = conn;
    try
    {
      writer = new PrintWriter(conn.getOutputStream(), true);
      reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      ipaddr = conn.getInetAddress().getHostName();
     port = conn.getPort();
     System.out.println("Got connection from: " + ipaddr + ":" + port);

    }
    catch (Exception ex)
    {
    }
     

  }
  private void declare()
    {
      for(int i =1;i<=ba.length;i++)
        ba[i-1]= new BankAccount(i+"");
  }
  public void run()
  {
    try
    {
      
      while ( (request = reader.readLine()) != null)
      {
        System.out.println(ipaddr + ":" + port + ": <-\t" + request);
        if (request.equals("hello"))
        {
          declare();
          response = "Server Respond";
        }
        else if (request.startsWith("balance"))
        {
          id = Integer.parseInt(""+request.charAt(request.length()-1));
          float f = ba[id-1].getbalance();
          response = "your current balance is: " + f;
          // complete...
        }
        else if (request.startsWith("deposit"))
        {
          // startsWith(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          // split(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          // trim(): http://java.sun.com/j2se/1.4.2/docs/...ring.html#trim()
          String amount = request.split(" ")[1].trim();
           id = Integer.parseInt(""+request.charAt(request.length()-1));
          // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          float f = Float.valueOf(amount).floatValue();
          response = Float.toString(ba[id-1].deposit(f));
        }
        else if (request.startsWith("withdraw"))
        {
          String amount = request.split(" ")[1].trim();
          // floatValue(): http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
          id = Integer.parseInt(""+request.charAt(request.length()-1));
          float f = Float.valueOf(amount).floatValue();
          boolean c = ba[id-1].withdraw(f);
          if (c)
          {
            response = "withdraw Succeeded";
          }
          else
          {
            response = "withdraw failed>> withdraw amount is too big";
          }
        }
        else if (request.startsWith("transfer"))
        {

          String temp[] = request.split(" ");
          String amount = temp[1].trim();
          int id1 = Integer.parseInt(temp[2]);
          int id2 = Integer.parseInt(temp[3]);
          float f = Float.valueOf(amount).floatValue();
          ba[id2-1].getbalance();
          boolean c = ba[id1-1].withdraw(f);
          if (c)
          {
            
            response = "transfer Succeeded";
            ba[id2-1].deposit(f);
          }
          else
          {
            response = "transfer failed>> transfer amount is too big";
          }

          //response = Float.toString(ba[id-1].deposit(f));
        }

        else
        {
          response = "Cannot understnad this command";
          // this is the case none of the above
          // complete ...
        }
        writer.println(response);
        System.out.println(ipaddr + ":" + port + ": ->\t" + response);
      }
    }
    catch (IOException ioe)
    {
      System.out.println(ioe);
    }
    catch (Exception ioe)
    {
      System.out.println(ioe);
       writer.println("Invalid id: "+ id);
       run();
    }
  }
}

4- Server لم يعدل عليه شيء

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

class Server
{
  public static void main(String[] args)
  {
    int port = 0;
    ServerSocket server;
    try
    {
      port = Integer.parseInt(args[0]);
      server = new ServerSocket(Integer.parseInt(args[0]));
    }
    catch (IOException ioe)
    {
      System.err.println("Couldn't run server on port " + port);
      return;
    }
    catch (Exception ioe)
    {
      System.out.print(ioe);
      return;
    }

    while (true)
    {
      try
      {
        Socket connection = server.accept();
        ConnectionHandler handler = new ConnectionHandler(connection);
        new Thread(handler).start();
      }
      catch (Exception ioe)
      {
        System.out.print(ioe);
      }
    }
  }
}

لا بد من صنعاء وان طال السفر

#10

مشكوره اختي ما قصرتي الله يجزيج الف خير

هذا الموضوع مغلق.

مواضيع مشابهة