Code Monkey home page Code Monkey logo

oopattern_observer's Introduction

Observer exercise - MoneyUnit observing ExchangeRatio

Using the Java library interface Observer and class Observable (read about them in Java API Documentation) implement a following currency system:

A class MoneyBag contains a collection (list) of MoneyUnits. Each MoneyUnit is an amount of money in a specific currency (USD, PLN, EUR, GBP, ...). It has also a third field: value holding the value of this unit in Euro independently of the real unit currency.

In the system there is also a CurrencyRatios class containing a collection of ExchangeRatios for each used in the system currency (ex. for PLN - how many PLN one should pay for 1 EUR). ExchangeRatio objects can be stored in CurrencyRatios object in a HashMap<String,ExchangeRatio> for easier currency finding. This class can be implemented with the Singleton pattern.

When one currency exchange ratio changes, all MoneyUnit expressed in this currency should update their value fields (expressed always in EUR). To achieve this, make each MoneyUnit observing (by implementing the Observer interface and registering it to the correct ExchangeRatio object) one specific ExchangeRatio (which should inherit from the Observable class)

Create one or more MoneyBags filled with several MoneyUnits in different currencies. Change few exchange ratios and observe, how the money unit values react and stay up to date.

Hint:

  • Each method changing the exchange ratio should also call a setChanged() method (inherited from the Observable class) before the notifyObservers method (also inherited from the Observable class) is called.
  • Managing the observers (e.g. specifying which observer observes which observable - in other words: registering observers to the observable) should be done using methods inherited from the Observable class (addObserver, ...).

oopattern_observer's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.