mardi 27 octobre 2020

Money pattern implementation in Java

Are there any techniques on implementing a simplified version of the money pattern in Java with Classes? With my current implementation there are three classes- Money, Rate and Currency which hold variables and references to other tables by referencing the object inside the respective class. Which currently only are implemented in Money and Currency. Currency has a relationship to MyClass1 that contains lists for currencies and the various exchange rates to convert the currencies into other rates vice versa from the amount specified in the Money class.

What would be the correct design patterns in using the money pattern in a simplified way or use the classes later for doing financial operations such as converting account money from one currency to another when withdrawing from an ATM abroad. Or using the same code for calculating loans with the same principles specifiec in those classes without really changing any logic it contains? There is Fowler Money which does calculations and stores the values in a class. Might there be a way to construct or design such a money object that would allow easily employ it in ways described above.

Current code has the following attributes;

public class Rate {
    
public float Rate;
public string TargetRate;
public string BaseRate;

}

public class Currency {
    
public string Name;
public MyClass1 MyClass1;
public string ISOCode; // uses ISO 4127

}

public class Money {

public Currency currency;
public decimal amount; // incremented money from methods

public decimal TallyAmount(decimal newamount) {
    
    return amount = amount + newamount;
   }

public decimal ReduceMoney(decimal amountToReduce) {

    return amount = amount - amountToReduce;
   }
}

Implementation is invoked in tests where list initialization also works via unit tests. Would it be reasonable to include a list of Rates in MyClass1 where the then MyClass1 would implement those classes using those class objects and using operations such as converting and receiving the exchange rate from the list that is inside the same class mimicking real currency data which will be populated with API calls in the future from another class.

public class MyClass1 { 

public List<Currency> currencies= new ArrayList<Currency>();

public List<Rate> exchangeRates = new ArrayList<Rate>();

public Currency AppendCurrency(Currency toAdd) { // to add a currency into the list from test setUp
       
       currencies.Add(toAdd);
       // ... currencies
       return toAdd;
  }

public decimal Transform(Money money) {
       // convert between currencies using exchange rate list
     
       // ...  calculations            

       return convertedMoney; // convertedMoney is typeof Money 
                              // Money contains relationship mappings into Currency and Currency
                              // into MyClass1. So money can be accessed both ways.
  }

public decimal GetRate(Currency for) {
 
       // .. exchange rates using the rates list.
       return decimalReturn;// Rate better?
       
  }

public decimal ChangeRate(Rate rate) {
 
       // .. change a current rate in the system
       return newRateInTheRateList; // typeof(Rate) or decimal
       
  }



}

The currencies will be populated from the tests right now. Would this behaviour be sufficient in managing this pattern with external devices when the usage of this class will be improved in the future. Mainly hesitant on how to do the conversions between exchange rates from unit tests. The list must be popultate with rate data as well into MyClass1. Would this simplified version of the money pattern suffice or how to design it better so it would be useful in deploying and injecting it for development as a simplified archetype pattern?

Would this be a good beginning to start using that in an enterprise as a simplified model for the money.(loan.bank account.). Are there any ways on how to improve this or what sources to read for creating a cleaner and durable pattern that would not be so prone to change. Would storing lists in MyClass1 a great idea? GoF. Grasp patterns.

Aucun commentaire:

Enregistrer un commentaire