I wonder if anyone can help me out. I have to make a code for a certain type of bar, where the prices change everytime someone buys a drink (example: customer buys 3 beers, the prices of all drinks change, 30 seconds later 5 lemonades are sold, prices of all drinks change again). I would like to make a dynamic price list that changes every time after a purchase (or if that isn't possible: that refreshes every 5 seconds (for example)).
Everytime a purchase has been made, I change the prices in my database and then I execute a query to get the new prices. Normally I would use setText() to show the prices on the screen, but this doesn't work because the GUI where I change the prices is another then the GUI where I have to show them.
Is there anyone who has a clue what I have got to do?
I assume you're talking about a web based GUI (not swing). You can use Ajax with frequent polling on the page which displays the prices. This is probably the easiest solution to implement, but it is not very efficient.
Julie Maenhout wrote:Is there anyone who has a clue what I have got to do?
Make your GUI completely separate from your program logic. Make an intermediary class to go between your GUI and the class that handles the logic. Something like this:
[GUI: handle user activity and display helpful text (like the price list)]
[Intermediary: watch the GUI controls for user events, take the data from those events and hand them off to the Logic, then get the updated price list and return it to the GUI]
[Logic: when new data arrives, update the price list]
You may want to read about the publisher/subscriber design pattern. You would most likely have something that computes all the new drink prices. You can set it up so that your GUI 'registers' with it as a "i want to know when things change" subscriber.
Each time the drink prices change, the publisher would send a message to all subscribers, saying "hey, things have changed". That way, you don't have to waste bandwith by your GUI checking every X seconds...if nothing changes for 10 minutes, there is no reason for these two things to be talking. But as soon as things do change, everyone who needs to know is alerted.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Dec 12, 2012
That is exactly what I mean, but I don't know how and where to send this signal. Is it possible to explain really thoroughly because I'm a real newbie and I don't know what I'm looking for?
I'm sorry if this causes you much work...
There are tons of sites and books that can explain things much better than I could. Google is your friend, as it your local bookstore.
Your class that computes the new prices implements a few methods (among others)...registerObserver, removeObserver, and notifyObserver. Your gui would implement a update() method.
The compute class would have (again, among other things) an ArrayList to hold the interested objects. An object could call the registerObserver() method, passing itself as a reference. The registerObserver() method then adds this reference to the list.
removeObserver does the similar obvious thing.
When the right conditions are met, the notifyObserver method is called. This basically iterates through the ArrayList, and calls the update() method on each object reference it finds there.
I got most of this from the book "Head First Design Patterns", which is a terrific reference. But if you simply google "design patterns Observer", you'll get tons of hits.
Joined: Dec 12, 2012
Thanks for your help! I'll try and see if I can get it to work!