How do I randomly create ten Account/CDAccount objects and store them in one Has
ID: 3685779 • Letter: H
Question
How do I randomly create ten Account/CDAccount objects and store them in one Hashmap and Treemap?
I'm supposed to add on to this code from a past java class but I don't know where to start or how to do it. The requirements are:
- Provide 10 account numbers and search for the specific objects from Hashmap and Treemap
- For Account objects, print out the account number and balance; for CDAccount objects, print out the account number and mature balance; for objects that can't be found print out "The Account Number doesn't exist".
- Print out all Accounts created in Hashmap and Treemap.
Any help will be greatly appreciated, thank you.
//Account class
import java.util.Date;
//public class Account implements Comparable{
public class Account{
private long id;
private double balance;
private Date dateCreated;
private static double annualInterestRate = 0.05;
final static int number = 5;
Account()
{
dateCreated = new Date();
}
Account(int ID, double init_balance)
{
id = ID;
balance = init_balance;
dateCreated = new Date();
}
/*public Account(String first, String last, String state, long People, double startBalance) throws Exception {
super();
balance = startBalance;
id = People;
}*/
public Date getdateCreatedObject()
{
return dateCreated;
}
public void setdateCreated(Date dateCreated)
{
this.dateCreated=dateCreated;
}
public String getdateCreated()
{
return dateCreated.toString();
}
public long getid()
{
return id;
}
public void setid(int ID)
{
id = ID;
}
public double getbalance()
{
return balance;
}
public void setbalance(double new_balance)
{
balance = new_balance;
}
public static double annualInterestRate()
{
return annualInterestRate;
}
public static void setannualInterestRate(double new_interestRate)
{
annualInterestRate = new_interestRate;
}
public static double getannualInterestRate()
{
return annualInterestRate;
}
public double getMonthlyInterestRate()
{
return annualInterestRate/12;
}
public double getMonthlyInterest()
{
return balance * getMonthlyInterestRate();
}
public void withdraw(double withdraw_amount)
{
setbalance(getbalance() - withdraw_amount);
}
public void deposit(double deposit_amount)
{
setbalance(getbalance() + deposit_amount);
}
/*@Override
public int compareTo(Object o)
{
Account a = (Account)o;
if (id > a.id)
return 1;
else if ( id < a.id)
return -1;
else
return 0;
}*/
@Override
public boolean equals(Object o)
{
if ( o instanceof Account)
{
Account a = (Account) o;
return (this.id == a.id);
}
return false;
}
@Override
public String toString()
{
return ("Account id: " + this.id + "Account balance: " + this.balance);
}
}
// CDAccount
public class CDAccount extends Account {
private double CDannualInterestRate=0.0;
private int duration;
CDAccount()
{
setDuration(0);
setCDAnnualInterestRate();
}
CDAccount(int duration, double CDAnnualInterestRate)
{
setDuration(duration);
setCDAnnualInterestRate();
}
CDAccount(int id, double balance, int duration)
{
super(id, balance);
setDuration(duration);
setCDAnnualInterestRate();
}
public int getDuration()
{
return duration;
}
public void setDuration(int duration)
{
this.duration = duration;
}
public double getCDAnnualInterestRate()
{
return CDannualInterestRate;
}
public void setCDAnnualInterestRate()
{
this.CDannualInterestRate = Account.getannualInterestRate() + getDuration()/3 * 0.005;
}
@Override
public double getMonthlyInterestRate()
{
return CDannualInterestRate/12;
}
@Override
public double getMonthlyInterest()
{
return getbalance() * getMonthlyInterestRate();
}
public String toString()
{
return String.format("%15s%20s%15s%30s ", "Account Number", "Initial Balance", "Rate(%)", "Date Created")
+ "================================================================================================ "
+ String.format("%15d%20.2f%15.2f%35s ", this.getid(), this.getbalance(), this.getCDAnnualInterestRate()*100, this.getdateCreated());
}
}
// main class
public class main extends Account
{
public main(String[] args)
{
ArrayList<CDAccount> CDaccount = new ArrayList<CDAccount>();
for (int i = 0; i < Account.number; i++)
{
CDaccount.add(new CDAccount(1000*(i+1), 1000.0*(i+1), 3*(i+1)));
}
//print Account number 1000
java.util.Collections.shuffle(CDaccount);
for (int i = 0; i < Account.number; i++)
{
System.out.println(CDaccount.get(i).toString());
for (int j=1; j <= CDaccount.get(i).getDuration(); j++)
{
System.out.printf("%7s%-6d%10.2f ", "Month " , j, CDaccount.get(i).getbalance()+CDaccount.get(i).getMonthlyInterest());
CDaccount.get(i).setbalance(CDaccount.get(i).getbalance() + CDaccount.get(i).getMonthlyInterest());
}
}
//Look for Account number 1000
System.out.println("Account number 1000:" + CDaccount.indexOf(new CDAccount(1000, 0.0, 0)));
System.out.println(CDaccount.indexOf(CDaccount.get(4)));
}
}
Explanation / Answer
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class main {
public static void main(String[] args) {
Map<Integer, Account> hashMap = new HashMap<Integer, Account>();
Map<Integer, Account> treeMap = new TreeMap<Integer, Account>();
for (int i = 0; i < Account.number; i++) {
hashMap.put(1000 * (i + 1), new CDAccount(1000 * (i + 1),
1000.0 * (i + 1), 3 * (i + 1)));
treeMap.put(3000 * (i + 1), new CDAccount(3000 * (i + 1),
1000.0 * (i + 1), 3 * (i + 1)));
hashMap.put(2000 * (i + 1), new Account(2000 * (i + 1),
1000.0 * (i + 1)));
treeMap.put(4000 * (i + 1), new Account(4000 * (i + 1),
1000.0 * (i + 1)));
}
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Account Number:");
int searchAcc = scanner.nextInt();
// print Account number 1000
if (hashMap.get(searchAcc) != null) {
Object obj = treeMap.get(searchAcc);
if (obj instanceof CDAccount) {
CDAccount cdAcc = (CDAccount) obj;
System.out.println(" Account Number :" + cdAcc.getid()
+ " Balance :" + cdAcc.getbalance());
} else {
Account acc = (Account) obj;
System.out.println(" Account Number :" + acc.getid()
+ " Balance :" + acc.getbalance());
}
} else if (treeMap.get(searchAcc) != null) {
Object obj = treeMap.get(searchAcc);
if (obj instanceof CDAccount) {
CDAccount cdAcc = (CDAccount) obj;
System.out.println(" Account Number :" + cdAcc.getid()
+ " Balance :" + cdAcc.getbalance());
} else {
Account acc = (Account) obj;
System.out.println(" Account Number :" + acc.getid()
+ " Balance :" + acc.getbalance());
}
} else {
System.out.println("The Account Number doesn't exist");
}
for (Integer key : hashMap.keySet()) {
Account acc = hashMap.get(key);
System.out.println(acc);
}
for (Integer key : treeMap.keySet()) {
Account acc = treeMap.get(key);
System.out.println(acc);
}
}
}
OUTPUT:
Enter the Account Number:3000
Account Number :3000 Balance :1000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
4000 4000.00 7.00 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
8000 8000.00 9.00 Tue Apr 05 13:12:18 IST 2016
Account id: 12000Account balance: 6000.0
Account id: 16000Account balance: 8000.0
Account id: 20000Account balance: 10000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
1000 1000.00 5.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
5000 5000.00 7.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
9000 9000.00 9.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
2000 2000.00 6.00 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
6000 6000.00 8.00 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
10000 10000.00 10.00 Tue Apr 05 13:12:18 IST 2016
Account id: 14000Account balance: 7000.0
Account id: 18000Account balance: 9000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
3000 3000.00 6.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
7000 7000.00 8.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
3000 1000.00 5.50 Tue Apr 05 13:12:18 IST 2016
Account id: 4000Account balance: 1000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
6000 2000.00 6.00 Tue Apr 05 13:12:18 IST 2016
Account id: 8000Account balance: 2000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
9000 3000.00 6.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
12000 4000.00 7.00 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
15000 5000.00 7.50 Tue Apr 05 13:12:18 IST 2016
Account id: 16000Account balance: 4000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
18000 6000.00 8.00 Tue Apr 05 13:12:18 IST 2016
Account id: 20000Account balance: 5000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
21000 7000.00 8.50 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
24000 8000.00 9.00 Tue Apr 05 13:12:18 IST 2016
Account Number Initial Balance Rate(%) Date Created
================================================================================================
27000 9000.00 9.50 Tue Apr 05 13:12:18 IST 2016
Account id: 28000Account balance: 7000.0
Account Number Initial Balance Rate(%) Date Created
================================================================================================
30000 10000.00 10.00 Tue Apr 05 13:12:18 IST 2016
Account id: 32000Account balance: 8000.0
Account id: 36000Account balance: 9000.0
Account id: 40000Account balance: 10000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.