A. Introduction:
Just as Point and Triangle, a BankAccount class uses Money class instead of just a double to store its balance. See below, notice the usage of static String variable currencyList and static 2-d array for exchange rate.
public class Money {
private String currency;
private double amount;
/* GBP, USD, CAD, AUD
GBP
USD
CAD
AUD
*/
public static String currencyList = ",GBP,USD,CAD,AUD";
public static double[][] rate = {{1.00, 1.25, 1.65, 1.84},
{0.80, 1.00, 1.32, 1.47},
{0.60, 0.76, 1.00, 1.11},
{0.54, 0.68, 0.90, 1.00}};
Money() {}
Money(String cur, double am) {
currency = cur;
amount = am;
}
Money(double am) {
currency = "USD";
amount = am;
}
Money(String cur) {
currency = cur;
amount = 0.0;
}
public void setAmount(double am) {amount = am;}
public void setCurrency(String cur) {currency = cur;}
public double getAmount() {return amount;}
public String getCurrency () {return currency;}
public static double getExchangeRate(String fromCur, String toCur) {
int row = currencyList.indexOf(","+fromCur)/4;
int col = currencyList.indexOf(","+toCur)/4;
if(row==-1 || col==-1){System.out.println("No such currency"); return -1.0;}
else {return rate[row][col];}
}
public void add(Money m){
this.amount+=m.getAmount()*getExchangeRate(m.getCurrency(), this.getCurrency());
}
public void subtract(Money m){
this.amount-=m.getAmount()*getExchangeRate(m.getCurrency(), this.getCurrency());
}
public static void main(String[] args) {
Money m1 = new Money ("USD", 123.25);
Money m2 = new Money ("AUD", 51.99);
Money m3 = new Money (0.1);
Money m4 = new Money ("CAD");
/* Money m1 = new Money();
Money m2 = new Money();
Money m3 = new Money();
Money m4 = new Money();*/
m1.subtract(m2);
double total = m1.getAmount()*getExchangeRate(m1.getCurrency(), "GBP")
+m2.getAmount()*getExchangeRate(m2.getCurrency(), "GBP")
+m3.getAmount()*getExchangeRate(m3.getCurrency(), "GBP")
+m4.getAmount()*getExchangeRate(m4.getCurrency(), "GBP");
System.out.printf("%.2f GBP",total);
}
}
public class BankAccount {
// instance variables
private String firstName;
private String middleInitial;
private String lastName;
private Money balance;
// static variables --- Belongs to the Class not specific object
private static int totalBankAccounts=0;
private static int numberOfTransactions=0;
// constructors, no return types
BankAccount(){
firstName="Test";
lastName="User";
balance= new Money ("USD");
totalBankAccounts++;
} //default constructor
BankAccount(String firstName, String middleInitial, String lastName){
setFirstName(firstName);
setmiddleInitial(middleInitial);
setLastName(lastName);
//what is balance? null
totalBankAccounts++;
}
BankAccount(String firstName, String middleInitial, String lastName, Money m){
setFirstName(firstName);
setmiddleInitial(middleInitial);
setLastName(lastName);
// setbalance(m);
totalBankAccounts++;
}
// instance methods
public void setFirstName(String firstName){
this.firstName=firstName;
}
public String getFirstName(){return firstName;}
public void setmiddleInitial(String middleInitial){this.middleInitial=middleInitial;}
public String getmiddleInitial(){return middleInitial;}
public void setLastName(String lastName){this.lastName=lastName;}
public String getLastName(){return lastName;}
public void setBalance(Money bal){this.balance=bal;}
public Money getBalance(){return balance;}
public void printAccount(){
System.out.print("FirstName: "+this.firstName);
System.out.print(" MI: "+this.middleInitial);
System.out.println(" LastName: "+this.lastName);
System.out.printf("Balance: %s %.2f\n",this.balance.getCurrency(),this.balance.getAmount());
}
public void deposit(Money m){
printAccount();
getTotalBankAccounts();
balance.add(m);
numberOfTransactions++;
}
public void withdraw(Money m){
balance.subtract(m);
numberOfTransactions++;
}
// static methods
public static int getTotalBankAccounts(){
//printAccount();
return totalBankAccounts;
}
public static int getNumberOfTransactions(){return numberOfTransactions;}
}
C. Try this(HW):
Implement both Money and BankAccount classes. Make necessary changes to Money class so it can handle a new currency RMB. Does BankAccount need to be changed? Why?
Complete the TriangleByVertex class first, then add a Tester class to create 6 Point objects, use these 6 points to create 2 triangles T1 and T2. Print the triangle that has a larger area and the triangle that has a longer perimeter.