I have an important question at the moment. I wanted to know how I can go about multiplying or adding checkboxes to display information on a label. For example if I have 3 checkboxes labeled chkMovie, chkAmusement and chkConcert and I have 2 checkbox groups called chkChild and chkAdult. How can I multiply the checkboxes (not the checkbox groups) to the text number entered in a field. There will be one field that asks for the number of adults or kids. If the user picks adults they can't pick children. But if they pick adult they have to select 2 out of the 3 selections. And each one has a price to it. For example Movie can be $6.00, Amusement park $15.00 and Concert $20.00. I'm just basically trying to figure out how to add all those numbers up and then multiply it by the number entered in the text field. What I'm working on is way larger than the examples. But if I can figure it out from this I can apply it to the overall program. Thanks for taking the time to read this and for any person that helps.
Well I don't see how you can multiply or add checkboxes or checkbox groups. You multiply and add numbers, so if a person checks "Concert" and a concert costs $20 and there is some text that they enter into some type of text field somewhere and they press a submit button, I'd say that your job is to extract that text from the text field, convert it to an integer so you can multiply, figure out what checkboxes are checked, look up the price for those checkboxes, do whatever multiplication/addition is required to calculate, then place the answer wherever it is supposed to go, probably in some variable and some display on the screen. Am I understanding the problem correctly?
Yea that's what I'm trying to do, but implementing it in code is what I find difficult.
Yea that's what I'm trying to do, but implementing it in code is what I find difficult.
Well I understand it's part of a larger project and you don't want to (and we don't want you to) put up a thousand lines of code, but you should probably put up some code so we can get a feel. Within your actionPerformed function, which will be called when the "Submit" button is pressed, you'll want to grab the relevant information from whatever text fields you have (each of which should have a variable name associated with it), as well as what checkboxes have been checked (the checkboxes will also have variable names associated with them). From there, hopefully you'll have some type of array of doubles or integers or something where you store what costs go with what checkbox options and you can do your math from that. It's vague advice I am giving, but that's because I haven't seen the code and i don't know the layout, etc., what kind of text fields you are using, variable names, and stuff. Post some code, even if it's just the layout of the dialog box, panel, or whatever you are using, and we can go from there.
I'll go ahead and post it up in a few. Thanks for taking the time to help me out.
This is a sample code that i just made to show you guys what my problem is. In my mind I know how the calculating should work, like I described earlier. My problem is actually implementing it in code. I left my calculations section empty because that's where I'm stuck . And by looking at my price section you can kind of tell what I'm trying to do. For example, if the user picks 300 and Cloverfield for 5 adults it will display the total price on the label (lblSection) for five adults. Another thing that I find challenging that I'm trying to do is if the user just picks the movies and selects the child check box a pop up box will appear showing only the prices of the movies selected for children, not the ones that aren't. Thanks again in advance for taking a look at my problem.
import java.util.Date;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Sample extends Applet implements ActionListener, ItemListener
{
Label lblMovies = new Label("What movie would you like to watch?");
Label lblTickets = new Label("Number of Tickets");
Label lblSelection = new Label("Choose your movie.");
TextField txtMovieNum = new TextField(10);
Button btnPrice = new Button("Price");
Button btnClear = new Button("Clear");
Checkbox chkThree = new Checkbox(" 300");
Checkbox chkBC = new Checkbox(" 10,000 B.C");
Checkbox chkClover = new Checkbox("Cloverfield");
CheckboxGroup cbgPeople = new CheckboxGroup();
Checkbox chkAdult = new Checkbox("Adult", false, cbgPeople);
Checkbox chkChild = new Checkbox("Child", false, cbgPeople);
Checkbox chkClear = new Checkbox(" ",true,cbgPeople);
public void init()
{
add(lblMovies);
add(lblTickets);
add(txtMovieNum);
add(chkThree);
add(chkBC);
add(chkClover);
add(chkAdult);
add(chkChild);
chkThree.addItemListener(this);
chkBC.addItemListener(this);
chkClover.addItemListener(this);
chkAdult.addItemListener(this);
chkChild.addItemListener(this);
add(btnPrice);
btnPrice.addActionListener(this);
add(btnClear);
btnClear.addActionListener(this);
add(lblSelection);
setBackground(Color.green);
setForeground(Color.gray);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnPrice)
{
calc();
}
else if (e.getSource() == btnClear)
{
clear();
}
}
public void itemStateChanged(ItemEvent Choice)
{
Price();
}
public double Price()
{
double price = 0;
if (chkAdult.getState())
if (chkThree.getState()) price = 8.00;
else
if (chkBC.getState()) price = 6.00;
else
if (chkClover.getState()) price = 10.00;
else if (chkChild.getState())
if (chkThree.getState()) price = 4.00;
else
if (chkBC.getState()) price = 3.00;
else
if (chkClover.getState()) price = 5.00;
return price;
}
private void calc()
{
}
private void clear()
{
txtMovieNum.setText("");
chkThree.setState(false);
chkBC.setState(false);
chkClover.setState(false);
chkAdult.setState(false);
chkChild.setState(false);
chkClear.setState(true);
}
}
Didn't realize it was an Applet. I'm real rusty on those (and on checkboxes), so I'm embarrassed to admit that I couldn't run it! :$ I'll give it a shot again later. I need to know how to run an applet anyway, so I'll check it out tonight, though I don't have time now. Hopefully someone else will come along and try it out too. Question: What are you doing with it once it's calculated? If you plan to store it in some global variable, I don't see anyplace to store it. I imagine you'll want to set up a global variable called transactionCost (or whatever you want to name it). The person clicks "Adult", "Cloverfield", and types 4. One Adult Cloverfield ticket costs $10, so you'll want $10 times 4 = $40, then store 40 in the variable transactionCost and do whatever you want with that variable. The math is easy. The main thing is to extract what's been checked and written in the text field. I'll see if I can run it tonight.
Well, I compiled and ran it, but whenever I change anything, it seems to not load the new Applet. My browser is caching it or something, I have no idea, but anyway it makes it so I can't really experiment around with it, so no real advice beyond what I've already given and what I'm adding in this post here.
Your job in the calc function is to extract your all the information in those relevant text fields, check boxes, and buttons. Only "child" or "adult" can be selected, so pull down that information and store it in a boolean. I would change your Price function to accept two parameters: one, the movie name, and two, the boolean variable of whether the person is an adult. Call the Price function from the calc function. I don't see any reason to call it from anywhere else. I would not check state in the Price function. It's kind of a six of one, half dozen of the other type thing, but I just think the program flows better if Price simply takes a movie and a person type and returns a price per ticket.
So in calc, check the state of whether the person is an adult or not. Check the state of your three checkboxes and act accordingly in the calc function instead of the Price function. Use three separate "if" statements as opposed to "if" - "else if" - "else if", as you do in "Price". After all, it's perfectly fine to buy a ticket for more than one movie, right?
So create three separate total varaibles and do three independent calculations, one for each movie. If a movie is not checked, its calculation is 0. Put the price for the total ticket cost (price of movie times number of tickets) in a different variable for each movie, then add the three variables together for your total cost, which will be a fourth variable. Then do whatever with that total_cost variable. You may want to create a text field or a label and add it to your applet with this value on it.
So there's my two cents on this subject. Again, I could run it, but for some reason when I tried changing the program around it (i.e. changing the Label contents), the Applet didn't seem to load my new values, so my help on this is somewhat limited. Good luck.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.