• 0

[Java] Progam Errors


Question

public class InfinityAccount

{

? private double balance;

? private double minbalance = INIT_BALANCE;

? private double fees;

?

? public InfinityAccount()

? {

? ? balance = INIT_BALANCE;

? }

? public static void endMonth()

? {

? ? if (balance >= minbalance)

? ? ? Account.printMonthlyStatement();

? ? else

? ? {

? ? ? balance -= fee;

? ? ? Account.printMonthlyStatement();

? ? }

? }

? public void setMonthlyFee(double fee)

? {

? ? fees = fee;

? }

? public void setMinBalanceRequirement(double nMinBalance)

? {

? ? minbalance = nMinBalance;

? }

? public void deposit(double amt)

? {

? ? Account.deposit(amt);

? }

? public void withdraw(double amt)

? {

? ? Account.withdraw(amt);

? }

? public double getBalance()

? {

? ? Account.getBalance();

? }

}

i get InfinityAccount.java: cannot find symbol for INIT_BALANCE

and InfinityAccount.java:: non-static variable balance cannot be referenced from a static context for all this other random stuff.... how do i fix that?

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Hmmm i can't see where you INIT_BALANCE variable is lol...

and your static method is accessing an instance variable, i dont think that would work, primarily because, if you call that static method endMonth(), there's no object which holds the variables balance and minbalance.

Link to comment
Share on other sites

  • 0

private double minbalance = INIT_BALANCE;

balance = INIT_BALANCE;

Dunno what you're trying to do with those, but this obviously won't work, since you didn't define a value for INIT_BALANCE.

U'd need something like: public static final double INIT_BALANCE = 0.00;

public static void endMonth() obviously needs to be public void endMonth()

It uses properties and method of the current class!

And while we're at it...

"Account." is used everywhere... no idea what you're trying to do again, but this won't work like that!

Edited by Mouton
Link to comment
Share on other sites

  • 0

This is what I would do.

on another note I would probable record a comment with deposits and withdrawals such as who depoisted the money

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;


/**
 * an account object 
 */
public class InfinityAccount {

    private double balance;
    private double minbalance = 0.0;
    private double fees;
    private double INIT_BALANCE = 0.0;
    private Vector tranactions = new Vector(); 

    /**
     * a class to record transactions 
     */
    private class Transaction {
        
        private Calendar date;
        private boolean deposit = true;
        private double  currentBalance;
        private double  amount;
        private double  newBalance;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:ss");
        NumberFormat nf = NumberFormat.getInstance();
        
        /**
         * record a deposit
         */   
       public double deposit(double currentBalance, double amount){
           deposit = true;
           date = new GregorianCalendar();
           this.currentBalance = currentBalance;
           this.amount = amount;
           newBalance = currentBalance + amount;
           return newBalance;
       }
       /**
        * record a withdrawal
        */
       public double withdrawal(double currentBalance, double amount){
           deposit = false;
           date = new GregorianCalendar();
           this.currentBalance = currentBalance;
           this.amount = amount;
           newBalance = currentBalance - amount;  
           return newBalance;
       }
       /**
        * pad string for formatting
        */
       private String pad(String str, int pad){
           StringBuffer s = new StringBuffer();
           s.append(str);

           for(int i = str.length(); i < pad; i++)
               s.append(" ");
           
           return s.toString();
       }
       /**
        * return a string for this transaction 
        */
       public String toString(){
           nf.setMaximumFractionDigits(2);
           String datetime = pad(sdf.format(this.date.getTime()), 20);
           String cBalance = pad(nf.format(this.currentBalance),10);
           String nBalance = pad(nf.format(this.newBalance),10);
           String amount   = pad(nf.format(this.amount),10);
           String type     = "Deposit       ";
           if (!deposit) 
               type = "Withdrawal    ";
           return datetime+cBalance+type+amount+nBalance;
       }
    }
    
    /**
     * init account 
     */
    public InfinityAccount()
    {
      balance = INIT_BALANCE;
    }
    /**
     * deposit money into account
     */
    public void deposit(double amount)
    {
      Transaction trans = new Transaction();  
      balance = trans.deposit(balance, amount);
      tranactions.add(trans);
    }
    /**
     * withdraw money
     */
    public void withdrawal(double amount)
    {
      Transaction trans = new Transaction();  
      balance = trans.withdrawal(balance, amount);
      tranactions.add(trans);
    }
    /**
     * get balance
     */
    public double getBalance()
    {
      return balance;
    }
    
    /**
     * print statement 
     */
    public void printMonthlyStatement()
    {
        System.out.println("--------------------------Account Transactions------------------------------");
        System.out.println("DateTime------------Balance---Trans Type----Amount----Balance");
        
        for(int i =0; i < tranactions.size(); i++)
            System.out.println( ((Transaction)tranactions.elementAt(i)).toString());
    }
    /**
     * set a monthly fee 
     */
    public void setMonthlyFee(double fee)
    {
      fees = fee;
    }
    
    /**
     * set min balance req 
     */
    public void setMinBalanceRequirement(double nMinBalance)
    {
      minbalance = nMinBalance;
    }

    /**
     * run 
     */
    public static void main (String[] args) {
        
        InfinityAccount account = new InfinityAccount();
        account.deposit(100.50);
        account.deposit(50);
        account.deposit(700);
        account.withdrawal(50);
        account.withdrawal(70);
        account.printMonthlyStatement();
        
    }
}

Link to comment
Share on other sites

  • 0

I'm not here to do your assignment for you, I coded my own modified version for an example implmentation. I think you can learn a lot from looking at other coding.

on another note your lectures

deposit(double amt)
the word "amt" is not descriptive and is very poor programming

he/she should have used the fullword "amount" as I did. short hand variable make it difficult to read

Link to comment
Share on other sites

  • 0

Hmm due today hey. Hope you got some of it done.

Good luck!

Note: when you extend Account class you dont have to reimplement all methods

such as

protected void printMonthlyStatement()

you only have to implement abstract methods.

you may not to override some though

Link to comment
Share on other sites

  • 0

another poor thing about "printMonthlyStatement()" is that it resets values.

always have a function do one thing. printMonthlyStatement() should only print not do other things such as ini values.

if you are feeling desperate you could look at this sample stuff I attached.

This is actually a very simple important concepts assignment so you will need to understand what happening.

If you dont understand a line of code or what is happening let us know and we can help

Sample.zip

Link to comment
Share on other sites

  • 0

i mananged to do it....

liykh001 - i didnt expect u to do it... thats y i stated the errors.. but yea its all done now works fine thx for the help neways... it helped out

Link to comment
Share on other sites

  • 0

It wasnt a problem doing it cause your lecture already already provided 98% of the code, I just filled in the 2% needed. :) - might have added 20 lines of code

(BTW: it's easy cause I have done similar stuff before)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.