R, matlab, C++ or Java is preferable. Please do do python at all. R is most pref
ID: 3731894 • Letter: R
Question
R, matlab, C++ or Java is preferable. Please do do python at all.
R is most preferable
Explanation / Answer
Below approach solves both test problem, any number of transactions can be added and dollar weighted rate can be calculated.
DollarWeighedYieldCalculator.Java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class DollarWeighedYieldCalculator {
Map<Integer,Transaction> map= new HashMap<Integer,Transaction>();
int i;
public static void main(String [] args) throws NumberFormatException, ParseException
{
Scanner sc=new Scanner(System.in);
DollarWeighedYieldCalculator dWYC = new DollarWeighedYieldCalculator();
System.out.println("Welcome to Dollar Weighed Yield Calculator");
System.out.println("Enter Current Account Balanace of Account Holder in following format AccountName Balance DateInFormat(dd/MM/yyyy) Ex Sioban 2000 01/01/1991");
String input= sc.nextLine();
String[] sa = input.split("");
dWYC.map.put(++(dWYC.i), new Transaction(sa[0],"Deposit",Integer.parseInt(sa[1]),new SimpleDateFormat("dd/MM/yyyy").parse(sa[2])));
boolean askAgain = true;
while (askAgain) {
System.out.print("Enter a number: 1 to add Transaction, 2 to view annual dollar yield ");
String line = sc.nextLine();
switch (line) {
case "1":
dWYC.addTransaction();
break;
case "2":
dWYC.showYield();
break;
default:
askAgain = false;
}
}
}
private void showYield() {
float netDeposit=0;
float netWithdraw=0;
float netDepositCont=0;
float netWithdrawCont=0;
for (Map.Entry<Integer,Transaction> entry : this.map.entrySet())
{
Transaction t=entry.getValue();
if(t.getTransactionType().equalsIgnoreCase("Deposit"))
{
netDeposit+=t.getAmt();
netDepositCont=(t.getAmt()*(t.getTransactionDate().getMonth())/12);
}
else if(t.getTransactionType().equalsIgnoreCase("Withdraw"))
{
netWithdraw+=t.getAmt();
netWithdrawCont=(t.getAmt()*(t.getTransactionDate().getMonth())/12);
}
}
float balance= netDeposit-netWithdraw;
float balanceCont= netDepositCont - netWithdrawCont;
float dollarWeightedSum = (balance/balanceCont)*(100);
System.out.println("Dollar weighted Sum is "+ dollarWeightedSum);
}
private void addTransaction() throws NumberFormatException, ParseException {
System.out.println("Enter Transaction of Account Holder in following format AccountName TransactionType Balance DateInFormat(dd/MM/yyyy) Ex Sioban Deposit/Withdraw 2000 01/01/1991");
Scanner sc1=new Scanner(System.in);
String input= sc1.nextLine();
String[] sa = input.split("");
this.map.put(++i, new Transaction(sa[0],sa[1],Integer.parseInt(sa[2]),new SimpleDateFormat("dd/MM/yyyy").parse(sa[3])));
}
}
class Transaction{
private String accountName;
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
//Deposit or Withdraw
private String transactionType;
private float amt;
private Date transactionDate;
public String getTransactionType() {
return transactionType;
}
public void setTransactionType(String transactionType) {
this.transactionType = transactionType;
}
public float getAmt() {
return amt;
}
public void setAmt(float amt) {
this.amt = amt;
}
public Date getTransactionDate() {
return transactionDate;
}
public void setTransactionDate(Date transactionDate) {
this.transactionDate = transactionDate;
}
public Transaction(String accountName, String transactionType, float amt,
Date transactionDate) {
super();
this.accountName = accountName;
this.transactionType = transactionType;
this.amt = amt;
this.transactionDate = transactionDate;
}
public Transaction()
{
}
@Override
public String toString() {
return "Transaction [accountName=" + accountName + ", transactionType="
+ transactionType + ", amt=" + amt + ", transactionDate="
+ transactionDate + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.