Need help creating a Java program with mandatory requirements! Very important th
ID: 3878389 • Letter: N
Question
Need help creating a Java program with mandatory requirements!
Very important though! The George account class instance must be in the main class, and the requirement of printing a list of transactions for only the ids used when the program runs(Detailed in the Additional simulation requirements) is extremely important! Thank you so very much!
Instructions: This homework models after a banking situation with ATM machine. You are required to create three classes, Account, Transaction, and the main class (with the main method). Please closely follow the requirements below Requirements for the Transaction class: A private Date data field that stores the date of the transaction A private char data field for the type of the transaction, such as "W" for withdrawal and "D" for deposit A private double data field for the amount of the transaction A private double data field for the balance of the account after the transaction A private String data field for the description of the transaction A constructor that creates an instance of the Transaction class with specified values of transaction type, amount balance, and description, and date of the transaction. Note: Instead of passing a Date object to the constructor, you should use the new operator inside the body of the constructor to pass a new Date instance to the date data field Define the corresponding accessor (get) methods to access a transaction's date, type, amount, description, and balance, i.e., you need to define 5 get methods. Define a toString() method which does not receive parameters but returns a String that summarizes the transaction details, including transaction type, amount, balance after transaction, description of the transaction and transaction date. You can organize and format the returned String in your own way, but you must include all of the required information. . . Note: the purpose of the Transaction class is to document a banking transaction, i.e., when a transaction happens, you can use the transaction parameters to create an instance of the Transaction class. Requirements for the Account class: A private int data field for the id of the account. A private String data field for the name of the customer A private double data field for the balance of the account. A private double data field for the annual interest rate. Key assumptions: (1) all accounts created following this class construct share the same interest rate. (2) while the annual interest rate is a percentage, e. g., 4.5%, users will just enter the double number as 4.5 (instead of 0.045) for simplicity. Therefore, you, the developer, need to divide the annual interest rate by 100 whenever you use it in a calculation A private Date data field that stores the account creating date A private ArrayList data field that stores a list of transactions for the account. Each element of this ArrayList must be an instance of the Transaction class defined above A no-arg constructor that creates a default account with default variable values A constructor that creates an instance of the Account class with a specified id and initial balance A constructor that creates an instance of the Account class with a specified id, name, and initial balance Note: for all constructors, instead of passing a Date object to the constructor, you should use the new operator inside the body of the constructor to pass a new Date instance to the date data field Define corresponding accessor (get) and mutator(set) methods to access and reset the account id, balance, and interest rate, i.e., you need to define 3 get methods and 3 set methods. .Explanation / Answer
Hello, I have answered a similar question before, so I’m referring my own solution here. Implemented everything as per the requirements. Defined following things in this answer.
//Account.java class
import java.util.ArrayList;
import java.util.Date;
public class Account {
private int id;
private String name;
private double balance;
private static double annualInterestRate;
private Date createdDate;
private ArrayList<Transaction> transactions;
/**
* Constructor with no arguments
*/
public Account() {
id=0;
name="";
balance=0;
createdDate=new Date();
transactions=new ArrayList<Transaction>();
}
/**
* Constructor with account id and starting balance as arguments
*/
public Account(int id,double balance) {
this.id=id;
this.balance=balance;
name="";
createdDate=new Date();
transactions=new ArrayList<Transaction>();
}
/**
* Constructor with id, name and balance as arguments
*/
public Account(int id,String name,double balance) {
this.id=id;
this.balance=balance;
this.name=name;
createdDate=new Date();
transactions=new ArrayList<Transaction>();
}
/**
* Required accessors and mutators
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
/**
* method to get annual interest rate, common for all accounts,
* so made it static.
*/
public static double getAnnualInterestRate() {
return annualInterestRate;
}
/**
* method to set annual interest rate, common for all accounts,
* so made it static.
*/
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
public String getName() {
return name;
}
public Date getCreatedDate() {
return createdDate;
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
/**
* method to calculate and return the monthly interest
*/
public double getMonthlyInterest(){
double monthlyInterestRate=(annualInterestRate/100)/12;
double monthlyInterest=balance*monthlyInterestRate;
return monthlyInterest;
}
/**
* method to withdraw money and log the transaction
*/
public void withdraw(double amount,String description){
balance-=amount;
Transaction transaction=new Transaction('W', amount, balance, description);
transactions.add(transaction);
}
/**
* method to deposit money and log the transaction
*/
public void deposit(double amount, String description){
balance+=amount;
Transaction transaction=new Transaction('D', amount, balance, description);
transactions.add(transaction);
}
}
//Transaction.java class
import java.util.Date;
public class Transaction {
private Date date;
private char type;
private double amount;
private double balance;
private String description;
/**
* Constructor to initialize all attributes
*/
public Transaction(char type, double amount, double balance,
String description) {
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
date=new Date();
}
/**
* required getters
*/
public Date getDate() {
return date;
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalance() {
return balance;
}
public String getDescription() {
return description;
}
/**
* method to return a string representation of transaction
*/
public String toString() {
return "Transaction type: "+type
+" Amount: "+amount
+" Balance after transaction: "+balance
+" Description: "+description
+" Date: "+date.toString();
}
}
//Main.java file
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
/**
* Scanner object to receive user input
*/
static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
/**
* Setting the annual interest rate
*/
Account.setAnnualInterestRate(1.5d);
/**
* Answer for the first part (Test) uncomment the below lines if you
* want to check
*/
/**
* Account acc=new Account(1122, "George", 1000); acc.deposit(3000,"Salary");
* acc.withdraw(2500,"Rent"); System.out.println("Name: "+acc.getName());
* System
* .out.println("Annual Interest Rate: "+Account.getAnnualInterestRate
* ()); System.out.println("Balance: "+acc.getBalance());
* System.out.println("Monthly Interest: "+acc.getMonthlyInterest());
* System.out.println("Created Date: "+acc.getCreatedDate().toString());
* System.out.println("Transactions:"); for(Transaction
* t:acc.getTransactions()){ System.out.println(" "+t); }
*/
/**
* Creating an array of 10 Account objects
*/
Account[] accounts = new Account[10];
/**
* Initializing each accounts with IDs from 1 to 10, and with a starting
* balance of $100
*/
for (int i = 0; i < 10; i++) {
accounts[i] = new Account(i + 1, 100);
}
/**
* Simulating ATM machine experience, and looping the menu continuously
* until user decides to quit
*/
try {
/**
* flag variable used to denote whether user decided to quit
* or not
*/
boolean quit = false;
/**
* Loops until the user wish to stop
*/
while (!quit) {
/**
* getting ID from user (login)
*/
int id = userLogin();
if (id != 0) {
/**
* Initializing the banking menu,.
* this variable is used to denote whether user decided to exit
* the banking menu or not.
*/
boolean mainMenuQuit = false;
/**
* Loops as long as user input is not 4
*/
while (!mainMenuQuit) {
/**
* getting user choice
*/
int choice = mainMenu();
if (choice != 4) {
switch (choice) {
case 1:
double balance = accounts[id - 1].getBalance();
System.out.println("The balance is " + balance);
break;
case 2:
System.out
.println("Enter amount to withdraw: ");
double amount = Double.parseDouble(scanner
.nextLine());
System.out.println("Enter description:");
String description=scanner.nextLine();
accounts[id - 1].withdraw(amount,description);
break;
case 3:
System.out.println("Enter amount to deposit: ");
amount = Double.parseDouble(scanner.nextLine());
System.out.println("Enter description:");
description=scanner.nextLine();
accounts[id - 1].deposit(amount,description);
break;
}
} else {
/**
* user entered 4, end of the loop
*/
mainMenuQuit = true;
}
}
} else {
/**
* user decided to quit the program, end of the loop
*/
quit = true;
System.out.println("All transactions:");
for(int i=0;i<accounts.length;i++){
ArrayList<Transaction> transactions=accounts[i].getTransactions();
if(transactions.size()>0){
System.out.println("Transactions for Account ID: "+accounts[i].getId());
for(Transaction t:transactions){
System.out.println(t);
}
}
}
System.out.println(" Thank you");
}
}
} catch (Exception e) {
/**
* In case abnormal values are given
*/
System.out.println("Invalid input given, program is quitting");
System.exit(1);
}
}
/**
* method to get ID from user (simulating user login)
*/
static int userLogin() {
System.out.println("Enter an ID, (0 to exit): ");
int id = Integer.parseInt(scanner.nextLine());
if (id > 10) {
System.out.println("Invalid ID, try again");
return userLogin();
}
return id;
}
/**
* method to display the banking menu & get choice from user
*/
static int mainMenu() {
System.out.println("Main Menu" + " 1. Check balance" + " 2. Withdraw"
+ " 3. Deposit" + " 4. Exit" + " Enter a choice: ");
int choice = Integer.parseInt(scanner.nextLine());
if (choice < 1 || choice > 4) {
System.out.println("Invalid choice, try again");
return mainMenu();
} else {
return choice;
}
}
}
/*OUTPUT*/
Enter an ID, (0 to exit):
2
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
1
The balance is 100.0
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
3
Enter amount to deposit:
3000
Enter description:
salary
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
1
The balance is 3100.0
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
2
Enter amount to withdraw:
100
Enter description:
food
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
4
Enter an ID, (0 to exit):
8
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
1
The balance is 100.0
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
2
Enter amount to withdraw:
20
Enter description:
recharge
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
1
The balance is 80.0
Main Menu
1. Check balance
2. Withdraw
3. Deposit
4. Exit
Enter a choice:
4
Enter an ID, (0 to exit):
0
All transactions:
Transactions for Account ID: 2
Transaction type: D
Amount: 3000.0
Balance after transaction: 3100.0
Description: salary
Date: Fri Jan 19 09:56:31 IST 2018
Transaction type: W
Amount: 100.0
Balance after transaction: 3000.0
Description: food
Date: Fri Jan 19 09:56:41 IST 2018
Transactions for Account ID: 8
Transaction type: W
Amount: 20.0
Balance after transaction: 80.0
Description: recharge
Date: Fri Jan 19 09:57:30 IST 2018
Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.