0

Let me give you an example on what I need help with:

Main class:

cnd:Currency 
usd:Currency 
cc:Combined_Currency 
usd := cc.usd
cnd := cc.cnd

So, the class Combined_Currency has a class Currency. The Currency class has attributes for example exchange_rate. I believe the Combined_Currency class would have something like:

cnd:Currency 
usd:Currency 

My question is how do I implement the exchange_rate part? I think exchange_rate is a constant, so I don't need to set anything to it. I need it to check if: cad.exchange_rate = 1.5

In which class to I set the exchange_rate to it's corresponding currencies? and could someone give me an idea what cad := cc.cad means.

J0natthaaann
  • 555
  • 1
  • 6
  • 11
  • I've tried doing `cnd.exchange_rate := 1.5` in the `Combined_Currency` class, but there's a compile error. – J0natthaaann Feb 17 '14 at 18:08
  • If you want to do "cnd.exchange_rate := 1.5", you have to use an assigner. See: http://docs.eiffel.com/book/examples/example-self-initializing-attributes-and-assigner-commands . Also, why do you say that exchange_rate must be a constant and you try to assign a value to it from a class client. – Louis M Feb 17 '14 at 18:42
  • Well, initially I have to assign a value to `cnd.exchange_rate` – J0natthaaann Feb 17 '14 at 19:38
  • And I'm not allowed to do this: `create cnd.make ( 1.5)` – J0natthaaann Feb 17 '14 at 19:42
  • 1
    Well, if you cannot use a creator to pass the value, you have to use an assigner (or a setter without the assigner). If you want to be sure that only one class can use the setter, you can set the classes that can access features in the the feature close. See: http://docs.eiffel.com/book/eiffelstudio/feature-clauses . – Louis M Feb 17 '14 at 20:00

1 Answers1

0

Here is what I would do: First, I would set the class Currency as a deferred and create sub classes to represent every currencies (cad, usd, etc.). I would put the exchange_rate in the Currency class. Of course, if you think that you will handle lots of currencies, you can use a non deferred Currency class and use the creator to initialize the attributes. After that, I would choose a base currency (example usd). In that currency, the currency_rate will always be 1 and the value of the currency_rate in every other Currency will be relative to the base.

The "cad := cc.cad" is use to get a local reference (cad) of an abject attribute (cc.cad).

Louis M
  • 576
  • 2
  • 4