/*
* The java class Account that simultes
* the Account class.
* */
//Account.java
public abstract class Account {
private String name;
private String accountNumber;
private double balance;
//Constructor to set name, account number and balnace
public Account(String name,
String accountNumber,
double balance)
{
this.name=name;
this.accountNumber=accountNumber;
this.balance=balance;
}
public void setBalance(double balance)
{
this.balance=balance;
}
public double getBalance()
{
return balance;
}
public void depositMoney(double amt)
{
this.balance=this.balance+amt;
}
public void withdrawMoney(double amt)
{
this.balance=this.balance-amt;
}
public String toString() {
return "Account Name:"+name
+" Account Number:"+accountNumber
+" Current Balance:"+balance;
}
//abstract methods
public abstract void checkCurrentBalance();
public abstract void bankStatement();
}
--------------------------------------------------------------------------------------------------------------------
----------------
/**
* The class SavingsAccount that simulates the savings account
* */
//SavingsAccount.java
public class SavingsAccount extends Account
{
private double interestRate;
//Constructor
public SavingsAccount(String name, String accountNumber, double balance,
double interestRate ) {
super(name, accountNumber, balance);
this.interestRate=interestRate;
}
//overirde the checkCurrentBalnace
public void checkCurrentBalance() {
System.out.println(super.toString());
System.out.println("Type: Checking Account");
}
//overirde the bankStatement
public void bankStatement()
{
System.out.println(super.toString());
System.out.println("Interest Rate: "
+interestRate+"%");
double balance=super.getBalance();
double interstamt=balance*(interestRate/100.0);
System.out.println("Interest Amount: "
+interstamt+"%");
super.setBalance(balance+interstamt);
System.out.println("Balance:$"+super.getBalance());
}
}
--------------------------------------------------------------------------------------------------------------------
----------------
/*
* The java class CheckingAccount that simultes the checking account.
* */
//CheckingAccount.java
public class CheckingAccount extends Account
{
private final double MONTHLY_FEE=10;
public CheckingAccount(String name, String accountNumber, double balance) {
super(name, accountNumber, balance);
}
public void checkCurrentBalance() {
System.out.println(super.toString());
System.out.println("Type: Checking Account");
}
public void bankStatement() {
System.out.println(super.toString());
System.out.println("Type: Checking Account");
System.out.println("Monthly Fee:$"+MONTHLY_FEE);
super.setBalance(super.getBalance()-MONTHLY_FEE);
System.out.println("New Balance:$"+super.getBalance());
}
}
--------------------------------------------------------------------------------------------------------------------
----------------
/**
* The java program BankTransactions that simulates
* the bank transactions.
* */
//BankTransactions.java
import java.util.Scanner;
public class BankTransactions
{
private final static double SAVINGS_RATE=1.5;
private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)
{
Account act=null;
String name;
String accountNumber;
double balance;
int type;
while(true)
{
//calling menu method
int choice=menu();
switch(choice)
{
case 1:
System.out.println("Enter name");
name=scanner.nextLine();
System.out.println("Enter account number");
accountNumber=scanner.nextLine();
System.out.println("Enter balance");
balance=Double.parseDouble(scanner.nextLine());
System.out.println("Enter type of account");
System.out.println("1.Savings 2.Checking");
type = Integer.parseInt(scanner.nextLine());
if(type==1)
act=new SavingsAccount(name, accountNumber, balance, SAVINGS_RATE);
else
act=new CheckingAccount(name, accountNumber, balance);
break;
case 2:
if(act==null)
System.out.println("First open an account");
else
act.checkCurrentBalance();
break;
case 3:
if(act==null)
System.out.println("First open an account");
else
{
System.out.println("Enter deposit amount : $");
double deposit=Double.parseDouble(scanner.nextLine());
act.depositMoney(deposit);
}
break;
case 4:
if(act==null)
System.out.println("First open an account");
else
{
System.out.println("Enter withdraw amount : $");
double withdraw=Double.parseDouble(scanner.nextLine());
act.withdrawMoney(withdraw);
}
break;
case 5:
if(act==null)
System.out.println("First open an account");
else
act.bankStatement();
break;
}
}
}
//menu method that returns the user choice
private static int menu() {
System.out.println("1. Open new account");
System.out.println("2. Check current balance");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Print Monthly Statement");
System.out.println("Enter your choice");
int choice = Integer.parseInt(scanner.nextLine());
return choice;
}
}
--------------------------------------------------------------------------------------------------------------------
----------------
Sample Output:
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
1
Enter name
Johnson
Enter account number
123456789
Enter balance
1000
Enter type of account
1.Savings 2.Checking
1
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
2
Account Name:Johnson
Account Number:123456789
Current Balance:1000.0
Type: Checking Account
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
3
Enter deposit amount : $
100
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
2
Account Name:Johnson
Account Number:123456789
Current Balance:1100.0
Type: Checking Account
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
4
Enter withdraw amount : $
50
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
3
Enter deposit amount : $
5
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
5
Account Name:Johnson
Account Number:123456789
Current Balance:1055.0
Interest Rate: 1.5%
Interest Amount: 15.825%
Balance:$1070.825
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
Solution
/*
* The java class Account that simultes
* the Account class.
* */
//Account.java
public abstract class Account {
private String name;
private String accountNumber;
private double balance;
//Constructor to set name, account number and balnace
public Account(String name,
String accountNumber,
double balance)
{
this.name=name;
this.accountNumber=accountNumber;
this.balance=balance;
}
public void setBalance(double balance)
{
this.balance=balance;
}
public double getBalance()
{
return balance;
}
public void depositMoney(double amt)
{
this.balance=this.balance+amt;
}
public void withdrawMoney(double amt)
{
this.balance=this.balance-amt;
}
public String toString() {
return "Account Name:"+name
+" Account Number:"+accountNumber
+" Current Balance:"+balance;
}
//abstract methods
public abstract void checkCurrentBalance();
public abstract void bankStatement();
}
--------------------------------------------------------------------------------------------------------------------
----------------
/**
* The class SavingsAccount that simulates the savings account
* */
//SavingsAccount.java
public class SavingsAccount extends Account
{
private double interestRate;
//Constructor
public SavingsAccount(String name, String accountNumber, double balance,
double interestRate ) {
super(name, accountNumber, balance);
this.interestRate=interestRate;
}
//overirde the checkCurrentBalnace
public void checkCurrentBalance() {
System.out.println(super.toString());
System.out.println("Type: Checking Account");
}
//overirde the bankStatement
public void bankStatement()
{
System.out.println(super.toString());
System.out.println("Interest Rate: "
+interestRate+"%");
double balance=super.getBalance();
double interstamt=balance*(interestRate/100.0);
System.out.println("Interest Amount: "
+interstamt+"%");
super.setBalance(balance+interstamt);
System.out.println("Balance:$"+super.getBalance());
}
}
--------------------------------------------------------------------------------------------------------------------
----------------
/*
* The java class CheckingAccount that simultes the checking account.
* */
//CheckingAccount.java
public class CheckingAccount extends Account
{
private final double MONTHLY_FEE=10;
public CheckingAccount(String name, String accountNumber, double balance) {
super(name, accountNumber, balance);
}
public void checkCurrentBalance() {
System.out.println(super.toString());
System.out.println("Type: Checking Account");
}
public void bankStatement() {
System.out.println(super.toString());
System.out.println("Type: Checking Account");
System.out.println("Monthly Fee:$"+MONTHLY_FEE);
super.setBalance(super.getBalance()-MONTHLY_FEE);
System.out.println("New Balance:$"+super.getBalance());
}
}
--------------------------------------------------------------------------------------------------------------------
----------------
/**
* The java program BankTransactions that simulates
* the bank transactions.
* */
//BankTransactions.java
import java.util.Scanner;
public class BankTransactions
{
private final static double SAVINGS_RATE=1.5;
private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)
{
Account act=null;
String name;
String accountNumber;
double balance;
int type;
while(true)
{
//calling menu method
int choice=menu();
switch(choice)
{
case 1:
System.out.println("Enter name");
name=scanner.nextLine();
System.out.println("Enter account number");
accountNumber=scanner.nextLine();
System.out.println("Enter balance");
balance=Double.parseDouble(scanner.nextLine());
System.out.println("Enter type of account");
System.out.println("1.Savings 2.Checking");
type = Integer.parseInt(scanner.nextLine());
if(type==1)
act=new SavingsAccount(name, accountNumber, balance, SAVINGS_RATE);
else
act=new CheckingAccount(name, accountNumber, balance);
break;
case 2:
if(act==null)
System.out.println("First open an account");
else
act.checkCurrentBalance();
break;
case 3:
if(act==null)
System.out.println("First open an account");
else
{
System.out.println("Enter deposit amount : $");
double deposit=Double.parseDouble(scanner.nextLine());
act.depositMoney(deposit);
}
break;
case 4:
if(act==null)
System.out.println("First open an account");
else
{
System.out.println("Enter withdraw amount : $");
double withdraw=Double.parseDouble(scanner.nextLine());
act.withdrawMoney(withdraw);
}
break;
case 5:
if(act==null)
System.out.println("First open an account");
else
act.bankStatement();
break;
}
}
}
//menu method that returns the user choice
private static int menu() {
System.out.println("1. Open new account");
System.out.println("2. Check current balance");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Print Monthly Statement");
System.out.println("Enter your choice");
int choice = Integer.parseInt(scanner.nextLine());
return choice;
}
}
--------------------------------------------------------------------------------------------------------------------
----------------
Sample Output:
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
1
Enter name
Johnson
Enter account number
123456789
Enter balance
1000
Enter type of account
1.Savings 2.Checking
1
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
2
Account Name:Johnson
Account Number:123456789
Current Balance:1000.0
Type: Checking Account
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
3
Enter deposit amount : $
100
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
2
Account Name:Johnson
Account Number:123456789
Current Balance:1100.0
Type: Checking Account
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
4
Enter withdraw amount : $
50
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
3
Enter deposit amount : $
5
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice
5
Account Name:Johnson
Account Number:123456789
Current Balance:1055.0
Interest Rate: 1.5%
Interest Amount: 15.825%
Balance:$1070.825
1. Open new account
2. Check current balance
3. Deposit
4. Withdraw
5. Print Monthly Statement
Enter your choice

The java class Account that simultes the Account class.pdf

  • 1.
    /* * The javaclass Account that simultes * the Account class. * */ //Account.java public abstract class Account { private String name; private String accountNumber; private double balance; //Constructor to set name, account number and balnace public Account(String name, String accountNumber, double balance) { this.name=name; this.accountNumber=accountNumber; this.balance=balance; } public void setBalance(double balance) { this.balance=balance; } public double getBalance() { return balance; } public void depositMoney(double amt) { this.balance=this.balance+amt; }
  • 2.
    public void withdrawMoney(doubleamt) { this.balance=this.balance-amt; } public String toString() { return "Account Name:"+name +" Account Number:"+accountNumber +" Current Balance:"+balance; } //abstract methods public abstract void checkCurrentBalance(); public abstract void bankStatement(); } -------------------------------------------------------------------------------------------------------------------- ---------------- /** * The class SavingsAccount that simulates the savings account * */ //SavingsAccount.java public class SavingsAccount extends Account { private double interestRate; //Constructor public SavingsAccount(String name, String accountNumber, double balance, double interestRate ) { super(name, accountNumber, balance); this.interestRate=interestRate; }
  • 3.
    //overirde the checkCurrentBalnace publicvoid checkCurrentBalance() { System.out.println(super.toString()); System.out.println("Type: Checking Account"); } //overirde the bankStatement public void bankStatement() { System.out.println(super.toString()); System.out.println("Interest Rate: " +interestRate+"%"); double balance=super.getBalance(); double interstamt=balance*(interestRate/100.0); System.out.println("Interest Amount: " +interstamt+"%"); super.setBalance(balance+interstamt); System.out.println("Balance:$"+super.getBalance()); } } -------------------------------------------------------------------------------------------------------------------- ---------------- /* * The java class CheckingAccount that simultes the checking account. * */ //CheckingAccount.java public class CheckingAccount extends Account { private final double MONTHLY_FEE=10; public CheckingAccount(String name, String accountNumber, double balance) { super(name, accountNumber, balance); } public void checkCurrentBalance() { System.out.println(super.toString());
  • 4.
    System.out.println("Type: Checking Account"); } publicvoid bankStatement() { System.out.println(super.toString()); System.out.println("Type: Checking Account"); System.out.println("Monthly Fee:$"+MONTHLY_FEE); super.setBalance(super.getBalance()-MONTHLY_FEE); System.out.println("New Balance:$"+super.getBalance()); } } -------------------------------------------------------------------------------------------------------------------- ---------------- /** * The java program BankTransactions that simulates * the bank transactions. * */ //BankTransactions.java import java.util.Scanner; public class BankTransactions { private final static double SAVINGS_RATE=1.5; private static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { Account act=null; String name; String accountNumber; double balance; int type; while(true) { //calling menu method
  • 5.
    int choice=menu(); switch(choice) { case 1: System.out.println("Entername"); name=scanner.nextLine(); System.out.println("Enter account number"); accountNumber=scanner.nextLine(); System.out.println("Enter balance"); balance=Double.parseDouble(scanner.nextLine()); System.out.println("Enter type of account"); System.out.println("1.Savings 2.Checking"); type = Integer.parseInt(scanner.nextLine()); if(type==1) act=new SavingsAccount(name, accountNumber, balance, SAVINGS_RATE); else act=new CheckingAccount(name, accountNumber, balance); break; case 2: if(act==null) System.out.println("First open an account"); else act.checkCurrentBalance(); break; case 3: if(act==null) System.out.println("First open an account"); else { System.out.println("Enter deposit amount : $"); double deposit=Double.parseDouble(scanner.nextLine()); act.depositMoney(deposit);
  • 6.
    } break; case 4: if(act==null) System.out.println("First openan account"); else { System.out.println("Enter withdraw amount : $"); double withdraw=Double.parseDouble(scanner.nextLine()); act.withdrawMoney(withdraw); } break; case 5: if(act==null) System.out.println("First open an account"); else act.bankStatement(); break; } } } //menu method that returns the user choice private static int menu() { System.out.println("1. Open new account"); System.out.println("2. Check current balance"); System.out.println("3. Deposit"); System.out.println("4. Withdraw"); System.out.println("5. Print Monthly Statement"); System.out.println("Enter your choice"); int choice = Integer.parseInt(scanner.nextLine()); return choice; }
  • 7.
    } -------------------------------------------------------------------------------------------------------------------- ---------------- Sample Output: 1. Opennew account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 1 Enter name Johnson Enter account number 123456789 Enter balance 1000 Enter type of account 1.Savings 2.Checking 1 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 2 Account Name:Johnson Account Number:123456789 Current Balance:1000.0 Type: Checking Account 1. Open new account 2. Check current balance
  • 8.
    3. Deposit 4. Withdraw 5.Print Monthly Statement Enter your choice 3 Enter deposit amount : $ 100 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 2 Account Name:Johnson Account Number:123456789 Current Balance:1100.0 Type: Checking Account 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 4 Enter withdraw amount : $ 50 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 3 Enter deposit amount : $ 5
  • 9.
    1. Open newaccount 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 5 Account Name:Johnson Account Number:123456789 Current Balance:1055.0 Interest Rate: 1.5% Interest Amount: 15.825% Balance:$1070.825 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice Solution /* * The java class Account that simultes * the Account class. * */ //Account.java public abstract class Account { private String name; private String accountNumber; private double balance; //Constructor to set name, account number and balnace public Account(String name, String accountNumber,
  • 10.
    double balance) { this.name=name; this.accountNumber=accountNumber; this.balance=balance; } public voidsetBalance(double balance) { this.balance=balance; } public double getBalance() { return balance; } public void depositMoney(double amt) { this.balance=this.balance+amt; } public void withdrawMoney(double amt) { this.balance=this.balance-amt; } public String toString() { return "Account Name:"+name +" Account Number:"+accountNumber +" Current Balance:"+balance; } //abstract methods
  • 11.
    public abstract voidcheckCurrentBalance(); public abstract void bankStatement(); } -------------------------------------------------------------------------------------------------------------------- ---------------- /** * The class SavingsAccount that simulates the savings account * */ //SavingsAccount.java public class SavingsAccount extends Account { private double interestRate; //Constructor public SavingsAccount(String name, String accountNumber, double balance, double interestRate ) { super(name, accountNumber, balance); this.interestRate=interestRate; } //overirde the checkCurrentBalnace public void checkCurrentBalance() { System.out.println(super.toString()); System.out.println("Type: Checking Account"); } //overirde the bankStatement public void bankStatement() { System.out.println(super.toString()); System.out.println("Interest Rate: " +interestRate+"%"); double balance=super.getBalance(); double interstamt=balance*(interestRate/100.0); System.out.println("Interest Amount: " +interstamt+"%");
  • 12.
    super.setBalance(balance+interstamt); System.out.println("Balance:$"+super.getBalance()); } } -------------------------------------------------------------------------------------------------------------------- ---------------- /* * The javaclass CheckingAccount that simultes the checking account. * */ //CheckingAccount.java public class CheckingAccount extends Account { private final double MONTHLY_FEE=10; public CheckingAccount(String name, String accountNumber, double balance) { super(name, accountNumber, balance); } public void checkCurrentBalance() { System.out.println(super.toString()); System.out.println("Type: Checking Account"); } public void bankStatement() { System.out.println(super.toString()); System.out.println("Type: Checking Account"); System.out.println("Monthly Fee:$"+MONTHLY_FEE); super.setBalance(super.getBalance()-MONTHLY_FEE); System.out.println("New Balance:$"+super.getBalance()); } } -------------------------------------------------------------------------------------------------------------------- ---------------- /**
  • 13.
    * The javaprogram BankTransactions that simulates * the bank transactions. * */ //BankTransactions.java import java.util.Scanner; public class BankTransactions { private final static double SAVINGS_RATE=1.5; private static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { Account act=null; String name; String accountNumber; double balance; int type; while(true) { //calling menu method int choice=menu(); switch(choice) { case 1: System.out.println("Enter name"); name=scanner.nextLine(); System.out.println("Enter account number"); accountNumber=scanner.nextLine(); System.out.println("Enter balance"); balance=Double.parseDouble(scanner.nextLine()); System.out.println("Enter type of account"); System.out.println("1.Savings 2.Checking"); type = Integer.parseInt(scanner.nextLine());
  • 14.
    if(type==1) act=new SavingsAccount(name, accountNumber,balance, SAVINGS_RATE); else act=new CheckingAccount(name, accountNumber, balance); break; case 2: if(act==null) System.out.println("First open an account"); else act.checkCurrentBalance(); break; case 3: if(act==null) System.out.println("First open an account"); else { System.out.println("Enter deposit amount : $"); double deposit=Double.parseDouble(scanner.nextLine()); act.depositMoney(deposit); } break; case 4: if(act==null) System.out.println("First open an account"); else { System.out.println("Enter withdraw amount : $"); double withdraw=Double.parseDouble(scanner.nextLine()); act.withdrawMoney(withdraw); } break; case 5: if(act==null) System.out.println("First open an account");
  • 15.
    else act.bankStatement(); break; } } } //menu method thatreturns the user choice private static int menu() { System.out.println("1. Open new account"); System.out.println("2. Check current balance"); System.out.println("3. Deposit"); System.out.println("4. Withdraw"); System.out.println("5. Print Monthly Statement"); System.out.println("Enter your choice"); int choice = Integer.parseInt(scanner.nextLine()); return choice; } } -------------------------------------------------------------------------------------------------------------------- ---------------- Sample Output: 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 1 Enter name
  • 16.
    Johnson Enter account number 123456789 Enterbalance 1000 Enter type of account 1.Savings 2.Checking 1 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 2 Account Name:Johnson Account Number:123456789 Current Balance:1000.0 Type: Checking Account 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 3 Enter deposit amount : $ 100 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 2 Account Name:Johnson
  • 17.
    Account Number:123456789 Current Balance:1100.0 Type:Checking Account 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 4 Enter withdraw amount : $ 50 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 3 Enter deposit amount : $ 5 1. Open new account 2. Check current balance 3. Deposit 4. Withdraw 5. Print Monthly Statement Enter your choice 5 Account Name:Johnson Account Number:123456789 Current Balance:1055.0 Interest Rate: 1.5% Interest Amount: 15.825% Balance:$1070.825 1. Open new account 2. Check current balance
  • 18.
    3. Deposit 4. Withdraw 5.Print Monthly Statement Enter your choice