Question

A command line currency converter application that prompts for a user input of source currency, source currency code and target currency code e.g.

C:\workspace> java CurrencyConverter 100.50 EUR GBP

The application returns the value of the source amount converted to the target currency e.g. for the above input it returns

100.50 EUR = 86.33 GBP

After displaying the converted value the program exits.

The available exchange rates (GBP based) are in a comma separated value file. Format of this file is COUNTRY,NAME,CODE,RATE e.g.

United Arab Emirates, Dirhams, AED, 7.2104
Australia, Dollars, AUD, 1.51239
Bosnia and Herzegovina, Convertible Marka, BAM, 2.60565
Bulgaria, Leva, BGN, 2.60948

I have a single java file which does the things but how can I convert it into well designed, extensible and maintainable form that adheres to good OO principals?

Should I consider any design pattern, if yes, what different types of objects/interfaces required and their relationships with each other?

Était-ce utile?

La solution

Some design aspects I thought of

  • Reading CSV file: Create an ExchangeRateReader factory so that exchange rates files of various format can be used as an input.

  • (Objectify) ExchangeRate POJO objects contains code, name, country and rate

  • Concrete factory class produces ExchangeRate objects reading from CSV file

  • Use of Enum for ReaderType: CVS, TEST, EXCEL //the factory relies on to create appropriate instance (concrete factory instance)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top