Solved

Trouble with calling another class' variable

Posted on 2004-10-05
29
176 Views
Last Modified: 2010-03-31
I can't figure this out, please help.

I have 3 classes;
Superclass Account
Subclass SavingsAcct
Subclass SavingsTester

In SavingsTester I'm trying to call a variable from SavingsAcct but it isn't working, I keep getting the error cannot resolve symbol.  Can anyone see what I'm doing wrong?

Here is the code I'm trying to call from SavingsAcct:
  private int serviceFee = 5;

      public int getServFee() {return serviceFee;}
      public void setServFee(int ServFee) {serviceFee = ServFee;}

// Check Minimum Balance
  public void checkMinBalance() {
    float balance = getCurrentBalance();
    int ServFee = getServFee();
    if (balance < minimumBalance) {
      ServFee = 0;
    }
    else {
      ServFee = 10;
    }
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }

Here is where I'm trying to get the code into SavingsTester:
      SavingsAcct minBalance = new SavingsAcct(checkMinBalance(ServFee));
      output_1 += ("\tService Fee\t" + minBalance.checkMinBalance(ServFee));

Please help!

Thanks for looking,
Erica
0
Comment
Question by:Job4Suprmn
  • 13
  • 11
  • 3
  • +1
29 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 12233544
check the variable scope if its private variable then you can not inherit.
0
 
LVL 4

Expert Comment

by:aratani
ID: 12233551
Erica,

What does the error message say exactly?

AJ
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12233579
If you want it to inherit but otherwise be limited access, use "protected."
0
 

Author Comment

by:Job4Suprmn
ID: 12233614
Well, I got it to work sort of.  It's not outputting correctly.  Here is how I got SavingsTester to work:
      SavingsAcct minBalance = new SavingsAcct();
      output_1 += (minBalance.checkMinBalance() + "\n");

And this is SavingsAcct code:
  public float minimumBalance = 100;
  public int serviceFee = 10;
  public String checkMinBalance() {
    float balance = getCurrentBalance();
//    int ServFee = getServFee();
    if (balance < minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      balance = balance-serviceFee;
      setCurrentBalance(balance);
  }
  return output_1 += ("\tService Fee\t" + balance);
    }

The problem now though is serviceFee isn't changing to 0 when it's supposed to.  And balance won't set right either.  I can paste my full code, but it's kinda long.  Go through it if you like, I'll add points if someone can figure out what's wrong and why it won't set balance right or serviceFee.

Account:
public abstract class Account {
// account attribute definitions
    private int accountNumber;    // private scope limits access to this class
    private float currentBalance;

// full constructor method to populate instance attributes
    public Account (int newAccountNumber, float newCurrentBalance) {
      accountNumber  = newAccountNumber;
      currentBalance = newCurrentBalance;
    }

//null (default) constructor
    public Account ( ) {
      accountNumber = 0000;
      currentBalance = 0.0F;
    }

// accessor methods
    public float getCurrentBalance () {
      return currentBalance;
    }

    public int getAccountNumber () {
      return accountNumber;
    }

// mutator methods
    protected void setCurrentBalance (float newCurrentBalance) {
      currentBalance = newCurrentBalance;
    }

    public void setAccountNumber (int acctNum) {
      accountNumber = acctNum;
    }
 }


SavingsAcct:
public class SavingsAcct extends Account {
// Attributes
  public double interestRate;
  public float minimumBalance = 100;
  public int serviceFee = 10;
  public int transactionCounter = 0;
  public int transactionFee = 25;
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

// Null Constructor
public SavingsAcct () {
    super();
    this.interestRate = interestRate;
}

// Full Constructor
public SavingsAcct (int newAccountNumber, float newCurrentBalance) {
    super(newAccountNumber, newCurrentBalance);
    interestRate = .03/12*newCurrentBalance;
}

  public double getInterestRate () {return interestRate;}

// Make a Deposit
  public void deposit(float depositAmount) {
    float balance = getCurrentBalance();
    balance = balance + depositAmount;
    setCurrentBalance(balance);
  }

// Check if funds exist before withdraw, then make a withdraw
  public void withdraw(float withdrawAmount) {
    float balance = getCurrentBalance();
    if (balance < withdrawAmount) {
      System.out.println("You do not have enough funds in your account to make a withdraw");
    }
      else {
      balance = balance - withdrawAmount;
      setCurrentBalance(balance);
    }
  }

// Check Number of Transactions
  public String checkNumTrans() {
    float balance = getCurrentBalance();
    if (transactionCounter > 3) {
      balance = balance-transactionFee;
      setCurrentBalance(balance);
    }
    else {
      transactionFee = 0;
      balance = balance-transactionFee;
      setCurrentBalance(balance);
    }
    return output_1 += ("\tTransaction Fee\t" + transactionFee);
  }

//      public int getServFee() {return serviceFee;}
//      public void setServFee(int ServFee) {serviceFee = ServFee;}

// Check Minimum Balance
  public String checkMinBalance() {
    float balance = getCurrentBalance();
//    int ServFee = getServFee();
    if (balance < minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      balance = balance-serviceFee;
      setCurrentBalance(balance);
  }
  return output_1 += ("\tService Fee\t" + balance);
    }

// Calculate Interest
  public void calculateInterest() {
    double finalBalance;
    finalBalance = interestRate;
  }
}

SavingsTester:
public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {

// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;

// Process each Savings Account
    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();

// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      double begBalanceNum = Double.parseDouble(begBalance);

      output_1 += ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_1 += ("Beginning Balance: \t" + "$" +begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_1 += ("Transactions: \n");

//      SavingsAcct currentCust2 = new SavingsAcct(accountNumber, begBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);

        if (transCode.equals("W")) {
//          SavingsAcct withdrawAmount = new SavingsAcct(withdrawAmount);
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
//          SavingsAcct transCount = new SavingsAcct(transactionCounter);
//          transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
//          transactionCounter++;
        }

      }

      SavingsAcct minBalance = new SavingsAcct();
      SavingsAcct numTrans = new SavingsAcct();
      SavingsAcct calcInterest = new SavingsAcct();

      output_1 += ("Fees:\n");
      output_1 += (minBalance.checkMinBalance() + "\n");
      output_1 += (numTrans.checkNumTrans() + "\n");
//      output_1 += ("\tInterest:\t" + calcInterest.calculateInterest());

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JOptionPane.showMessageDialog (null, displayArea, "Message",
                                     JOptionPane.INFORMATION_MESSAGE);
    }
  }
}
0
 
LVL 4

Expert Comment

by:aratani
ID: 12233685
Your logic looks great. Are you sure you have recompiled the files and everything after changing the logic. I sometimes forget to do that.

AJ
0
 
LVL 4

Expert Comment

by:aratani
ID: 12233688
Also if it is an error, where does it give the error. Like what is not displaying the current balance.

AJ
0
 

Author Comment

by:Job4Suprmn
ID: 12233722
I'm using JBuilder, so it recompiles everytime I run it basically.

It doesn't give an error, just on the output it always says balance is -10 for the Service Fee when it should be 0.  It's like balance is always 0 when it should change throughout the array in SavingsTester
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12233798
>      serviceFee = 0;
>      balance = balance-serviceFee;
>      setCurrentBalance(balance);

What's the point of that second line? :P "balance = balance - 0;" = "balance = balance;"

Hrmm... Is getCurrentBalance() working correctly (and is the balance set correctly)? It's always good to look for problems starting at the source. ;)
0
 

Author Comment

by:Job4Suprmn
ID: 12233831
lol yeah good point, sorry been working on this for hours.

getCurrentBalance() probably isn't set correctly.  I can't figure out how to make it work right I guess.  I guess it's supposed to be somewhere in SavingsTester, but every time I try to make a new instance of the Savings class in SavingsTester, I get an error.  I'm so lost.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12234046
>>if (balance < minimumBalance) {
change it to
if (balance > minimumBalance) {
0
 

Author Comment

by:Job4Suprmn
ID: 12234075
Nah, if balance < minimumBalance, then you get a service fee.  It's right for what the program needs.  But thanks.
0
 

Author Comment

by:Job4Suprmn
ID: 12234317
Also, each time I go through the output in SavingsTester, it builds on itself, like the first output screen will have the first Savings Account output, the second screen will have both the first and second savings account output, the third will have the first, second, and third savings account's output.  

Here's the newest version of my SavingsTester:

public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {
// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;

// Process each Savings Account
    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();

// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      double begBalanceNum = Double.parseDouble(begBalance);

      output_1 += ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_1 += ("Beginning Balance: \t" + "$" +begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_1 += ("Transactions: \n");

//      SavingsAcct curCust = new SavingsAcct(accountNumber, currentBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);
      SavingsAcct transCount = new SavingsAcct();

        if (transCode.equals("W")) {
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          transCount.transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          transCount.transactionCounter++;
        }
      }

      SavingsAcct minBalance = new SavingsAcct();
      SavingsAcct numTrans = new SavingsAcct();
      SavingsAcct calcInterest = new SavingsAcct();
      SavingsAcct finalBalance = new SavingsAcct();

      output_1 += ("Fees:\n");

// Show Service Fees
      DecimalFormat servFeeFormat = new DecimalFormat("###.00");
      SavingsAcct servFee = new SavingsAcct();
      output_1 += ("\tService Fee\t" + "$" + servFeeFormat.format
                   (servFee.serviceFee) + "\n");

// Show Transaction Fees
      SavingsAcct transFee = new SavingsAcct();
      DecimalFormat transFeeFormat = new DecimalFormat("###.00");
      output_1 += ("\ttransaction Fee\t" + "$" + transFeeFormat.format
                   (transFee.transactionFee)
                   + "\t" + "#" + " withdrawals" + "\n");

// Show Interest Accumulated
      DecimalFormat calcInterestFormat = new DecimalFormat("###.00");
      SavingsAcct interest = new SavingsAcct();
      output_1 += ("\tInterest:\t" + "$" + calcInterestFormat.format
                   (calcInterest.interestRate) + "\n\n");

// Show Ending Balance
      DecimalFormat endBalanceFormat = new DecimalFormat("###.00");
      SavingsAcct endBalance = new SavingsAcct();
      output_1 += ("Ending Balance =\t" + "$" + endBalanceFormat.format
                   (finalBalance.balance));

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JOptionPane.showMessageDialog (null, displayArea, "Message",
                                     JOptionPane.INFORMATION_MESSAGE);
    }
  }
}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12234352
does your problem solved?
0
 

Author Comment

by:Job4Suprmn
ID: 12234388
Nowhere near solved.

I have to make it stop repeating the previous records in each record and also it's still not reading balance anywhere
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Job4Suprmn
ID: 12234493
Upped the points, please help!  Need this done by tomorrow morning and I've been working on it for a week.  Hate to beg, but I'm desperate!
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12234769
modified code

import java.util.*;
import java.text.DecimalFormat;
import javax.swing.*;

//Account:
abstract class Account {
// account attribute definitions
    private int accountNumber;    // private scope limits access to this class
    private float currentBalance;

// full constructor method to populate instance attributes
    public Account (int newAccountNumber, float newCurrentBalance) {
      accountNumber  = newAccountNumber;
      currentBalance = newCurrentBalance;
    }

//null (default) constructor
    public Account ( ) {
      accountNumber = 0000;
      currentBalance = 0.0F;
    }

// accessor methods
    public float getCurrentBalance () {
      return currentBalance;
    }

    public int getAccountNumber () {
      return accountNumber;
    }

// mutator methods
    protected void setCurrentBalance (float newCurrentBalance) {
      currentBalance = newCurrentBalance;
    }

    public void setAccountNumber (int acctNum) {
      accountNumber = acctNum;
    }
 }


//SavingsAcct:
class SavingsAcct extends Account {
// Attributes
  public float interestRate;
  public float minimumBalance = 100;
  public float serviceFee = 10;
  public int transactionCounter = 0;
  public float transactionFee = 25;
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

// Null Constructor
public SavingsAcct () {
    super();
    this.interestRate = interestRate;
}

// Full Constructor
public SavingsAcct (int newAccountNumber, float newCurrentBalance) {
    super(newAccountNumber, newCurrentBalance);
    interestRate = .03f/12.0f*newCurrentBalance;
}

  public double getInterestRate () {return interestRate;}

// Make a Deposit
  public void deposit(float depositAmount) {
    float balance = getCurrentBalance();
    balance = balance + depositAmount;
    setCurrentBalance(balance);
  }

// Check if funds exist before withdraw, then make a withdraw
  public void withdraw(float withdrawAmount) {
    float balance = getCurrentBalance();
    if (balance < withdrawAmount) {
      System.out.println("You do not have enough funds in your account to make a withdraw");
    }
      else {
      balance = balance - withdrawAmount;
      setCurrentBalance(balance);
    }
  }

// Check Number of Transactions
  public String checkNumTrans() {
    float balance = getCurrentBalance();
    if (transactionCounter > 3) {
      balance = balance-transactionFee;
      setCurrentBalance(balance);
    }
    else {
      transactionFee = 0;
      balance = balance-transactionFee;
      setCurrentBalance(balance);
    }
    return output_1 += ("\tTransaction Fee\t" + transactionFee);
  }

//      public int getServFee() {return serviceFee;}
//      public void setServFee(int ServFee) {serviceFee = ServFee;}

// Check Minimum Balance
  public String checkMinBalance() {
    float balance = getCurrentBalance();
//    int ServFee = getServFee();

      System.out.println("balance: " +balance );

      System.out.println("minimumBalance: " + minimumBalance);


    if (balance > minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      balance = balance-serviceFee;
      setCurrentBalance(balance);
  }

  System.out.println("service fee: " +"\tService Fee\t" + balance );
  return output_1 += ("\tService Fee\t" + balance);
    }

// Calculate Interest
  public void calculateInterest() {
    double finalBalance;
    finalBalance = interestRate;
  }
}

//SavingsTester:
/*public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {

// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;

// Process each Savings Account
    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();

// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      double begBalanceNum = Double.parseDouble(begBalance);

      output_1 += ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_1 += ("Beginning Balance: \t" + "$" +begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_1 += ("Transactions: \n");

//      SavingsAcct currentCust2 = new SavingsAcct(accountNumber, begBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);

        if (transCode.equals("W")) {
//          SavingsAcct withdrawAmount = new SavingsAcct(withdrawAmount);
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
//          SavingsAcct transCount = new SavingsAcct(transactionCounter);
//          transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
//          transactionCounter++;
        }

      }

      SavingsAcct minBalance = new SavingsAcct();
      SavingsAcct numTrans = new SavingsAcct();
      SavingsAcct calcInterest = new SavingsAcct();

      output_1 += ("Fees:\n");
      output_1 += (minBalance.checkMinBalance() + "\n");
      output_1 += (numTrans.checkNumTrans() + "\n");
//      output_1 += ("\tInterest:\t" + calcInterest.calculateInterest());

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JOptionPane.showMessageDialog (null, displayArea, "Message",
                                     JOptionPane.INFORMATION_MESSAGE);
    }
  }
}*/

public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {
// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;

// Process each Savings Account
      SavingsAcct transCount = new SavingsAcct();
    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();

// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      float begBalanceNum = Float.parseFloat(begBalance);

      output_1 += ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_1 += ("Beginning Balance: \t" + "$" +begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_1 += ("Transactions: \n");

//      SavingsAcct curCust = new SavingsAcct(accountNumber, currentBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);


        if (transCode.equals("W")) {
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum -= Double.parseDouble(transAmount);
          transCount.transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum += Double.parseDouble(depositAmount);
          transCount.transactionCounter++;
        }
        transCount.setCurrentBalance(begBalanceNum);
      }

      SavingsAcct minBalance = new SavingsAcct();
      SavingsAcct numTrans = new SavingsAcct();
      SavingsAcct calcInterest = new SavingsAcct();
      SavingsAcct finalBalance = new SavingsAcct();

      output_1 += ("Fees:\n");

// Show Service Fees
      DecimalFormat servFeeFormat = new DecimalFormat("###.00");
      SavingsAcct servFee = new SavingsAcct();
      output_1 += ("\tService Fee\t" + "$" + servFeeFormat.format
                   (servFee.serviceFee) + "\n");

// Show Transaction Fees
      SavingsAcct transFee = new SavingsAcct();
      DecimalFormat transFeeFormat = new DecimalFormat("###.00");
      output_1 += ("\ttransaction Fee\t" + "$" + transFeeFormat.format
                   (transFee.transactionFee)
                   + "\t" + "#" + " withdrawals" + "\n");

// Show Interest Accumulated
      DecimalFormat calcInterestFormat = new DecimalFormat("###.00");
      SavingsAcct interest = new SavingsAcct();
      output_1 += ("\tInterest:\t" + "$" + calcInterestFormat.format
                   (calcInterest.interestRate) + "\n\n");

// Show Ending Balance
      DecimalFormat endBalanceFormat = new DecimalFormat("###.00");
      SavingsAcct endBalance = new SavingsAcct();

      float balance = transCount.getCurrentBalance() + calcInterest.interestRate -
      transFee.transactionFee  - servFee.serviceFee;
      transCount.setCurrentBalance(balance);

      output_1 += ("Ending Balance =\t" + "$" + endBalanceFormat.format
                   (transCount.getCurrentBalance()));

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JOptionPane.showMessageDialog (null, displayArea, "Message",
                                     JOptionPane.INFORMATION_MESSAGE);
    }
  }
}
0
 

Author Comment

by:Job4Suprmn
ID: 12235152
K still having some problems...

There are supposed to be 5 output screens total, one for each Savings Account in the Array.  But for some reason it keeps adding on to the screen.  The first screen will have just the first account, the second screen will have the first and second, the third screen will have the first, second, and third, and so on.  Not sure how to fix that.  Also, seems like all the totals are coming out wrong.  This is what the first output screen should look like:

Monthly Savings Account Summary          Account# 2200

Beginning Balance:                   $500.00

Transactions:
                       Withdrew         $100.00
                       Withdrew         $100.00
                       Deposited         $100.00
                       Withdrew         $50.00
                       Withdrew         $200.00

Fees:
                       Service Fee      $00.00
                       transaction fee $25.00           4 withdrawals
                       Interest           $00.31

Ending Balance =                    $125.3



Each customer must maintain a minimum blaance of $100 or they will be charged a service fee of $10.  A transaction fee is charged after 3 withdrawal transactions per month.  Interest Rate is .03/12*endingBalance.
0
 

Author Comment

by:Job4Suprmn
ID: 12235449
Here are my latest revisions:

*****SavingsAcct*****
public class SavingsAcct extends Account {
// Attributes
  public float interestRate = .0025F;
  public float minimumBalance = 100F;
  public float serviceFee = 10F;
  public int transactionCounter = 0;
  public float transactionFee = 25F;
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

// Null Constructor
public SavingsAcct () {
    super();
}

// Full Constructor
public SavingsAcct (int newAccountNumber, float newCurrentBalance) {
    super(newAccountNumber, newCurrentBalance);
}

// Make a Deposit
  public void deposit(float depositAmount) {
    float balance = getCurrentBalance();
    balance = balance + depositAmount;
    setCurrentBalance(balance);
  }

// Check if funds exist before withdraw, then make a withdraw
  public void withdraw(float withdrawAmount) {
    float balance = getCurrentBalance();
    if (balance < withdrawAmount) {
      System.out.println("You do not have enough funds in your account to make a withdraw");
    }
      else {
      balance = balance - withdrawAmount;
      setCurrentBalance(balance);
    }
  }

// Check Number of Transactions
  public String checkNumTrans() {
    float balance = getCurrentBalance();
    if (transactionCounter > 3) {
      balance = balance-transactionFee;
      setCurrentBalance(balance);
    }
    else {
      transactionFee = 0;
      setCurrentBalance(balance);
    }
    return output_1 += ("");
  }

// Check Minimum Balance
  public String checkMinBalance() {
    float balance = getCurrentBalance();
    if (balance < minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      setCurrentBalance(balance);
  }
  return output_1 += ("");
    }

// Calculate Interest
  public String calculateInterest() {
    float balance = getCurrentBalance();
    balance = balance + (interestRate*balance);
    setCurrentBalance(balance);
    return output_1 += ("");
  }
}




*****SavingsTester*****
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;

public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {
// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;

    output_2 += ("Summary of all Savings Account Activity\n\n\n");
    output_2 += ("Acct\tBegBal\tTot_WD\tTot_Dep\t#trans\tEngBal\n");
    output_2 += ("=====================================================================\n\n");

// Process each Savings Account
    SavingsAcct transCount = new SavingsAcct();
    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();

// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      float begBalanceNum = Float.parseFloat(begBalance);

      output_1 += ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_2 += (accountNumber + "\t");
      output_1 += ("Beginning Balance: \t" + "$" + begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_2 += (begBalanceFormat.format(begBalanceNum) + "\t");
      output_1 += ("Transactions: \n");

//      SavingsAcct curCust = new SavingsAcct(accountNumber, currentBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);

        if (transCode.equals("W")) {
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum -= Double.parseDouble(transAmount);
          transCount.transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum += Double.parseDouble(depositAmount);
          transCount.transactionCounter++;
        }
        transCount.setCurrentBalance(begBalanceNum);
      }

      SavingsAcct minBalance = new SavingsAcct();
      SavingsAcct numTrans = new SavingsAcct();
      SavingsAcct calcInterest = new SavingsAcct();
      SavingsAcct finalBalance = new SavingsAcct();

      output_1 += ("Fees:\n");

// Show Service Fees
      DecimalFormat servFeeFormat = new DecimalFormat("###.00");
      SavingsAcct servFee = new SavingsAcct();
      output_1 += ("\tService Fee\t" + "$" + servFeeFormat.format
                   (servFee.serviceFee) + "\n");

// Show Transaction Fees
      SavingsAcct transFee = new SavingsAcct();
      DecimalFormat transFeeFormat = new DecimalFormat("###.00");
      output_1 += ("\ttransaction Fee\t" + "$" + transFeeFormat.format
                   (transFee.transactionFee)
                   + "\t" + "#" + " withdrawals" + "\n");

// Show Interest Accumulated
      DecimalFormat calcInterestFormat = new DecimalFormat("###.00");
      output_1 += ("\tInterest:\t" + "$" + calcInterestFormat.format
                   (calcInterest.interestRate) + "\n\n");

// Show Ending Balance
      DecimalFormat endBalanceFormat = new DecimalFormat("###.00");
      SavingsAcct endBalance = new SavingsAcct();

      float balance = transCount.getCurrentBalance () + calcInterest.interestRate -
          transFee.transactionFee - servFee.serviceFee;
      transCount.setCurrentBalance(balance);
      output_1 += ("Ending Balance =\t" + "$" + endBalanceFormat.format
                   (transCount.getCurrentBalance()));
      output_2 += (transCount.transactionCounter++ +"\t");
      transCount.transactionCounter = 0;
      output_2 += ("$" + endBalanceFormat.format(transCount.getCurrentBalance()) + "\n");

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JOptionPane.showMessageDialog (null, displayArea, "Message",
                                     JOptionPane.INFORMATION_MESSAGE);
    }

// Show Summary of all Savings Account Activity
    JTextArea displayArea2 = new JTextArea();
    displayArea2.setText(output_2);

    JOptionPane.showMessageDialog (null, displayArea2, "Message",
                                   JOptionPane.INFORMATION_MESSAGE);
  }
}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12236453
//*****SavingsAcct*****
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Account:
abstract class Account {
    private int accountNumber;
    private float currentBalance;

    public Account (int newAccountNumber, float newCurrentBalance) {
      accountNumber  = newAccountNumber;
      currentBalance = newCurrentBalance;
    }

    public Account ( ) {
      accountNumber = 0000;
      currentBalance = 0.0F;
    }

    public float getCurrentBalance () {
      return currentBalance;
    }

    public int getAccountNumber () {
      return accountNumber;
    }

    protected void setCurrentBalance (float newCurrentBalance) {
      currentBalance = newCurrentBalance;
    }

    public void setAccountNumber (int acctNum) {
      accountNumber = acctNum;
    }
 }

class SavingsAcct extends Account {
// Attributes
  public float interestRate = .0025F;
  public float minimumBalance = 100F;
  public float serviceFee = 10F;
  public int transactionCounter = 0;
  public float transactionFee = 25F;
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");


public SavingsAcct () {
    super();
}


public SavingsAcct (int newAccountNumber, float newCurrentBalance) {
    super(newAccountNumber, newCurrentBalance);
}

// Make a Deposit
  public void deposit(float depositAmount) {
    float balance = getCurrentBalance();
    balance = balance + depositAmount;
    setCurrentBalance(balance);
  }

// Check if funds exist before withdraw, then make a withdraw
  public void withdraw(float withdrawAmount) {
    float balance = getCurrentBalance();
    if (balance < withdrawAmount) {
      System.out.println("You do not have enough funds in your account to make a withdraw");
    }
      else {
      balance = balance - withdrawAmount;
      setCurrentBalance(balance);
    }
  }

// Check Number of Transactions
  public String checkNumTrans() {
    if (transactionCounter < 3) {
      transactionFee = 0;
    }
    return output_1 += ("");
  }

// Check Minimum Balance
  public String checkMinBalance() {
    float balance = getCurrentBalance();
    if (balance < minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      setCurrentBalance(balance);
  }
  return balance+"";//output_1 += ("");
    }

// Calculate Interest
  public float calculateInterest() {
    float balance = getCurrentBalance();
    float interest = (interestRate*balance);
    return interest;
  }


  public float getServiceFee(){
      checkMinBalance();
      return serviceFee;
  }

  public float getTransactionFee(){
      checkNumTrans();
      return transactionFee;
  }

  public float getInterestEarned(){
      return calculateInterest();
  }

  public int getTransactionCount(){
        return transactionCounter;
  }
}




//*****SavingsTester*****

public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {
// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;

    output_2 += ("Summary of all Savings Account Activity\n\n\n");
    output_2 += ("Acct\tBegBal\tTot_WD\tTot_Dep\t#trans\tEngBal\n");
    output_2 += ("=====================================================================\n\n");

      String temp = "";


// Process each Savings Account

    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();

            SavingsAcct transCount =
            new SavingsAcct(Integer.parseInt(accountNumber), Float.parseFloat(begBalance));
// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      float begBalanceNum = Float.parseFloat(begBalance);

      output_1 = ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_2 += (accountNumber + "\t");
      output_1 += ("Beginning Balance: \t" + "$" + begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_2 += (begBalanceFormat.format(begBalanceNum) + "\t");
      output_1 += ("Transactions: \n");

//      SavingsAcct curCust = new SavingsAcct(accountNumber, currentBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);

        if (transCode.equals("W")) {
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum -= Double.parseDouble(transAmount);
          transCount.transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum += Double.parseDouble(depositAmount);
          //transCount.transactionCounter++;
        }
        transCount.setCurrentBalance(begBalanceNum);
      }

      output_1 += ("Fees:\n");

// Show Service Fees
      DecimalFormat decFormat = new DecimalFormat("###.00");
      output_1 += ("\tService Fee\t" + "$" + decFormat.format
                   (transCount.getServiceFee()) + "\n");
// Show Transaction Fees
      output_1 += ("\ttransaction Fee\t" + "$" + decFormat.format
                   (transCount.getTransactionFee())
                   + "\t" + transCount.getTransactionCount()
                   +"#" + " Transaction" + "\n");

// Show Interest Accumulated
      output_1 += ("\tInterest Earned:\t" + "$" + decFormat.format
                   (transCount.getInterestEarned()) + "\n\n");

      transCount.checkMinBalance();

      float balance =
            transCount.getCurrentBalance () +
            transCount.calculateInterest() -
        transCount.transactionFee - transCount.serviceFee;

      transCount.setCurrentBalance(balance);

      output_1 += ("Ending Balance =\t" + "$" + decFormat.format
                   (transCount.getCurrentBalance()));
      output_2 += (transCount.transactionCounter++ +"\t");
      transCount.transactionCounter = 0;
      output_2 += ("$" + decFormat.format(transCount.getCurrentBalance()) + "\n");

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JButton ok = new JButton("Ok");
      final JDialog dlg = new JDialog((JFrame)null, true);

      ok.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                              dlg.dispose();
                  }
            });

      dlg.getContentPane().add(displayArea, BorderLayout.CENTER);
      dlg.getContentPane().add(ok, BorderLayout.SOUTH);

            dlg.setSize(400,400);
            dlg.setVisible(true);

    }

// Show Summary of all Savings Account Activity
    JTextArea displayArea2 = new JTextArea();
    displayArea2.setText(output_2);



    JOptionPane.showMessageDialog (null, displayArea2, "Message",
                                   JOptionPane.INFORMATION_MESSAGE);
  }
}

0
 

Author Comment

by:Job4Suprmn
ID: 12237109
Nice!  Works great except for the Interest Rate.

The first interest rate is supposed to be .31, it's showing up as .38 for some reason.  Do you think it's calculating interest rate when balance isn't right?
0
 

Author Comment

by:Job4Suprmn
ID: 12237196
Also is there a way to add up the total for the amount of withdraws and the total for the amount of deposits for each record?  For example, the Total Withdraw Amount for Acct 2200 would be $450.00 and the Total Deposit would be $100.00.

Thanks so much for helping me with this!  Upping the points again because you've been such a massive help.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12237330
checkMinBalance()
checkNumTrans()
calculateInterest()

problem is with these methods.
for example in check for minimum balance, check the minimum then set the serviceFee and no need to do recompute the balance.
similarly you have to see how many transactions are happened in checkNumTrans()  no need to recompute balance.
similarly with calculateInterest().

taking first example
"500 W 100 W 100 D 100 W 50 W 200"


Beginning Balance:       $500.00

Transactions:
      Withdrew       $100.00
      Withdrew       $100.00
      Deposted       $100.00
      Withdrew       $50.00
      Withdrew       $200.00
      
      Balance   = $150.00
      
      when you call checkNumTrans() the amount of  transactionFee was getting deducted, which is not correct,
      because you are doing at the end as shown in below line. Actually you are deducting two times the transactionFee.
      
      float balance =
                  transCount.getCurrentBalance () +
                  transCount.calculateInterest() -
        transCount.transactionFee - transCount.serviceFee;
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12237421
you need to deduct the service fee/ transaction fees at the end not at each check methods.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12237448
//*****SavingsAcct*****
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Account:
abstract class Account {
    private int accountNumber;
    private float currentBalance;

    public Account (int newAccountNumber, float newCurrentBalance) {
      accountNumber  = newAccountNumber;
      currentBalance = newCurrentBalance;
    }

    public Account ( ) {
      accountNumber = 0000;
      currentBalance = 0.0F;
    }

    public float getCurrentBalance () {
      return currentBalance;
    }

    public int getAccountNumber () {
      return accountNumber;
    }

    protected void setCurrentBalance (float newCurrentBalance) {
      currentBalance = newCurrentBalance;
    }

    public void setAccountNumber (int acctNum) {
      accountNumber = acctNum;
    }
 }

class SavingsAcct extends Account {
// Attributes
  public float interestRate = .0025F;
  public float minimumBalance = 100F;
  public float serviceFee = 10F;
  public int transactionCounter = 0;
  public float transactionFee = 25F;
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");


public SavingsAcct () {
    super();
}


public SavingsAcct (int newAccountNumber, float newCurrentBalance) {
    super(newAccountNumber, newCurrentBalance);
}

// Make a Deposit
  public void deposit(float depositAmount) {
    float balance = getCurrentBalance();
    balance = balance + depositAmount;
    setCurrentBalance(balance);
  }

// Check if funds exist before withdraw, then make a withdraw
  public void withdraw(float withdrawAmount) {
    float balance = getCurrentBalance();
    if (balance < withdrawAmount) {
      System.out.println("You do not have enough funds in your account to make a withdraw");
    }
      else {
      balance = balance - withdrawAmount;
      setCurrentBalance(balance);
    }
  }

// Check Number of Transactions
  public String checkNumTrans() {
    if (transactionCounter < 3) {
      transactionFee = 0;
    }
    return output_1 += ("");
  }

// Check Minimum Balance
  public String checkMinBalance() {
    float balance = getCurrentBalance();
    if (balance < minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      setCurrentBalance(balance);
  }
  return balance+"";//output_1 += ("");
    }

// Calculate Interest
  public float calculateInterest() {
    float balance = getCurrentBalance();
    float interest = (interestRate*balance);
    return interest;
  }


  public float getServiceFee(){
      checkMinBalance();
      return serviceFee;
  }

  public float getTransactionFee(){
      checkNumTrans();
      return transactionFee;
  }

  public float getInterestEarned(){
      return calculateInterest();
  }

  public int getTransactionCount(){
        return transactionCounter;
  }
}




//*****SavingsTester*****

public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {
// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;


    output_2 += ("Summary of all Savings Account Activity\n\n\n");
    output_2 += ("Acct\tBegBal\tTot_WD\tTot_Dep\t#trans\tEngBal\n");
    output_2 += ("=====================================================================\n\n");

      String temp = "";


// Process each Savings Account

    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();
    float totalWithdrawn = 0f;
    float totalDeposited = 0f;
            SavingsAcct transCount =
            new SavingsAcct(Integer.parseInt(accountNumber), Float.parseFloat(begBalance));
// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      float begBalanceNum = Float.parseFloat(begBalance);

      output_1 = ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_2 += (accountNumber + "\t");
      output_1 += ("Beginning Balance: \t" + "$" + begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_2 += (begBalanceFormat.format(begBalanceNum) + "\t");
      output_1 += ("Transactions: \n");

//      SavingsAcct curCust = new SavingsAcct(accountNumber, currentBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);

        if (transCode.equals("W")) {
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum -= Double.parseDouble(transAmount);
          totalWithdrawn += Float.parseFloat(withdrawAmount);
          transCount.transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum += Double.parseDouble(depositAmount);
          totalDeposited += Float.parseFloat(depositAmount);
          //transCount.transactionCounter++;
        }
        transCount.setCurrentBalance(begBalanceNum);
      }

      output_1 += ("Fees:\n");

// Show Service Fees
      DecimalFormat decFormat = new DecimalFormat("###.00");
      output_1 += ("\tService Fee\t" + "$" + decFormat.format
                   (transCount.getServiceFee()) + "\n");
// Show Transaction Fees
      output_1 += ("\ttransaction Fee\t" + "$" + decFormat.format
                   (transCount.getTransactionFee())
                   + "\t" + transCount.getTransactionCount()
                   +"#" + " Transaction" + "\n");

// Show Interest Accumulated
      output_1 += ("\tInterest Earned:\t" + "$" + decFormat.format
                   (transCount.getInterestEarned()) + "\n\n");

      output_1 += ("\tTotal Withdrawn Amount :\t" + "$" + decFormat.format
                   (totalWithdrawn) + "\n\n");
      output_1 += ("\tTotal Deposited Amount :\t" + "$" + decFormat.format
                   (totalDeposited) + "\n\n");
      transCount.checkMinBalance();

      float balance =
            transCount.getCurrentBalance () +
            transCount.calculateInterest() -
        transCount.transactionFee - transCount.serviceFee;

      transCount.setCurrentBalance(balance);

      output_1 += ("Ending Balance =\t" + "$" + decFormat.format
                   (transCount.getCurrentBalance()));

      output_2 += (transCount.transactionCounter++);
      transCount.transactionCounter = 0;
         output_2 += ("\t " + "$" + decFormat.format
                              (totalWithdrawn) );
         output_2 += ("\t" + "$" + decFormat.format
                     (totalDeposited));
     output_2 += ("\t" + "$" + decFormat.format(transCount.getCurrentBalance()) + "\n");



      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JButton ok = new JButton("Ok");
      final JDialog dlg = new JDialog((JFrame)null, true);

      ok.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                              dlg.dispose();
                  }
            });

      dlg.getContentPane().add(displayArea, BorderLayout.CENTER);
      dlg.getContentPane().add(ok, BorderLayout.SOUTH);

            dlg.setSize(400,400);
            dlg.setVisible(true);

    }

// Show Summary of all Savings Account Activity
    JTextArea displayArea2 = new JTextArea();
    displayArea2.setText(output_2);



    JOptionPane.showMessageDialog (null, displayArea2, "Message",
                                   JOptionPane.INFORMATION_MESSAGE);
  }
}

0
 
LVL 6

Accepted Solution

by:
expertmb earned 225 total points
ID: 12237468
modified correct version

//*****SavingsAcct*****
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Account:
abstract class Account {
    private int accountNumber;
    private float currentBalance;

    public Account (int newAccountNumber, float newCurrentBalance) {
      accountNumber  = newAccountNumber;
      currentBalance = newCurrentBalance;
    }

    public Account ( ) {
      accountNumber = 0000;
      currentBalance = 0.0F;
    }

    public float getCurrentBalance () {
      return currentBalance;
    }

    public int getAccountNumber () {
      return accountNumber;
    }

    protected void setCurrentBalance (float newCurrentBalance) {
      currentBalance = newCurrentBalance;
    }

    public void setAccountNumber (int acctNum) {
      accountNumber = acctNum;
    }
 }

class SavingsAcct extends Account {
// Attributes
  public float interestRate = .0025F;
  public float minimumBalance = 100F;
  public float serviceFee = 10F;
  public int transactionCounter = 0;
  public float transactionFee = 25F;
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");


public SavingsAcct () {
    super();
}


public SavingsAcct (int newAccountNumber, float newCurrentBalance) {
    super(newAccountNumber, newCurrentBalance);
}

// Make a Deposit
  public void deposit(float depositAmount) {
    float balance = getCurrentBalance();
    balance = balance + depositAmount;
    setCurrentBalance(balance);
  }

// Check if funds exist before withdraw, then make a withdraw
  public void withdraw(float withdrawAmount) {
    float balance = getCurrentBalance();
    if (balance < withdrawAmount) {
      System.out.println("You do not have enough funds in your account to make a withdraw");
    }
      else {
      balance = balance - withdrawAmount;
      setCurrentBalance(balance);
    }
  }

// Check Number of Transactions
  public String checkNumTrans() {
    if (transactionCounter < 3) {
      transactionFee = 0;
    }
    return output_1 += ("");
  }

// Check Minimum Balance
  public String checkMinBalance() {
    float balance = getCurrentBalance();
    if (balance < minimumBalance) {
      balance = balance-serviceFee;
      setCurrentBalance(balance);
    }
    else {
      serviceFee = 0;
      setCurrentBalance(balance);
  }
  return balance+"";//output_1 += ("");
    }

// Calculate Interest
  public float calculateInterest() {
    float balance = getCurrentBalance();
    float interest = (interestRate*balance);
    return interest;
  }


  public float getServiceFee(){
      checkMinBalance();
      return serviceFee;
  }

  public float getTransactionFee(){
      checkNumTrans();
      return transactionFee;
  }

  public float getInterestEarned(){
      return calculateInterest();
  }

  public int getTransactionCount(){
        return transactionCounter;
  }
}




//*****SavingsTester*****

public class SavingsTester {
  public static String output_1 = ("\n");
  public static String output_2 = ("\n");

  public static void main(String[] args) {
// Customer Data Array
    String custArray[] = {
        "2200 500 W 100 W 100 D 100 W 50 W 200",
        "2201 900 W 220 D 200",
        "2202 300 D 500",
        "2203 800 D 200 D 200 D 200",
        "2204 600 W 100 W 100 D 200"};

    String accountNumber;
    String begBalance;
    String transCode;
    String transAmount;


    output_2 += ("Summary of all Savings Account Activity\n\n\n");
    output_2 += ("Acct\tBegBal\tTot_WD\tTot_Dep\t#trans\tEngBal\n");
    output_2 += ("=====================================================================\n\n");

      String temp = "";


// Process each Savings Account

    for (int record=0; record<custArray.length; record++) {
      StringTokenizer currentCust = new StringTokenizer(custArray[record]);
      accountNumber = currentCust.nextToken();
      begBalance = currentCust.nextToken();
    float totalWithdrawn = 0f;
    float totalDeposited = 0f;
            SavingsAcct transCount =
            new SavingsAcct(Integer.parseInt(accountNumber), Float.parseFloat(begBalance));
// Change Beginning Balance String to Double and Format
      DecimalFormat begBalanceFormat = new DecimalFormat("###.00");
      float begBalanceNum = Float.parseFloat(begBalance);

      output_1 = ("Monthly Savings Account Summary \t" + "Acct# " + accountNumber + "\n\n");
      output_2 += (accountNumber + "\t");
      output_1 += ("Beginning Balance: \t" + "$" + begBalanceFormat.format(begBalanceNum) + "\n\n");
      output_2 += (begBalanceFormat.format(begBalanceNum) + "\t");
      output_1 += ("Transactions: \n");

//      SavingsAcct curCust = new SavingsAcct(accountNumber, currentBalance);

      while(currentCust.hasMoreTokens()) {
        transCode = currentCust.nextToken();
        transAmount = currentCust.nextToken();

// Change Transaction Amount String to Double and Format
      DecimalFormat transAmountFormat = new DecimalFormat("###.00");
      double transAmountNum = Double.parseDouble(transAmount);

        if (transCode.equals("W")) {
          String withdrawAmount;
          withdrawAmount = transAmount;
          output_1 += ("\tWithdrew \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum -= Double.parseDouble(transAmount);
          totalWithdrawn += Float.parseFloat(withdrawAmount);
          transCount.transactionCounter++;
        }

        if (transCode.equals("D")) {
          String depositAmount;
          depositAmount = transAmount;
          output_1 += ("\tDeposted \t" + "$" + transAmountFormat.format(transAmountNum) + "\n");
          begBalanceNum += Double.parseDouble(depositAmount);
          totalDeposited += Float.parseFloat(depositAmount);
          //transCount.transactionCounter++;
        }
        transCount.setCurrentBalance(begBalanceNum);
      }

      output_1 += ("Fees:\n");

// Show Service Fees
      DecimalFormat decFormat = new DecimalFormat("###.00");
      output_1 += ("\tService Fee\t" + "$" + decFormat.format
                   (transCount.getServiceFee()) + "\n");
// Show Transaction Fees
      output_1 += ("\ttransaction Fee\t" + "$" + decFormat.format
                   (transCount.getTransactionFee())
                   + "\t" + transCount.getTransactionCount()
                   +"#" + " Transaction" + "\n");

// Show Interest Accumulated
      output_1 += ("\tInterest Earned:\t" + "$" + decFormat.format
                   (transCount.getInterestEarned()) + "\n\n");

      output_1 += ("\tTotal Withdrawn Amount :\t" + "$" + decFormat.format
                   (totalWithdrawn) + "\n\n");
      output_1 += ("\tTotal Deposited Amount :\t" + "$" + decFormat.format
                   (totalDeposited) + "\n\n");
      transCount.checkMinBalance();

      float balance =
            transCount.getCurrentBalance () +
            transCount.calculateInterest() -
        transCount.transactionFee - transCount.serviceFee;

      transCount.setCurrentBalance(balance);

      output_1 += ("Ending Balance =\t" + "$" + decFormat.format
                   (transCount.getCurrentBalance()));



         output_2 += ("$" + decFormat.format
                              (totalWithdrawn) );
         output_2 += ("\t" + "$" + decFormat.format
                     (totalDeposited));

            output_2 += ("\t " + transCount.transactionCounter++);
     output_2 += ("\t" + "$" + decFormat.format(transCount.getCurrentBalance()) + "\n");

transCount.transactionCounter = 0;

      JTextArea displayArea = new JTextArea();
      displayArea.setText(output_1);

      JButton ok = new JButton("Ok");
      final JDialog dlg = new JDialog((JFrame)null, true);

      ok.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                              dlg.dispose();
                  }
            });

      dlg.getContentPane().add(displayArea, BorderLayout.CENTER);
      dlg.getContentPane().add(ok, BorderLayout.SOUTH);

            dlg.setSize(400,400);
            dlg.setVisible(true);

    }

// Show Summary of all Savings Account Activity
    JTextArea displayArea2 = new JTextArea();
    displayArea2.setText(output_2);



    JOptionPane.showMessageDialog (null, displayArea2, "Message",
                                   JOptionPane.INFORMATION_MESSAGE);
  }
}

0
 

Author Comment

by:Job4Suprmn
ID: 12237834
K the transaction counts worked on that one, good job.

Still having trouble with interest though.  I made it so the ending balance reflects interest correctly, but still getting the wrong number for where it outputs the actual amount of interest.  Here's what I did to get the ending balance correct:

In SavingsTester:
      float balance = (transCount.getCurrentBalance() - transCount.transactionFee -
                       transCount.serviceFee);
      transCount.setCurrentBalance(balance);
      balance = transCount.getCurrentBalance () + transCount.calculateInterest();

      transCount.setCurrentBalance(balance);



But for the Interest output I'll have to mess around with SavingsAcct, right?  Shouldn't be a problem in SavingsTester?
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12238110
>>balance = transCount.getCurrentBalance () + transCount.calculateInterest();
should be
balance = balance  + transCount.calculateInterest();


>>but for the Interest output I'll have to mess around with SavingsAcct, right?  
interest is associated with particular account, its always easy and correct way to calculate the interest in accounts class only.

>>Shouldn't be a problem in SavingsTester?
i dont think so.
0
 

Author Comment

by:Job4Suprmn
ID: 12239596
I got it :)

Thanks expertmb, here are your points!
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12245815
welcome :-)

mb..
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now