• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

should be easy.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Samuel Robert
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Samuel Robert
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic