Greedy Algorithm java in map -


i'm working on emulator of atm in java. overall pattern in project command. have 4 commands - getinfo, deposit,withdraw , exit.

i'm facing problems implementation of greedy algorithm in withdrawal method. should return map first integer "denomination" , second integer "amount" left in atm after withdrew.

public map<integer, integer> withdrawamount(int expectedamount)  

so takes expected amount argument , has subtract atm least possible amount of bills.

   public class currencymanipulator     { // denominations map each denomination , it's quantity stored         private string currencycode;         private map<integer, integer> denominations = new hashmap<>();          public string getcurrencycode()         {             return currencycode;         }      public currencymanipulator(string currencycode)     {         this.currencycode = currencycode;     }      public void addamount(int denomination, int count)     {         if (denominations.containskey(denomination))         {             denominations.put(denomination, denominations.get(count) + count);         } else         {             denominations.put(denomination, count);         }     }      public int gettotalamount()     {         int sum = 0;         (map.entry<integer, integer> pair : denominations.entryset())         {             sum = pair.getkey() * pair.getvalue();         }         return sum;     }      public boolean hasmoney()     {         return denominations.size() != 0;     }      public boolean isamountavailable(int expectedamount)     {         return expectedamount <= gettotalamount();     }       public map<integer, integer> withdrawamount(int expectedamount) throws notenoughmoneyexception     {       } }  

so need method return map or throw exception if amount asked "expectedamount" higher money available in atm.

if take $600 - 3 bills: $500 + $50 + $50 or $200 + $200 + $200, preferred option $500 + $50 + $50 example, have give $600 atm has following bill-count:

500 - 2

200 - 3

100 - 1

50 - 12

the result should be:

500 - 1

100 - 1

this came with:

public map<integer, integer> withdrawamount(int expectedamount) throws notenoughmoneyexception     {          denominations.put(50,1);         denominations.put(500,1);         denominations.put(200,3);         hashmap<integer, integer> map = new hashmap<>();         treemap<integer, integer> sortedmap = new treemap<>(collections.reverseorder());         sortedmap.putall(denominations);         arraylist<integer> bills = new arraylist<>();         bills.addall(sortedmap.keyset());           int num;          (int = 0; < bills.size(); i++)         {             if (bills.get(i) <= expectedamount)             {                 num = expectedamount / bills.get(i);                 map.put(bills.get(i), num);                 expectedamount -= num * bills.get(i);             }         }         system.out.println(map);         return map;     } 

it returns map of needed bills , quantity.

now question is..how compare "denominations" map have , subtract new map it?

seems working code if ever needs

public map<integer, integer> withdrawamount(int expectedamount) throws notenoughmoneyexception     {           denominations.put(50,2);         denominations.put(500,1);         denominations.put(100,1);         hashmap<integer, integer> map = new hashmap<>();          treemap<integer, integer> sorteddenominations = new treemap<>(collections.reverseorder());         sorteddenominations.putall(denominations);         arraylist<integer> bills = new arraylist<>();         bills.addall(sorteddenominations.keyset());           int num;          (int = 0; < bills.size(); i++)         {             if (bills.get(i) <= expectedamount)             {                 num = expectedamount / bills.get(i);                 map.put(bills.get(i), num);                 expectedamount -= num * bills.get(i);             }         }         system.out.println(map);          (map.entry<integer,integer> denominpresent:sorteddenominations.entryset()){             int value;             (map.entry<integer,integer> deominneeded:map.entryset()){                  if(denominpresent.getkey().equals(deominneeded.getkey())){                     value = denominpresent.getvalue()-deominneeded.getvalue();                     if (value>=0) sorteddenominations.put(denominpresent.getkey(),value);                     else throw new notenoughmoneyexception();                 }              }          }          system.out.println(sorteddenominations);         return sorteddenominations;     } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -