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

Implement the Observable pattern for ServerSolution, particularly observe when a

ID: 3670607 • Letter: I

Question

Implement the Observable pattern for ServerSolution, particularly observe when an Account is added to the List of accounts that ServerSolution maintains. When it does have the observer print a message to the console: “Account NNN has been added, there are now XXX accounts on the server” where NNN is the name of the Account object added, and XXX is the new number of Account objects in the list after the add was successful. Use Java’s built-in Observer class and Observable interface to create your solution.

class ServerSolution implements IAccountServer {

   static String fileName = "accounts.ser";

   List<AAccount> accountList = new ArrayList<AAccount>();

   public ServerSolution() {

       File file = new File(fileName);

       ObjectInputStream in = null;

       try {

           if (file.exists()) {

               System.out.println("Reading from file " + fileName + "...");

               in = new ObjectInputStream(new FileInputStream(file));

               Integer sizeI = (Integer) in.readObject();

               int size = sizeI.intValue();

               for (int i=0; i < size; i++) {

                   AAccount acc = (AAccount) in.readObject();

                   accountList.add(acc);

               }

           }

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           if (in != null) {

               try {

                   in.close();

               } catch (IOException iexc) {

                   iexc.printStackTrace();

               }

           }

       }

   }

   public void newAccount(String type, String name, float balance) {

       AAccount acc;

       if ("Checking".equals(type)) {

           acc = new Checking(name, balance);

       } else if ("Savings".equals(type)) {

           acc = new Savings(name, balance);

       } else {

           throw new IllegalArgumentException("Bad account type:" + type);

       }

       accountList.add(acc);

   }

   public void update(AAccount account) {

       int index = findIndex(account.getName());

       if (index < 0) {

           throw new IllegalStateException("Account not found:" + account);

       }

       accountList.remove(index);

       accountList.add(account);

   }

   public AAccount getAccount(String name) {

       int index = findIndex(name);

       if (index < 0)

           return null;

       return accountList.get(index);

   }

   public List<AAccount> getAllAccounts() {

       return accountList;

   }

   public List<AAccount> getOverdrawnAccounts() {

       List<AAccount> result = new ArrayList<AAccount>();

       for (int i=0; i < accountList.size(); i++) {

           AAccount acc = accountList.get(i);

           if (acc.getBalance() < 0) {

               result.add(acc);

           }

       }

       return result;

   }

   public void shutdown() {

       ObjectOutputStream out = null;

       try {

           out = new ObjectOutputStream(new FileOutputStream(fileName));

           out.writeObject(Integer.valueOf(accountList.size()));

           for (int i=0; i < accountList.size(); i++) {

               out.writeObject(accountList.get(i));

           }

       } catch (Exception e) {

           System.out.println("Could not write file:" + fileName);

           e.printStackTrace();

       } finally {

           if (out != null) {

               try {

                   out.close();

               } catch (IOException oexc) {

                   oexc.printStackTrace();

               }

           }

       }

   }

   protected int findIndex(String name) {

       for (int i=0; i < accountList.size(); i++) {

           AAccount acc = accountList.get(i);

           if (name.equals(acc.getName())) {

               return i;

           }

       }

       return -1;

   }

}

Explanation / Answer

If you have any doubts post comments on the answer.

For implementing an observer,observer class should implement Observer interface and implement the update method which will be called whenever notifyObservers will be called by ServerSolution.

ServerSolution.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

class ServerSolution extends Observable implements IAccountServer {

static String fileName = "accounts.ser";

List<AAccount> accountList = new ArrayList<AAccount>();

public ServerSolution() {
File file = new File(fileName);
ObjectInputStream in = null;
try {
if (file.exists()) {
System.out.println("Reading from file " + fileName + "...");
in = new ObjectInputStream(new FileInputStream(file));

Integer sizeI = (Integer) in.readObject();
int size = sizeI.intValue();
for (int i=0; i < size; i++) {
AAccount acc = (AAccount) in.readObject();
accountList.add(acc);
//account is added so calling set changed which makes the flag true that observable entity has changed
setChanged();
//notifying all observers,which will call the update method for all observers and pass in the string provided
//as a parameter which will be displayed by the observer
notifyObservers("Account "+acc.getName()+" has been added, there are now "+accountList.size()+
       " accounts on the server");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException iexc) {
iexc.printStackTrace();
}
}
}
}
public void newAccount(String type, String name, float balance) {
AAccount acc;
if ("Checking".equals(type)) {
acc = new Checking(name, balance);

} else if ("Savings".equals(type)) {
acc = new Savings(name, balance);

} else {
throw new IllegalArgumentException("Bad account type:" + type);
}
accountList.add(acc);
setChanged();
notifyObservers("Account "+acc.getName()+" has been added, there are now "+accountList.size()+
       " accounts on the server");
}

public void update(AAccount account) {
int index = findIndex(account.getName());
if (index < 0) {
throw new IllegalStateException("Account not found:" + account);
}

accountList.remove(index);
accountList.add(account);
setChanged();
notifyObservers("Account "+account.getName()+" has been added, there are now "+accountList.size()+
       " accounts on the server");
}

public AAccount getAccount(String name) {
int index = findIndex(name);
if (index < 0)
return null;

return accountList.get(index);
}

public List<AAccount> getAllAccounts() {
return accountList;
}

public List<AAccount> getOverdrawnAccounts() {
List<AAccount> result = new ArrayList<AAccount>();

for (int i=0; i < accountList.size(); i++) {
AAccount acc = accountList.get(i);
if (acc.getBalance() < 0) {
result.add(acc);
}
}

return result;
}

public void shutdown() {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(fileName));

out.writeObject(Integer.valueOf(accountList.size()));
for (int i=0; i < accountList.size(); i++) {
out.writeObject(accountList.get(i));
}
} catch (Exception e) {
System.out.println("Could not write file:" + fileName);
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException oexc) {
oexc.printStackTrace();
}
}
}
}

protected int findIndex(String name) {

for (int i=0; i < accountList.size(); i++) {
AAccount acc = accountList.get(i);
if (name.equals(acc.getName())) {
return i;
}
}
return -1;
}
}