Question

Need help creating a Java program with mandatory requirements!

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 transactions 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. .

VERY IMPORTANT: 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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Copyable Code:

//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

+"\nAmount: "+amount

+"\nBalance after transaction: "+balance

+"\nDescription: "+description

+"\nDate: "+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("\n"+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("\nThank 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" + "\n1. Check balance" + "\n2. Withdraw"

+ "\n3. Deposit" + "\n4. Exit" + "\nEnter 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



Feel free to reach out if you have any doubts.
Rate if the answer was helpful.
Thanks

Add a comment
Know the answer?
Add Answer to:
Need help creating a Java program with mandatory requirements! VERY IMPORTANT: The George account class instance...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • in java Account that contains: balance: double data field date: Date data field. Use Date class...

    in java Account that contains: balance: double data field date: Date data field. Use Date class from the java.util package accountNumber: long data field. You should generate this value randomly. The account number should be 9 digits long. You can you random method from java Math class. annuallnterestRate: double data field. customer: Customer data field. This is the other class that you will have to design. See description below. The accessor and mutator methods for balance, annuallnterestRate, date, and customer....

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

  • Design a class named BankAccount containing the following data field and methods. • One double data...

    Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance.  throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits...

  • Be sure to submit homework via Canvas, not email. Ask questions via email or Canvas inbox....

    Be sure to submit homework via Canvas, not email. Ask questions via email or Canvas inbox. Late assignments will not be accepted. Important: please zip the complete NetBeans project file (not just the jar or .java file) before submitting to Canvas. If you just submit the jar or .java file, it will not be graded and if you later submit the NetBeans project it may be treated as a late submission. Please name the project after your name (e.g., HWSFirstNameLastName)....

  • PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field...

    PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field named id for the savings account. A private float data field named balance for the savings account. A private float data field named annuallnterestRate that stores the current interest rate. A_init_ that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). . The accessor and mutator methods for id, balance, and annuallnterestRate. A method...

  • Design a class named Account that contains: A private int datafield named id(default 0) A private...

    Design a class named Account that contains: A private int datafield named id(default 0) A private double datafield named balance(default 0) A no-arg constructor that creates default account A constructor that creates an account with specified id & balance The getter and setter methods for id and balance. Create an object of account class using default constructor and update the balance to 2000$ .

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT