Bank Class Each bank will be represented by an instance of a Bank class, which s
ID: 3775199 • Letter: B
Question
Bank Class
Each bank will be represented by an instance of a Bank class, which should contain the following:
• Five private data fields
o The name of the bank
o The commission rate
o Three fields for the three currencies exchanged by the bank. Each of these should have type Currency.
• A constructor that takes a file name (a String) as an argument. The constructor should...
o open the file,
o read in the information about the bank, initializing all of the bank's data fields.
• A method supportCurrency that takes a currency code (a String) as its only argument. It should return true if the bank can exchange the given currency and false if it cannot.
• [4 pts] A method getRate that takes a currency code (a String) as its only argument. It should return the rate of exchange for that currency, or -1.0 if the bank does not exchange that currency.
• Two additional methods: quoteBuy and quoteSell. Each of these takes two arguments, a double for the amount of foreign currency, and a String for the currency code. Each method will return a Quote object (defined below) if the bank supports that currency and null if the bank does not. Call the getRate method you wrote within your quote methods. Use the following formulas to calculate the number of US dollars and the bank's commission:
o For selling amt units of currency with exchange rate rate, the number of US dollars before commission is amt / rate; call that base. Then the commission is the commission rate times base and the amount of US dollars the customer will receive is base minus the commission.
o For buying amt units of currency with exchange rate rate and commission rate com (where com is between 0.0 and 1.0), the number of US dollars the customer will pay is amt / (rate * (1 - com)); call that dollarsOwed. The commission is com * dollarsOwed.
Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.util.Currency; import java.util.HashMap; import java.util.Scanner; /** * Created by RahulPatidar on 21/11/16. */ public class Bank { private String name; private double com_rate; private Currency currency_1; private Currency currency_2; private Currency currency_3; //************ As nothing is given about exchange rate Calculations, So I stored them in HashMap ***********///// public HashMap exchangeRates = new HashMap(); public double getExchangeRate(String currency) { return exchangeRates.get(currency); } public void setExchangeRates(){ exchangeRates.put("USD",1.0); exchangeRates.put("INR",68.10); exchangeRates.put("AUD",1.35); exchangeRates.put("GBP",0.81); exchangeRates.put("JPY",110.68); exchangeRates.put("EUR",0.93); } public Bank(String fileName){ setExchangeRates(); try{ Scanner sc; File file = new File(fileName); sc = new Scanner(file); this.name = sc.nextLine(); this.com_rate = sc.nextDouble(); this.currency_1 = Currency.getInstance(sc.next()); this.currency_2 = Currency.getInstance(sc.next()); this.currency_3 = Currency.getInstance(sc.next()); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getCom_rate() { return com_rate; } public void setCom_rate(double com_rate) { this.com_rate = com_rate; } public String toString(){ String bank = ""; bank+=this.name+", "; bank+=this.com_rate+", "; bank+=this.currency_1+", "; bank+=this.currency_2+", "; bank+=this.currency_3; return bank; } public boolean supportCurrency(String currency){ if(Currency.getInstance(currency).equals(currency_1)){ return true; }else if(Currency.getInstance(currency).equals(currency_2)){ return true; } else if(Currency.getInstance(currency).equals(currency_3)){ return true; }else{ return false; } } public double getRate(String currency){ if(supportCurrency(currency)){ return getExchangeRate(currency); } return -1.0; } public Quote quoteBuy(double amount, String currency){ double rate = getRate(currency); if(rate==-1.0)return null; double dollarOwed = amount/(rate*(1-this.com_rate)); double commission = com_rate*dollarOwed; return new Quote(dollarOwed,commission,Currency.getInstance(currency)); } public Quote quoteSell(double amount, String currency){ double rate = getRate(currency); if(rate==-1.0)return null; double base = amount/rate; double com = com_rate*base; return new Quote(base-com,com,Currency.getInstance(currency)); } } import java.util.Currency; /** * Created by RahulPatidar on 21/11/16. */ public class Quote { private double amount; private Currency currency; private double commission; public Quote(double amount,double commission,Currency currency){ this.amount = amount; this.currency = currency; this.commission = commission; } public String toString(){ return this.currency+", "+this.amount+", "+this.commission; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.