•create a new savings account object (for choice 2). •ask the user to enter an i
ID: 3758028 • Letter: #
Question
•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.
/**
* 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);
}
}
----------------------------------------
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();
}
}
Explanation / Answer
package mani;
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();
double iBal;
Account acct = null;
if (choice == 1) {
acct = new Account();
System.out.println("New normal account created.");
acct.displayBalance();
} else if (choice == 2) {
acct=new SavingsAccount();
System.out.println("New savings account created.");
while(true){
System.out.print("Enter initial balance: (>$50) - ");
iBal=sc.nextDouble();
if(iBal<50){
System.out.println("Balance should be greater than 50");
}else{
acct.setBalance(iBal);
break;
}
acct.displayBalance();
}
System.out.print("Enter amount to deposit: ");
double bal=sc.nextDouble();
acct.deposit(bal);
//Here write your code as required in the Question
}
acct.displayBalance();
}
}
package mani;
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);
}
}
package mani;
public class SavingsAccount extends Account {
public void deposit(double amt){
balance+=amt;
balance+=(amt*0.1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.