SOURCECODE |
How to... incorporate a MoneyBag on a Currency
Description:
This data structure is meant to count the number of coins and notes in the associated Currency.
It is a special CountingStock on a special Catalog.
(See also: HowTo..incorporateACurrency, HowTo..define a new Currency, HowTo..incorporate a CountingStock on a Catalog )
The use of MoneyBagImpl, the implementation of the interface MoneyBag, is sufficient in most cases.
ToDo's:
- Get the instance of Currency you want to incorporate a MoneyBag on.
( ex.: CurrencyImpl ciRef = (CurrencyImpl) Shop.getTheShop().getCatalog(String sName)
)
- Make a new instance of MoneyBag associated with the Currency.
( MoneyBagImpl m = new MoneyBagImpl(String sName, CurrencyImpl ciRef)
)
- Add the MoneyBag to the Shop´s global list of stocks (
Shop.getTheShop().addStock(Stock m)
).
- Add and remove coins and notes as you wish.
( m.add(String sKey, int nCount, DataBasket db); m.remove(String sKey, int nCount, DataBasket db)
)
Uses:
MoneyBag MoneyBagImpl
// mainly imports
import data.ooimpl.MoneyBagImpl;
// Main Class
public class MyShop extends Shop
{
// Constructor
public MyShop()
{
super();
}
// Main Method
public static void main (String[] noArgs)
{
// the singleton instance of the shop
MyShop myshop = new MyShop();
1
// getting the currency
usCatalog = (CurrencyImpl) myshop.getCatalog("US_Catalog");
2
// a new moneybag
MoneyBagImpl usbag = new MoneyBagImpl("US_Bag",usCatalog);
3
// adding the bag to the shop
myshop.addStock(usbag);
4
// adding five of each coin and note
for (int i = 0; i < CurrencyUS.US$.length; i++)
usbag.add(CurrencyUS.US$[i], 5, (DataBasket) null);
}
}