I received an assignment modifiying a previouse java program the program I initi
ID: 3803800 • Letter: I
Question
I received an assignment modifiying a previouse java program the program I initially created is as follows:
// Account.java //
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary of the account.
// *******************************************************
public class Account {
private double balance;
private String name;
private long acctNum;
// --------------------------------------------- //Constructor --
// initializes balance, owner, and account number
// --------------------------------------------
public Account(double initBal, String owner, long number) {
balance = initBal;
name = owner;
acctNum = number;
}
// -------------------------------------------- // Checks to see if balance
// is sufficient for withdrawal.
// If so,decrements balance by amount; if not, prints message. //
// --------------------------------------------
public void withdraw(double amount) {
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
// -------------------------------------------- // Adds deposit amount to
// balance.Chapter 4: Writing Classes 53 //
// --------------------------------------------
public void deposit(double amount) {
balance += amount;
} // --------------------------------------------
// Returns balance. // --------------------------------------------
public double getBalance() {
return balance;
}
I need to make these modifications and dont really know how, Thanks for the help.
Menu for Bank Account:
Menu for Bank Account requires the use of Account.java from previous assignments.
Follow the instructions below:
Complete accountMenu.java.
You must complete the code in the order specified
Use a switch statement to call menu choices
The switch statement should include a default for an invalid choice
The menu and the switch should be in a do-while loop
The loop should continue to execute until user enters “6”
Make the following modifications to Account:
Add the method public Boolean validateAmount(double amount) to insure the amount deposited is a positive number
Call validateAmount from each method that uses an amount
The withdraw method must also call the chargeFee method to determine if there are sufficient funds to cover both the withdrawal amount and the fee amount. Do not use a fee for any other method.
// ****************************************************************
// accountMenu.java
//
// Use Account class to create and manage bank accounts
// ****************************************************************
import java.util.Scanner;
public class accountMenu
{
public static void main(String[] args)
{
// declare variables
//prompt user for beginning information for the account: name, //number, and beginning balance
// create an instance of the account using the beginning information
// create the following menu
// ------------------------------------------------
// Welcome to CSCI Bank
//
// Choose one of the following options
// by entering the number in front of the option:
//
// 1) Check account balance
// 2) Deposit to account
// 3) Withdraw from account
// 4) Print account information
// 5) Change account name
// 6) Finished
//
// ------------------------------------------------
// final output thanking user by name for using CSCI Bank
}
}
Explanation / Answer
package bank;
public class Account {
private double balance;
private String name;
private long acctNum;
private int fee=50;
//fee for withdrawal is set for 50/- you can change it as per requirement
// --------------------------------------------- //Constructor --
// initializes balance, owner, and account number
// --------------------------------------------
public Account(double initBal, String owner, long number) {
balance = initBal;
name = owner;
acctNum = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAcctNum() {
return acctNum;
}
public void setAcctNum(long acctNum) {
this.acctNum = acctNum;
}
public void setBalance(double balance) {
this.balance = balance;
}
// -------------------------------------------- // Checks to see if balance
// is sufficient for withdrawal.
// If so,decrements balance by amount; if not, prints message. //
// --------------------------------------------
public void withdraw(double amount) {
if (balance >= amount && validateAmount(amount) &&
chargeFee(amount))
balance -= amount;
else
System.out.println("Insufficient funds");
}
// -------------------------------------------- // Adds deposit amount to
// balance.Chapter 4: Writing Classes 53 //
// --------------------------------------------
public void deposit(double amount) {
if(validateAmount(amount)){
balance += amount;
return;
}
System.out.println("Enter positive number for amount");
} // --------------------------------------------
// Returns balance. // --------------------------------------------
public double getBalance() {
return balance;
}
public boolean validateAmount(double amount){
if(amount>0)
return true;
return false;
}
private boolean chargeFee(double amount){
if(balance>=(amount+fee))
return true;
return false;
}
}
AccountMenu.java
package bank;
import java.util.Scanner;
public class AccountMenu {
public static void main(String[] args)
{
int choice=0;
Scanner sc=new Scanner(System.in);
System.out.println("------------------------------------------------");
System.out.println(" Welcome to CSCI Bank");
System.out.println("Choose one of the following options by entering the number in front of the option:");
Account acc=new Account(39874,"Tim John",254785466);
do{
System.out.println("1) Check account balance");
System.out.println("2) Deposit to account");
System.out.println("3) Withdraw from account");
System.out.println("4) Print account information");
System.out.println("5) Change account name");
System.out.println("6) Finished");
choice=sc.nextInt();
switch(choice){
case 1:
System.out.println("Account Balance is: "+acc.getBalance());
break;
case 2:
System.out.println("Enter amount to deposite");
double amount=sc.nextDouble();
acc.deposit(amount);
System.out.println("Deposited amount :"+amount);
break;
case 3:
System.out.println("Enter amount to withdraw");
amount=sc.nextDouble();
acc.withdraw(amount);
System.out.println("Withdrawn amount :"+amount);
break;
case 4:
System.out.println("Account Holder's Name: "+acc.getName());
System.out.println("Account Number: "+acc.getAcctNum());
System.out.println("Account Balance: "+acc.getBalance());
break;
case 5:
System.out.println("Enter New name for account: ");
String name=sc.nextLine();
acc.setName(name);
break;
default:
System.out.println("Invalid input:");
}
}while(choice!=6);
System.out.println("Thank You "+acc.getName()+" for using CSCI Bank");
}
}