-
-
Save amirul12/1dbd46f49893f3879feb6d9a4152138d to your computer and use it in GitHub Desktop.
import java.util.Map; | |
import java.util.TreeMap; | |
public class InterestCalculation { | |
public static void main(String[] args) { | |
// Creating a TreeMap to store account balances sorted by balance | |
TreeMap<Double, String> accountBalances = new TreeMap<>(); | |
// Adding account balances and account holder names | |
accountBalances.put(5000.00, "John Doe"); | |
accountBalances.put(15000.00, "Jane Smith"); | |
accountBalances.put(3000.00, "Alice Johnson"); | |
accountBalances.put(25000.00, "Bob Brown"); | |
accountBalances.put(12000.00, "Charlie Davis"); | |
// Print header for the interest report | |
System.out.println("Account Balance Interest Report"); | |
System.out.println("--------------------------------"); | |
// Iterate through the TreeMap and calculate interest on each balance | |
for (Map.Entry<Double, String> entry : accountBalances.entrySet()) { | |
Double balance = entry.getKey(); | |
String accountHolder = entry.getValue(); | |
// Calculate interest based on balance | |
double interestRate = getInterestRate(balance); | |
double interest = balance * interestRate; | |
// Print the account holder, balance, interest rate, and interest | |
System.out.printf("Account Holder: %-15s | Balance: $%-10.2f | Interest: $%-10.2f | Rate: %.2f%%\n", | |
accountHolder, balance, interest, interestRate * 100); | |
} | |
} | |
// Method to determine interest rate based on balance | |
public static double getInterestRate(double balance) { | |
if (balance < 5000) { | |
return 0.01; // 1% interest for balances below $5000 | |
} else if (balance < 10000) { | |
return 0.02; // 2% interest for balances between $5000 and $9999 | |
} else if (balance < 20000) { | |
return 0.03; // 3% interest for balances between $10000 and $19999 | |
} else { | |
return 0.04; // 4% interest for balances over $20000 | |
} | |
} | |
} |
Account Balance Interest Report
Account Holder: Alice Johnson | Balance: $3000.00 | Interest: $30.00 | Rate: 1.00%
Account Holder: John Doe | Balance: $5000.00 | Interest: $100.00 | Rate: 2.00%
Account Holder: Charlie Davis | Balance: $12000.00 | Interest: $360.00 | Rate: 3.00%
Account Holder: Jane Smith | Balance: $15000.00 | Interest: $450.00 | Rate: 3.00%
Account Holder: Bob Brown | Balance: $25000.00 | Interest: $1000.00 | Rate: 4.00%
Key Points:
Sorted by Balance: Since TreeMap stores keys in sorted order, the account balances are displayed in ascending order.
Interest Calculation: The method getInterestRate() determines the interest rate based on the balance, making it easy to adjust interest rules as needed.
Formatting Output: The System.out.printf() function is used to display a nicely formatted report.
Explanation of the Code:
TreeMap Declaration:
TreeMap<Double, String> accountBalances = new TreeMap<>();
The key is the account balance (of type Double), and the value is the account holder's name (of type String).
The TreeMap automatically sorts the balances in ascending order by the key (balance).
Inserting Data:
The put() method is used to insert account balance and holder information into the TreeMap.
Interest Calculation:
We define a method getInterestRate() to determine the interest rate based on the account balance.
The interestRate is calculated by multiplying the balance by the rate (e.g., 0.01 for 1%, 0.02 for 2%, etc.).
Iterating Through TreeMap:
We iterate over the TreeMap using an enhanced for loop (for (Map.Entry<Double, String> entry : accountBalances.entrySet())).
For each entry (key-value pair), we retrieve the balance and account holder, calculate the interest, and print the report in a formatted way.
Output: The program generates a sorted report of account balances and the calculated interest for each account.