10 Replies - 498 Views - Last Post: 09 November 2015 - 08:09 AM Rate Topic: -----

#1 JavaNoob87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-November 15

Java Swing Simulation Timer HELP

Posted 05 November 2015 - 08:27 AM

Hi,

I need help in regards to a GUI bank account program that changes over time through series of debits and credits. The user selects specific account, initial balance and account is created and they start simulation, a months activity(5 second interval)is recorded, debits and credits are processed until user presses stop ,which is what I need help with. How do I setup the monthly timer interval to perform these actions every 5 seconds to account and update relevant information.

Any help is greatly appreciated

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Java Swing Simulation Timer HELP

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java Swing Simulation Timer HELP

Posted 05 November 2015 - 09:25 AM

See the java.util.Timer class. It has methods for repeated execution at regular intervals.

Also the javax.swing.Timer class could be used.

This post has been edited by NormR: 05 November 2015 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Java Swing Simulation Timer HELP

Posted 05 November 2015 - 01:50 PM

Have you written the bank account program without the timer and simulation? I suggest you start with the business logic, including the methods needed to simulate what a bank account and its user does via the interface you have in mind. Then write a manual driver/interface that reacts to a human user's input. Once you have all that tested and proven, write the stimulation required to make it go without a user's input.

I'm not sure what the stimulated Swing application will look like, but you should think about that as you write the business logic. Once you've gotten that done, you should have a better idea about the requirements for the stimulation part.

This is an interesting project. I hope you come back and let us know how it's going.
Was This Post Helpful? 0
  • +
  • -

#4 JavaNoob87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-November 15

Re: Java Swing Simulation Timer HELP

Posted 06 November 2015 - 03:24 AM

Thank you very much for your help. I will check back with my progress.
Was This Post Helpful? 0
  • +
  • -

#5 JavaNoob87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-November 15

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 07:07 AM

Hi,

Currently having problems with my program. I have the timer class up and running but having difficulty performing the methods I need executed every 5 seconds. I have abstract class which is Account. Then two subclasses which extends this called current and savings. I have two non static methods in my timer class which throws an error because its from a static context. I can't work out how to fix this.

class Month extends TimerTask {
        public void run() {
            System.out.println("Month's up!"); 
            if(BankSim.debOrCred()< 10)
                Account.debit(BankSim.randomValue());
            else
                Account.credit(BankSim.randomValue());
            monthPassed++;
            //timer.cancel(); //Terminate the timer thread
        }


The if statement takes a random number from debOrCred(); method to decide to debit or credit account. Then it takes a randomValue to credit or debit account and is suppose to perform this every 5 seconds to simulate a month. The account class is suppose to be polymorphic so depending on user selecting a current or savings account.


Again any help is greatly appreciated.

Thank you
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 07:15 AM

Quote

which throws an error because its from a static context.

Where is the static context? The posted code doesn't have any static items.
OPs often get that error trying to do too much in the main() method which is static. The solution is to move all the code out of the main() method into a non static method in the class and use a class instance reference to call it.

Note: java naming standards suggest that variable names start with a lower case letter and class names start with uppercase letter. By those standards Account and BankSim are the names of classes.
If so the methods called are static which doesn't make sense.
Should those methods that set credit or debit for an individual account not for all accounts?

This post has been edited by NormR: 09 November 2015 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 07:20 AM

At face value - very little value, because you haven't told us much - in order to overcome the error, create an instance of the timer class and use the instance to access the non-static methods.

I'm not sure this fits with your overall architecture design, because you haven't given us enough info, but it's a start.
Was This Post Helpful? 0
  • +
  • -

#8 JavaNoob87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-November 15

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 07:36 AM

Hi,

I created an instance of the timer class and tried to access the non-static methods but no luck.
Sorry the static context comes from the Account abstract class and its two abstract methods.
Yes NormR they are classes. It's just for an individual account. I'm not creating an array of accounts.

public abstract class Account {

protected double intBalance;
int noOfDebits;
int noOfCredits;

  public Account(double intBal) {
        intBalance = intBal;

    }

    public abstract void debit (double amount);{
       
    }
    
    public abstract void credit (double amount);{
       
    }





I'm sorry if this isn't coming over clear.
Was This Post Helpful? 0
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 07:39 AM

Quote

but no luck.

Please explain.
Copy the full text of any error messages and paste them here.

What you have posted for code so far is just about useless for helping you solve the problem.

This post has been edited by NormR: 09 November 2015 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#10 JavaNoob87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-November 15

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 08:03 AM

non-static method debit(double) cannot be referenced from a static context

non-static method credit(double) cannot be referenced from a static context
Was This Post Helpful? 0
  • +
  • -

#11 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Java Swing Simulation Timer HELP

Posted 09 November 2015 - 08:09 AM

sorry, its hard to guess the problem without seeing the code.

My only suggestions:
get rid of ALL static variables and methods except for main()
move all code from main() to a non-static method

This post has been edited by NormR: 09 November 2015 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1