Write a test program (class named TestAccount) that does the following: • Create
ID: 3664891 • Letter: W
Question
Write a test program (class named TestAccount) that does the following: • Creates 2 objects - a Saving Account and a Checking Account. Get user inputs for account information that is needed to create these 2 objects. • Get user input for an amount to be withdrawn from the Checking Account (for example, $2,500) and an amount to be deposited to the Checking Account (for example, $3,000). Make sure the overdraft restrictions work. • Similarly, withdraw and deposit from and into the Saving Account also. • Finally, print the account information for both the Checking and Saving accounts (i.e., print the current balance (after withdrawal and deposit), the monthly interest, and the date when these two accounts were created).
Explanation / Answer
/**
* @author Srinivas
*
*/
public class Account {
private double balance;
private String name;
private long acctNum;
/**
* @param owner
*/
public Account(String owner) {
balance = 0.0;
name = owner;
}
public Account(double initBal, String owner) {
balance = initBal;
name = owner;
}
/**
* @param initBal
* @param owner
* @param number
*/
public Account(double initBal, String owner, long number) {
balance = initBal;
name = owner;
acctNum = number;
}
/**
* @param amount
*/
public void withdraw(double amount) {
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
/**
* @param amount
*/
public void deposit(double amount) {
balance += amount;
}
/**
* @return
*/
public double getBalance() {
return balance;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return "Name:" + name + " Account Number: " + acctNum + " Balance: "
+ balance;
}
}
import java.util.Scanner;
public class TestAccount {
public static void main(String[] args) {
String name;
double balance;
long acctNum;
Account acct;
Scanner scan = new Scanner(System.in);
System.out.println("Enter account holder's first name");
name = scan.next();
acct = new Account(name);
System.out.println("Account for " + name + ":");
System.out.println(acct);
System.out.println(" Enter initial balance");
balance = scan.nextDouble();
acct = new Account(balance, name);
System.out.println("Account for " + name + ":");
System.out.println(acct);
System.out.println(" Enter account number");
acctNum = scan.nextLong();
acct = new Account(balance, name, acctNum);
System.out.println("Account for " + name + ":");
System.out.println(acct);
System.out.print(" Depositing 100 into account, balance is now ");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print(" Withdrawing $2500, balance is now ");
acct.withdraw(2500);
System.out.println(acct.getBalance());
System.out.print(" Withdrawing $25, balance is now ");
acct.withdraw(25);
System.out.println(acct.getBalance());
}
}
OUTPUT:
Enter account holder's first name
Srinivas
Account for Srinivas:
Name:Srinivas
Account Number: 0
Balance: 0.0
Enter initial balance
234
Account for Srinivas:
Name:Srinivas
Account Number: 0
Balance: 234.0
Enter account number
3244
Account for Srinivas:
Name:Srinivas
Account Number: 3244
Balance: 234.0
Depositing 100 into account, balance is now 334.0
Withdrawing $2500, balance is now Insufficient funds
334.0
Withdrawing $25, balance is now 309.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.