For my java class, we're doing a project based on inheritance.
I can get my code to update the balanceFloat and display it.
But it won't output the string objects. @.@
This is just a modified version of the full code.
Design class:
BankAccount class
BankChecking subClass
BankSavings subClass
Output:
I can get my code to update the balanceFloat and display it.
But it won't output the string objects. @.@
This is just a modified version of the full code.
Design class:
Code:
JTextArea transactionsTextArea = new JTextArea(10,10);
//Instance Variable
int accountInteger = 0;
//Array
BankAccount[] accounts = new BankAccount[10];
public void intializeTextArea(){
//Example account
accounts[0] = new BankChecking("name","last", "0000", 100.0f, 1);
String accountTypeString = null;
DecimalFormat currencyFormat = new DecimalFormat("$#0.00");
transactionsTextArea.setText("");
if(accounts[accountInteger].getAccountType() == 1){
accountTypeString = "Checking";
}else if(accounts[accountInteger].getAccountType() == 2){
accountTypeString = "Savings";
}
transactionsTextArea.append("Account Type: " + accountTypeString + " Balance: " + currencyFormat.format(accounts[accountInteger].getBalance()) + "\n");
transactionsTextArea.append("First Name: " + accounts[accountInteger].getFirstName() + "Last Name:" + accounts[accountInteger].getLastName() + "Pin Code:" + accounts[accountInteger].getPinCode() + "\n");
transactionsTextArea.append("----------------------------------------------------------\n");
}
Code:
public abstract class BankAccount {
protected static String firstNameString, lastNameString, pinCodeString;
protected int typeAccountInteger;
protected static float balanceFloat;
protected BankAccount(String firstNameString, String lastNameString, String pinCodeString,float balanceNewFloat, int typeNewInteger){
setFirstName(firstNameString);
setLastName(lastNameString);
setAccountType(typeNewInteger);
setPinCode(pinCodeString);
setBalance(balanceNewFloat);
}
protected BankAccount(float balanceNewFloat){
setBalance(balanceNewFloat);
}
protected void setFirstName(String firstNameString){
this.firstNameString = firstNameString;
}
protected void setLastName(String lastNameString){
this.lastNameString = lastNameString;
}
protected void setPinCode(String pinCodeString){
this.pinCodeString = pinCodeString;
}
protected void setBalance(float balanceNewFloat){
this.balanceFloat = balanceNewFloat;
}
protected void setAccountType(int typeNewInteger){
this.typeAccountInteger = typeNewInteger;
}
protected String getFirstName(){
return firstNameString;
}
protected String getLastName(){
return lastNameString;
}
protected String getPinCode(){
return pinCodeString;
}
protected float getBalance(){
return balanceFloat;
}
protected int getAccountType(){
return typeAccountInteger;
}
}
BankChecking subClass
Code:
public class BankChecking extends BankAccount {
public BankChecking(String firstNameString, String lastNameString,
String pinCodeString, float balanceNewFloat, int typeAccountInteger) {
super(firstNameString, lastNameString, pinCodeString, balanceNewFloat, typeAccountInteger);
}
public void withdraw(float balanceNewFloat){
float feeFloat = 0.50f;
if(balanceNewFloat > 750.0f){
BankAccount.balanceFloat = BankAccount.balanceFloat - balanceNewFloat - feeFloat;
}else{
BankAccount.balanceFloat = BankAccount.balanceFloat - balanceNewFloat;
}
}
public void deposit(float balanceNewFloat){
BankAccount.balanceFloat = BankAccount.balanceFloat + balanceNewFloat;
}
public String getFirstName(){
return firstNameString;
}
public String getLastName(){
return lastNameString;
}
public String getPinCode(){
return pinCodeString;
}
public float getBalance(){
return balanceFloat;
}
public int getAccountType(){
return typeAccountInteger;
}
}
BankSavings subClass
Code:
public class BankSaving extends BankAccount {
public BankSaving(String firstNameString, String lastNameString,
String pinCodeString, float balanceNewFloat, int typeAccountInteger) {
super(firstNameString, lastNameString, pinCodeString, balanceNewFloat, typeAccountInteger);
}
public void withdraw(float balanceNewFloat){
float feeFloat = 0.75f;
if(balanceNewFloat > 2000.0f){
BankAccount.balanceFloat = BankAccount.balanceFloat - balanceNewFloat - feeFloat;
}else{
BankAccount.balanceFloat = BankAccount.balanceFloat - balanceNewFloat;
}
}
public void deposit(float balanceNewFloat){
BankAccount.balanceFloat = BankAccount.balanceFloat + balanceNewFloat;
}
public String getPinCode(){
return pinCodeString;
}
}
Output:
Code:
Account Type: Checking Balance: $100.00
First Name: Last Name:Pin Code:
----------------------------------------------------------