Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:09:07+00:00 2026-05-30T16:09:07+00:00

I have the following Observer: public class Fisc implements Observer { double value1; double

  • 0

I have the following Observer:

public class Fisc implements Observer {
double value1;
double value2;
private static HashMap<String, Deposit> accounts=new HashMap<String,Deposit>();
public Fisc(String cnp,Deposit deposit) {

    System.out.println("\n*******Observer*******Watching account:"+id);
    accounts.put(id, deposit);

}

public void update(Observable obj, Object arg) {
    if (arg instanceof Deposit) {
        value1 =((Deposit) arg).getvalue1();
        value2=((Deposit) arg).getvalue2();
        System.out.println("\n*******Observer*******value1 current value:"+value1);
        System.out.println("*******Observer*******value2 current value:"+value2);
    } else {
        System.out.println("Observable error!");
    }
}
}

and the Observable:

import java.util.HashMap;
import java.util.Observable;


public class obs extends Observable {

    private static HashMap<String, Deposit> accounts;

    private static obs instance;

    private obs(HashMap<String,Deposit> accounts){
        obs.accounts=accounts;
    }

    public static obs getInstance(){
        if (instance==null){
            return new obs(new HashMap<String,Deposit>());
        }
        else return instance;
    }

        // ... some unimportant other stuff

    public void depositvalue1(String id,double value1){
        Deposit deposit=accounts.get(id);
        deposit.addvalue1(value1);

        if(deposit.isWatchedByFisc()){
            notifyFisc(deposit);
        }
        System.out.println("obs:Deposited "+value1+ " value1 to account:"+id+"!");
        System.out.println("obs:Current value1 in account:"+deposit.getvalue1());
    }

    public void depositvalue2(String id,double value2){
        Deposit deposit=accounts.get(id);
        deposit.addvalue2(value2);

        if(deposit.isWatchedByFisc()){
            notifyFisc(deposit);
        }

        System.out.println("obs:Deposited "+value2+" value2 to account:"+id+"!");
        System.out.println("obs:Current value1 in account:"+deposit.getvalue2());
    }

    public void depositValues(String id,double value1,double value2){
        Deposit deposit=accounts.get(id);
        deposit.addvalue1(value1);
        deposit.addvalue2(value2);

        if(deposit.isWatchedByFisc()){
            notifyFisc(deposit);
        }

        System.out.println("obs:Deposited "+value1+ " value1 and "+value2+" value2 to account"+id+"!");
        System.out.println("obs:Current value1 in account:"+deposit.getvalue1());
    }

    public void watchAccount(String id){
        Deposit deposit=accounts.get(id);
        deposit.setWatchedByFisc(true);
        addObserver(new Fisc(id,deposit));
    }

    public void stopWatchAccount(String id){
        accounts.get(id).setWatchedByFisc(false);
        System.out.println("obs:Account "+id+" is no longer watched by Fisc!");
    }

    public void notifyFisc(Deposit deposit){
        setChanged();
        notifyObservers(deposit);
    }

}

Everything works as suppossed except the following: If I use the depositValue(1,2,s) methods instead of getting the message once, I get the same message the number of times I have registered deposits to be watched. How can I fix this?

Hope this makes sense. Thanks in advance and sorry if this is a stupid question this is the first time using Observer Pattern.

My guess is this line maybe(multiple instances?): addObserver(new Fisc(id,deposit));

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:09:08+00:00Added an answer on May 30, 2026 at 4:09 pm

    Every observer (an instance of Fisc) is notified whenever a Deposit instance has changed. So with your code, every Fisc has to look at the notification message and check, if it is about its deposit.

    If you don’t want that, then you should make the Deposit observable (instead of observing the whole bank). Then you can register listeners to individual deposits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following classes. class A { public: void fun(); } class B: public
Why does this happen? Please observe the following code: static class StringExtension { public
I have the following notification sent for an Observer and Observable design pattern private
I have the following two classes: TcmTPDataPanel = class(TcmTPBasePanel) Database: TnxDatabase; Session: TnxSession; private
I have the following problem. Given a Interface EventNotifier for the Observer Pattern: public
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.