aspose file tools*
The moose likes Beginning Java and the fly likes should be easy. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "should be easy." Watch "should be easy." New topic
Author

should be easy.

Samuel Robert
Greenhorn

Joined: Dec 02, 2005
Posts: 4
I am trying to create a calculation where different buttons will deduct different amounts from a common or set amount. The buttons do not have to be implemented it is more of the calculation, if statement and structure which im trying to get my head around. If someone could give me any reasonable, simmilar code with good anotation i would be very gratefull. God bless you.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

The buttons do not have to be implemented it is more of the calculation


What do you mean by that?

For buttons, you add an ActionListener to the button and the ActionListener class will do the work. So in that code you do your calculation.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Samuel Robert
Greenhorn

Joined: Dec 02, 2005
Posts: 4
Sorry about that.

I have two seperate files one contains the gui and in the other i am trying to create a calculation class which subtracts a total from the text area. This calculation is where the main clarity is needed. Any idas on what ineed to implement here

thanxx and God bless ya
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hi Samuel. I'm not entirely clear on what you're asking, but perhaps an example of the Model View Controller (MVC) pattern is what you're looking for...

http://www.onjava.com/pub/a/onjava/2004/07/07/genericmvc.html


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The separate class idea is a great one. You can make sure it's working well from the command line before you confuse yourself with the GUI. How much do you have so far and where are you stuck?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Samuel Robert
Greenhorn

Joined: Dec 02, 2005
Posts: 4
SORRY TO TAKE SO LONG TO REPLY. i believe that it wasthe member marc who said that i was not being specific enough, well i have been trying to get the code to a level that one might understand what i am trying to do exactly and i have attached it to this message.

As for the initial seperate class that i was querying about i still have no idea and believe that it is even worse now than when i first mailed if you could just have a look, any help would be much appreciated and very welcome.

In this seperate class what i am trying to do is a calculation where thae cost is deducted untill the cost is paid.
Ah! sounds so simple, although can be a major bummer when your in the elimentry stages of java programming.

Thanks to all who reply.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class CarPark extends JFrame implements ActionListener

{
JButton tenP, twentyP, fiftyP, onePound, twoPounds, //declaring all East buttons.
less1, oneTwo, twoThree, threeFour, overFour; //declaring all West buttons.
JLabel messLabel = new JLabel("Amount to pay: "); //Message label being created.
JTextField message = new JTextField(10); //Text field is being created with its character limit [10].
double amount = 0;
DecimalFormat pounds = new DecimalFormat("�0.00");

public static void main(String[] args)
{
CarPark c = new CarPark();
c.setTitle("Car park payment simulator");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(500, 200);
c.setVisible(true);
}


CarPark() {
setLayout(new BorderLayout());
tenP = new JButton("10p"); //creating a button and showing its caption.
tenP.setEnabled(false);
tenP.addActionListener(this);
twentyP = new JButton("20p");
twentyP.setEnabled(false);
twentyP.addActionListener(this);
fiftyP = new JButton("50p");
fiftyP. setEnabled(false);
fiftyP.addActionListener(this);
onePound = new JButton("�1");
onePound.setEnabled(false);
onePound.addActionListener(this);
twoPounds = new JButton("�2");
twoPounds.setEnabled(false);
twoPounds.addActionListener(this);

less1 = new JButton("Up to 1 hr");
less1.addActionListener(this);
oneTwo = new JButton("1 to 2 hr");
oneTwo.addActionListener(this);
twoThree = new JButton("2 to 3 hr");
twoThree.addActionListener(this);
threeFour = new JButton("3 to 4 hr");
threeFour.addActionListener(this);
overFour = new JButton("Over 4 hr");
overFour.addActionListener(this);

JPanel leftSide = new JPanel();
leftSide.setLayout(new GridLayout(5,1));
leftSide.add(tenP);
leftSide.add(twentyP);
leftSide.add(fiftyP);
leftSide.add(onePound);
leftSide.add(twoPounds);
add("West", leftSide); //This labels our left side West.

JPanel rightSide = new JPanel();
rightSide.setLayout(new GridLayout(5, 1));
rightSide.add(less1);
rightSide.add(oneTwo);
rightSide.add(twoThree);
rightSide.add(threeFour);
rightSide.add(overFour);
add("East", rightSide); //This labels our right side East.

JPanel middle = new JPanel();
middle.setLayout(new FlowLayout());
middle.add(messLabel);
message.setEditable(false); //This states that the message positioned in the center can not be editted.
middle.add(message);
add("Center", middle); //This labels the middle Center.
// add extra code to the constructor
}

public void actionPerformed(ActionEvent e)
{
// add your code here
if(e.getSource()== oneTwo)
{
oneTwo.setEnabled(false);
less1.setEnabled(false);
twoThree.setEnabled(false);
threeFour.setEnabled(false);
overFour.setEnabled(false);
message.setText("�2.00");
}



if (e.getSource()== less1)
{
oneTwo.setEnabled(false);
less1.setEnabled(false);
twoThree.setEnabled(false);
threeFour.setEnabled(false);
overFour.setEnabled(false);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
message.setText("�1.00");
}
if (e.getSource()== twoThree)
{
oneTwo.setEnabled(false);
less1.setEnabled(false);
twoThree.setEnabled(false);
threeFour.setEnabled(false);
overFour.setEnabled(false);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
message.setText("�3.50");
}
if (e.getSource() == threeFour)
{
oneTwo.setEnabled(false);
less1.setEnabled(false);
twoThree.setEnabled(false);
threeFour.setEnabled(false);
overFour.setEnabled(false);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
message.setText("�5.00");
}

if (e.getSource() == overFour)
{
oneTwo.setEnabled(false);
less1.setEnabled(false);
twoThree.setEnabled(false);
threeFour.setEnabled(false);
overFour.setEnabled(false);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
message.setText("�6.00");
}
}
}

This is apparently the seperate class for the calculation a hurdle which seems to big to scale.???


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculation
{
public static int calculate(int amount)
{
double tenpence = Double.parseDouble(tenP);
double twentypence = Double.parseDouble(twentyP);
double fiftypence = Double.parseDouble(fiftyP);
double onepound = Double.parseDouble(onePound);
double twopound = Double.parseDouble(twoPounds);
int amount = 500
tenpence = (10);
twentypence = (20);
fiftypence = (50);
onepound = (100);
twopound = (200);
int total = amount - fiftypence;
return total;

}
}

tHANKS FOR HAVING A LOOK.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Your GUI looks great, so it seems you've got the "view" taken care of. The difficulty appears to be in the "model" -- the logical workings of your program. As Stan mentioned above, this can be developed independent of the other pieces. Once you have the model working, you can "connect" it to the view through a "controller."

My suggestion is to carefully define what this model "is" -- what properties it "has" and exactly what it needs to "do." Basically, this is a Calculator that keeps track of an amount. One thing that it "does" (if I'm understanding your intent correctly) is deduct certain quantities from that amount. So you might envison something like...

Note that in the above code, "amount" is an instance variable outside of the method. At this point, don't worry about how the argument is supplied to the method or how the GUI is updated. You will take care of those details with the controller, assuming that the MVC pattern is how you want to approach this (and the Swing forum is an excellent place to tackle that). For now, concentrate on getting the model working.
[ December 03, 2005: Message edited by: marc weber ]
 
Don't get me started about those stupid light bulbs.
 
subject: should be easy.
 
Similar Threads
which one is better in terms of performance
how to handle mutiple buttons
JTable-Getting Data out to db
JButton with bad Labels
Strictfp why i cant use???