You are given the class Account with predefined methods for bank account. It alr
ID: 3758422 • Letter: Y
Question
You are given the class Account with predefined methods for bank account. It already has methods to display the balance and methods to withdraw and deposit some amount.
You need to create a subclass called SavingsAccount. Your SavingsAccount class should override the deposit method, so that when a user deposits an amount in the savings account, the bank will add additional 10% to the amount.
You are given a testAccount class which asks the user what account it want to create. In the testAccount class, add java code to do the following things in a sequential order:
create a new savings account object (for choice 2).
ask the user to enter an initial balance which should be greater than 50. If the entered amount is less than 50, the program will ask the user to enter another amount. This continues until an amount greater than 50 is entered.
Set the balance of the savings account object by calling the setBalance() method.
Ask the user to deposit an amount. Deposit that amount and display the balance of the account.
Turn in the SavingsAccount.java and testAccount.java.
---------account java-------
/**
* Super class Account
*
*/
public class Account {
/**
* balance of the account
*/
protected double balance;
/**
* constructor
*/
public Account() {
balance = 0;
}
/**
* set the balance
* @param amt balance
*/
public void setBalance(double amt) {
balance += amt;
}
/**
* deposit to account
* @param amt
*/
public void deposit(double amt) {
balance += amt;
}
/**
* withdraw from account
* @param amt
*/
public void withDraw(double amt) {
balance -= amt;
}
/**
* display the account balance
*/
public void displayBalance() {
System.out.println("Balance - " + balance);
}
}
*****testAccount java****
import java.util.Scanner;
public class testAccount {
/**
* A simple test function to test Account and SavingsAccount class
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome. Which account do you want to create? 1. Normal 2. Savings");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
Account acct = null;
if (choice == 1) {
acct = new Account();
System.out.println("New normal account created.");
acct.displayBalance();
} else if (choice == 2) {
//Here write your code as required in the Question
}
//Deposit money to account here
acct.displayBalance();
}
}
Below is an illustration of how the program works:
Welcome. Which account do you want to create?
1. Normal 2. Savings
Enter choice: 2
Enter initial balance: (>$50) - 20
Balance should be greater than 50
Enter initial balance: (>$50) - 70
Balance - 70.0
Enter amount to deposit: 100
Balance - 180.0
Explanation / Answer
public class Account {
/**
* balance of the account
*/
protected double balance;
/**
* constructor
*/
public Account() {
balance = 0;
}
/**
* set the balance
* @param amt balance
*/
public void setBalance(double amt) {
balance += amt;
}
/**
* deposit to account
* @param amt
*/
public void deposit(double amt) {
balance += amt;
}
/**
* withdraw from account
* @param amt
*/
public void withDraw(double amt) {
balance -= amt;
}
/**
* display the account balance
*/
public void displayBalance() {
System.out.println("Balance - " + balance);
}
}
public class SavingsAccount extends Account()
{
SavingsAccount()
{
super();
}
public void deposit(double amt) {
amt=amt+0.1*amt;
balance += amt;
}
}
*****testAccount java****
import java.util.Scanner;
public class testAccount {
/**
* A simple test function to test Account and SavingsAccount class
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome. Which account do you want to create? 1. Normal 2. Savings");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
Account acct = null;
if (choice == 1) {
acct = new Account();
System.out.println("New normal account created.");
acct.displayBalance();
} else if (choice == 2) {
SavingsAccount acc=new SavingsAccount();
System.out.println("Enter innital Balance. It must be greater than 50..");
do
{
double initialBalance=sc.nextDouble();
if(initialBalance < 50)
{
System.out.println("Error.. Not greater than 50");
continue;
}
else{
acc.setBalance(initialBalance);
System.out.println("Specify Deposit Amount");
double amountDeposit=sc.nextDouble();
acc.deposit(amountDeposit);
System.out.println("The amount is"+ acc.displayBalance());
exit;
}
} while(initialBalance>50)
}
//Deposit money to account here
acct.displayBalance();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.