Test program
import javax.swing.JOptionPane;
public class testAccount {
public static void main(String[] args) {
boolean done = true;
Account account = null;
Account account2 = null;
String accountTypeString = JOptionPane.showInputDialog(null, " Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
"2- Checking Accoun \n" + "\n");
int accountType = Integer.parseInt(accountTypeString);
if(accountType == 1 || accountType == 2){
Account oneAccount = new Account(1, 100, .045);
account = oneAccount;
Account twoAccount = new Account(2, 100, .045);
account2 = twoAccount;
}
else if(accountType < 1 || accountType > 2){
JOptionPane.showMessageDialog(null, "Invalid input");
}
if(accountType == 1)
while(done){
String savingsAccountActionsString = JOptionPane.showInputDialog( null, " Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" +
"2- Deposit \n" + "\n");
int savingsAccountActions = Integer.parseInt(savingsAccountActionsString);
if(savingsAccountActions == 1){
String savingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
double savingsWithdrawAmount = Double.parseDouble(savingsWithdrawAmountString);
if(savingsWithdrawAmount > account.getBalance()){
JOptionPane.showMessageDialog(null, "Insufficient funds");
}
else {
account.withdraw(savingsWithdrawAmount);
JOptionPane.showMessageDialog(null, "The balance is " + account.getBalance());
done = false;
}
}
}
if(accountType == 2)
while(done){
String checkingsAccountActionsString = JOptionPane.showInputDialog( null, " Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" +
"2- Deposit \n" + "\n");
int checkingsAccountActions = Integer.parseInt(checkingsAccountActionsString);
if(checkingsAccountActions == 1){
String checkingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
double checkingsWithdrawAmount = Double.parseDouble(checkingsWithdrawAmountString);
if(checkingsWithdrawAmount > account2.getBalance()){
JOptionPane.showMessageDialog(null, "Insufficient funds");
}
else {
account2.withdraw(checkingsWithdrawAmount);
JOptionPane.showMessageDialog(null, "The balance is " + account2.getBalance());
done = false;
}
}
}
}
}
Account class
I'm supposed to override the withdraw method so I can do the overdraft in the checkings account.
import java.util.Date;
//Create the class and define the variables needed
public class Account {
private int id;
protected static double balance;
private double annualInterestRate;
private Date dateCreated;
//Constructor with no arguments that creates a default account
Account(){
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}
//A constructor that takes in parameters and does a couple of operations.
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
dateCreated = new Date();
}
//All the accesors used for the different variables
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
//All the mutators used for the different variables
public void setNewDate(Date newDate) {
dateCreated = newDate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate){
annualInterestRate = newAnnualInterestRate;
}
//Method used to return the monthly rate and the monthly balance accordingly
public static double getMonthlyInterestRate(double annualInterestRate) {
double monthlyRate = 0;
monthlyRate = annualInterestRate / 12;
monthlyRate = monthlyRate * balance;
return monthlyRate;
}
//Method used to withdraw money
public void withdraw(double wd) {
balance = balance - wd;
}
//Method used to deposit money
public void deposit(double dp) {
balance = balance + dp;
}
}
Savings account class
I'm not sure why we there is a savings account as it does the same thing as the withdraw method.
//import javax.swing.JOptionPane;
public class savingsAccount extends Account {
public void withdraw(double wd) {
balance = balance + wd;
}
}
Checkings accounts class
It's supposed to allow the user to withdraw more than their balance with the limit being -500
public class checkingAccounts extends Account {
public checkingAccounts(){
}
}

New Topic/Question
Reply



MultiQuote







|