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

I have problem when i use more than one method. if i use stub.deposit(12000); th

ID: 3878645 • Letter: I

Question

I have problem when i use more than one method. if i use stub.deposit(12000); the program run well but if i add  stub.interest(); the compiler give me 0 in interest and the deposit not sum the previos balance.
should i add any code in Accountserver?

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
* AccountClient: An example client for an RMI-exported Account.
*/

public class AccountClient {
public static void main(String args[]) {
String host;
if (args.length < 1)
host = null;
else
host = args[0];
try {
// Lookup account object
Registry registry = LocateRegistry.getRegistry(host);
Account stub = (Account) registry.lookup("BlobbyAccount");
  
// Make deposit
stub.deposit(12000);
// make interest
stub.interest();

// Report results and balance.
System.out.println("Deposited 12,000 into account owned by " +
stub.getName());
System.out.println("Balance now totals: " + stub.getBalance());
//calculates the interest on the current balance at a value of 5%;
// stub.interest();
// System.out.println("Balance after calculate Interest: " + stub.getBalance());
// stub.deposit(100);
// System.out.println("Balance now total: " + stub.getBalance());
}
catch (RemoteException re) {
System.err.println("Remote exception while looking up account: "
+ re.getMessage());
}
catch (NotBoundException nbe) {
System.err.println("Server object not found at expected URL: "
+ nbe.getMessage());
}
}
}

import java.rmi.RemoteException;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

import java.rmi.server.UnicastRemoteObject;

/**

* AccountServer: A utility class that registers an Account with the local

* RMI registry for remote access

*/

public class AccountServer {

public static void main(String argv[]) {

try {

// Make an Account with a given name

AccountImpl acct = new AccountImpl("Mr_Blobby");

Account stub = (Account) UnicastRemoteObject.exportObject(acct, 0);

  

// Register it with the local naming registry

Registry registry = LocateRegistry.getRegistry();

registry.rebind("BlobbyAccount", stub);

System.out.println(" Registered account as BlobbyAccount");

  

// Keep the server process alive indefinitely

Object dummy = new Object();

synchronized (dummy) {

try {

dummy.wait();

}

catch (InterruptedException ie) {}

}

}

catch (RemoteException re) {

System.err.println("Remote exception while creating/registering: "

+ re.getMessage());

}

}

Explanation / Answer

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
* Account: A simple RMI interface that represents a banking account.
*/
public interface Account extends Remote {

    // Get the name on the account

    public String getName() throws RemoteException;

    // Get current balance

    public float getBalance() throws RemoteException;

    // Take some money away

    public void withdraw(float amt)
            throws RemoteException, InsufficientFundsException;

    // Put some money in

    public void deposit(float amt) throws RemoteException;

    // Move some money from one account into this one

    public void transfer(float amt, Account src)
            throws RemoteException, InsufficientFundsException;
  
    public float calculateInterest(float interestRatePercent) throws RemoteException;;
}

-----------------------------------------------------------------------------------------------------------------
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
* AccountClient: An example client for an RMI-exported Account.
*/
public class AccountClient {

    public static void main(String args[]) {
        String host;
        if (args.length < 1) {
            host = null;
        } else {
            host = args[0];
        }
      
        try {
            // Lookup account object
            Registry registry = LocateRegistry.getRegistry(host);
          
            //List names in registry
            String[] registryList = registry.list();
            System.out.println("Listing entries in registry:");
            for (String registryListEntry : registryList) {
                System.out.println("Registry entry: " + registryListEntry);
            }
          
            //Get account stub from registry
            Account stub = (Account) registry.lookup("BlobbyAccount");

            // Make deposit
            stub.deposit(12000);

            // Report results and balance.
            System.out.println("Deposited 12,000 into account owned by "
                    + stub.getName());
            System.out.println("Balance now totals: " + stub.getBalance());
          
            //Calculate interest on current balance
            float interestCalculation = stub.calculateInterest(5);
            System.out.println("Interest calculated on current balance is: " + interestCalculation);
          
        } catch (RemoteException re) {
            System.err.println("Remote exception while looking up account: "
                    + re.getMessage());
        } catch (NotBoundException nbe) {
            System.err.println("Server object not found at expected URL: "
                    + nbe.getMessage());
        }
    }
}
--------------------------------------------------------------------------------------------------------------
import java.io.Serializable;
import java.rmi.RemoteException;

/**
* AccountImpl: Implementation of the Account remote interface.
*/

public class AccountImpl implements Account, Serializable {
// Our current balance
private float accBalance = 0;
// Name on account
private String accName = "";
// Create a new account with the given name
public AccountImpl(String name) throws RemoteException {
    accName = name;
}

public String getName() throws RemoteException {
    return accName;
}
public float getBalance() throws RemoteException {
    return accBalance;
}
// Withdraw some funds
public void withdraw(float amt)
throws RemoteException, InsufficientFundsException {
    if (accBalance >= amt) {
      accBalance -= amt;
      // Log transaction...
      System.out.println("--> Withdrew " + amt +
                         " from account " + getName());
      System.out.println("    New balance: " + getBalance());
    }
    else {
      throw new InsufficientFundsException("Withdrawal request of " +
                                           amt + " exceeds balance of "
                                           + accBalance);
    }
}

// Deposit some funds
public void deposit(float amt) throws RemoteException {
    accBalance += amt;
    // Log transaction...
    System.out.println("--> Deposited " + amt +
                       " into account " + getName());
    System.out.println("    New balance: " + getBalance());
}
// Move some funds from another (remote) account into this one
public void transfer(float amt, Account src)
throws RemoteException, InsufficientFundsException {
    if (checkTransfer(src, amt)) {
      src.withdraw(amt);
      this.deposit(amt);
      // Log transaction...
      System.out.println("--> Transferred " + amt +
                         " from account " + getName());
      System.out.println("    New balance: " + getBalance());
    }
    else {
      throw new InsufficientFundsException("Source account balance " +
      "is insufficient for transfer");
    }
}

// Check to see if the transfer is possible, given the source account
private boolean checkTransfer(Account src, float amt) {
    boolean approved = false;
    try {
      if (src.getBalance() >= amt) {
        approved = true;
      }
    }
    catch (RemoteException re) {
      // If some remote exception occurred, then the transfer is still
      // compromised, so return false
      approved = false;
    }
    return approved;
}

/**
   *
   * @param interestRatePercent Interest rate in percent e.g. 5
   * @return float Calculated interest rate. Can return 0 if an error
   */
public float calculateInterest(float interestRatePercent)
{
      float calculated = 0;
      float balance = 0;
    
      try
      {
          balance = getBalance();
          calculated = (float) balance * (interestRatePercent / 100);
      }
      catch (RemoteException re) {
          //calculated = (float) 0;
      }
    
      System.out.println("Calculated " + interestRatePercent + "% interest on balance " + balance + " as: " + calculated);
    
      return calculated;
}
}
--------------------------------------------------------------------------------------------------------------------------------
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

/**
* AccountServer: A utility class that registers an Account with the local
* RMI registry for remote access
*/

public class AccountServer {
    public static void main(String argv[]) {
        try {
            // Make an Account with a given name
            AccountImpl acct = new AccountImpl("Mr_Blobby");
         Account stub = (Account) UnicastRemoteObject.exportObject(acct, 0);
            // Register it with the local naming registry
            Registry registry = LocateRegistry.getRegistry();
        registry.rebind("BlobbyAccount", stub);

            System.out.println(" Registered account as BlobbyAccount");
          
            // Keep the server process alive indefinitely
            Object dummy = new Object();
            synchronized (dummy) {
                try {
                    dummy.wait();
                }
                catch (InterruptedException ie) {}
            }
        }
        catch (RemoteException re) {
            System.err.println("Remote exception while creating/registering: "
                               + re.getMessage());
        }
    }
}
--------------------------------------------------------------------------------------------------------------
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String msg) {
    super(msg);
}

public InsufficientFundsException() {
    super();
}
}