//test program
import java.text.*;
import java.util.*;
public class TestInheritedAccounts {
public static void main(String[] args) {
int answer;
int choice;
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("Which account do you want to ascess:");
System.out.println("1.Checking\n2.Savings\n");
answer = keyboard.nextInt();
if (answer==1){
Account checking = new CheckingAccount();//creates an object of the checking account
CheckingAccount c = new CheckingAccount();
checking.setBalance(234);
c.readCheckingAccount();//read information for checking account
System.out.println(c.toString());//print info from the toString method
System.out.println("********************************************************************");
}
if (answer==2){
Account savings = new SavingsAccount();//creates an object of the savings account
SavingsAccount s = new SavingsAccount();
s.readSavingsAccount();//read information for savings account
System.out.println(s.toString());
System.out.println("********************************************************************");
}
System.out.println("Do you want to do a different transaction:");
System.out.println("1.Yes\n2.No");
answer = keyboard.nextInt();
}while (answer != 2);
}
}
//account class
import java.util.*;
public class Account {
/************** data fields ****************/
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;//defines the date
/************** constructors **************/
public Account() {
balance = 0;
annualInterestRate = 0;
}
public Account(int id, double balance){
this.id = id;
this.balance = balance;
dateCreated = new Date();
}
/************** mutator methods ************/
public void setID(int id){
this.id = id;
}
public void setBalance(double balance){
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
/****************** Accesor method *******************/
public int getId(){
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public Date getDateCreated(){
return dateCreated;
}
/**************** Additional methods ***********/
public double getMonthlyInterest(){
return balance * (annualInterestRate / 1200);//calculates monthly interest rate
}
public void withdraw (double amount){//calculates balance after withdraw
balance -= amount;
}
public void deposit (double amount){//calculates balance after deposit
balance += amount;
}
}
//checking class
import java.util.*;
public class CheckingAccount extends Account {
int overdraft = 250;//data field
private double balance;
double withdraw;
double deposit;
int choice;
CheckingAccount(){
}
public void readCheckingAccount(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Checking account");
System.out.print("Enter 1 to deposit or 2 to withdraw:");
choice =keyboard.nextInt();
if (choice==1){
System.out.print("Enter amount to be deposit:" );
deposit =keyboard.nextDouble();
}
else if (choice==2){
System.out.print("Enter amount to be withdraw:");
withdraw = keyboard.nextDouble();
}
else {System.out.print("Invalid choice");
}
}
public void withdraw(double withdraw){
if (withdraw > (balance + overdraft)){
System.out.println("You cannot withdraw $" + withdraw +". Your overdraft limit is "+ overdraft+ ". The available balance in your checking account is" + getBalance());
}
else{
balance =- withdraw;}
}
public String toString(){
return "The balance on your checking account is:" + getBalance();
}
}
//savings class
import java.util.*;
public class SavingsAccount extends Account {
int choice;
double withdraw;
double deposit;
private double balance;
SavingsAccount(){
}
public void readSavingsAccount(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Press 1 to deposit or 2 to withdraw:");
choice = keyboard.nextInt();
if (choice == 1){
System.out.print("Enter amount to deposit:" );
deposit = keyboard.nextDouble();
}
if (choice == 2){
System.out.println("Sorry, you cannot withdraw from your savings account at this moment.");
}
}
public String toString(){
return "The balance on your savings account is:" + getBalance();
}
}

New Topic/Question
Reply


MultiQuote




|