//******************************************************************** // Accoun
ID: 3663098 • Letter: #
Question
//********************************************************************
// Account.java Java Foundations
//
// Represents a bank account with basic services such as deposit
// and withdraw.
//********************************************************************
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.035; // interest rate of 3.5%
private String name;
private long acctNumber;
private double balance;
//-----------------------------------------------------------------
// Sets up this account with the specified owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//-----------------------------------------------------------------
// Deposits the specified amount into this account and returns
// the new balance. The balance is not modified if the deposit
// amount is invalid.
//-----------------------------------------------------------------
public double deposit (double amount)
{
if (amount > 0)
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Withdraws the specified amount and fee from this account and
// returns the new balance. The balance is not modified if the
// withdraw amount is invalid or the balance is insufficient.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
if (amount+fee > 0 && amount+fee < balance)
balance = balance - amount - fee;
return balance;
}
//-----------------------------------------------------------------
// Adds interest to this account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of this account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns a one-line description of this account as a string.
//-----------------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + " " + name + " " + fmt.format(balance));
}
}
write a menu driven program that simulates the options for a Savings Account.
1. Instantiate 2 Accounts in your Driver.
2. Hard code the following information to initialize each account:
Customer Name ("First Last")
Account number
Initial Balance
3. Prompt the User to enter her/his account number. Display an error message if the account number does not exist. Display the menu below if the account number is valid.
Your menu will offer the following options:
Deposit
Withdrawal
Returns Balance
Returns Balance with Interest
Returns Account Number
Returns all Account Information
Exit
Explanation / Answer
//Account.java
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.035; // interest rate of 3.5%
private String name;
private long acctNumber;
private double balance;
//-----------------------------------------------------------------
// Sets up this account with the specified owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//Add the getAcccountNumber that returns the acctNumber
public long getAccountNumber()
{
return acctNumber;
}
//-----------------------------------------------------------------
// Deposits the specified amount into this account and returns
// the new balance. The balance is not modified if the deposit
// amount is invalid.
//-----------------------------------------------------------------
public double deposit (double amount)
{
if (amount > 0)
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Withdraws the specified amount and fee from this account and
// returns the new balance. The balance is not modified if the
// withdraw amount is invalid or the balance is insufficient.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
if (amount+fee > 0 && amount+fee < balance)
balance = balance - amount - fee;
return balance;
}
//-----------------------------------------------------------------
// Adds interest to this account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of this account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns a one-line description of this account as a string.
//-----------------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + " " + name + " " + fmt.format(balance));
}
}
--------------------------------------------------------------------------------------------------------------------------------
/*The java tester program that displays a list of menu
choices to select the option. The prompt for
the corresponding fields until user choose to exit
from the program */
//AccountDriver.java
import java.util.ArrayList;
import java.util.Scanner;
public class AccountDriver
{
//Create an arraylist of type Account
private static ArrayList<Account>accounts=new ArrayList<Account>();
//create a scanner object
private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)
{
//Add two account instances to the accounts
accounts.add(new Account("John", 1234, 500));
accounts.add(new Account("Micheal", 1236, 1000));
int choice;
int accountNumber;
//do while continues until user enters 7 to exit the program
do
{
choice=menu();
System.out.println("Enter account number: ");
accountNumber=Integer.parseInt(scanner.nextLine());
if(valid(accountNumber))
{
int pos=getIndex(accountNumber);
switch (choice)
{
case 1:
System.out.println("Enter deposit amount");
double deposit=Double.parseDouble(scanner.nextLine());
accounts.get(pos).deposit(deposit);
break;
case 2:
System.out.println("Enter withdrawl amount");
double wdraw=Double.parseDouble(scanner.nextLine());
System.out.println("Enter fee amount");
double fee=Double.parseDouble(scanner.nextLine());
accounts.get(pos).withdraw(wdraw, fee);
break;
case 3:
System.out.println("Balance: "+accounts.get(pos).getBalance());
break;
case 4:
System.out.println("Balance+intereest "
+(accounts.get(pos).getBalance()+
accounts.get(pos).addInterest()));
break;
case 5:
System.out.println
("Account Number: "+accounts.get(pos).getAccountNumber());
break;
case 6:
System.out.println
("Account Information: "+accounts.get(pos).toString());
break;
case 7:
System.out.println("Exiting from program.");
System.exit(0);
}
}
else
System.out.println("Invalid account number. ");
}while(choice!=7);
}
/*The method getIndex that takes accountNumber
* that returns the position of index of the account
* numbers */
private static int getIndex(int accountNumber) {
boolean found=false;
int position=-1;
for (int i = 0; i < accounts.size() && !found; i++)
{
if(accounts.get(i).getAccountNumber()==accountNumber)
{
found=true;
position=i;
}
}
return position;
}
/*The method valid takes accountNumber as input and
* return true if account number is valid otherwise
* returns false.*/
private static boolean valid(int accountNumber)
{
boolean found=false;
for (int i = 0; i < accounts.size() && !found; i++)
{
if(accounts.get(i).getAccountNumber()==accountNumber)
{
found=true;
}
}
return found;
}
/*menu method*/
public static int menu()
{
System.out.println("1.Deposit");
System.out.println("2.Withdrawal");
System.out.println("3.Balance");
System.out.println("4.Returns Balance with Interest");
System.out.println("5.Returns Account Number");
System.out.println("6.Returns all Account Information");
System.out.println("7.Exit");
System.out.println("Enter your choice.");
int choice=Integer.parseInt(scanner.nextLine());
return choice;
}
}
--------------------------------------------------------------------------------------------------------------------------------
Sample output:
1.Deposit
2.Withdrawal
3.Balance
4.Returns Balance with Interest
5.Returns Account Number
6.Returns all Account Information
7.Exit
Enter your choice.
1
Enter account number:
4526
Invalid account number.
1.Deposit
2.Withdrawal
3.Balance
4.Returns Balance with Interest
5.Returns Account Number
6.Returns all Account Information
7.Exit
Enter your choice.
1
Enter account number:
1234
Enter deposit amount
500
1.Deposit
2.Withdrawal
3.Balance
4.Returns Balance with Interest
5.Returns Account Number
6.Returns all Account Information
7.Exit
Enter your choice.
6
Enter account number:
1234
Account Information: 1234 John $1,000.00
1.Deposit
2.Withdrawal
3.Balance
4.Returns Balance with Interest
5.Returns Account Number
6.Returns all Account Information
7.Exit
Enter your choice.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.