Click to See Complete Forum and Search --> : Performing calculations from user input
jhguynn
April 29th, 2000, 04:40 PM
I'm using FrontPage 98. I want to provide a web page that allows the user to "test drive" various "what if" scenarios that affect total price on a product. I'd like the user to select different options from a group of drop-down lists, then press "calculate," and see the total cost displayed. It should operate such that the user can conveniently re-enter diferent values without changing screens, so that many "what-if's" can be tried out quickly.
I'm not even sure if java is the way to go, but I'm giving it a shot. I'd prefer HTML code, actually. (much like I can insert formmail HTML to run a form page).
Thaks forthe help whoever has it!
Jeffrey Guynn
dogbear
May 2nd, 2000, 08:57 AM
JHGuynn,
Java is ideal for what you want to do...
Use the following Applet code as a template and add components and change the formulas to suit your needs...
import java.awt.*;
import java.applet.*;
public class CalculateApplet extends Applet
{
// Components for this applet
Choice productName = new Choice();
TextField amountTF = new TextField();
Button calculateButton = new Button();
Label totalLabel = new Label();
CalcActionListener cAL = new CalcActionListener();
// init() is the first method that is called.
// do all your initializations here...
public void init()
{
// add all the components
add(productName);
add(amountTF);
add(calculateButton);
add(totalLabel);
// initialize all the components
// products
productName.addItem("Rabbit");
productName.addItem("Fox");
productName.addItem("Mother-in-law");
productName.setBounds(0,0,100,25);
// amount
amountTF.setText("");
amountTF.setBounds(0,30,100,25);
// calculateButton
calculateButton.setText("Calculate NOW!");
calculateButton.setBounds(0,60,100,25);
// label
totalLabel.setText("");
totalLabel.setBounds(0,90,100,20);
// now, add the action listener - only for the button!
calculateButton.addActionListener(cAL);
}
// This is our own action listener called CalcActionListener
class CalcActionListener implements java.awt.event.ActionListener
{
// this is where you see if the button was pressed
public void actionPerformed(java.awt.event.ActionEvent event)
{
// if the button was clicked, call the method 'calculateAll'
if ( event.getSource() == calculateButton )
calculateAll();
}
}
// This is the method that calculates the total due and displays the
// total in the Label called 'totalLabel'
void calculateAll()
{
int price = 0; // this will hold the price of the item chosen
int amount = 0; // this will hold the quantity the user wants
// step 1: set the price, depending on which product was chosen
if ( productName.getSelectedIndex() == 0 ) // Rabbit chosen
price = 5;
else if ( productName.getSelectedIndex() == 1 ) // Fox chosen
price = 20;
else if ( productName.getSelectedIndex() == 2 ) // Mother-in-law chosen
price = 2;
// step 2: get the amount from the textfield 'amountTF'
String text = amountTF.getText();
// don't forget the try-catch block to change a string into an int
try {
amount = Integer.parseInt(text);
} catch(NumberFormatException e) {}
// step 3: now calculate the total amount due
int total = price * amount;
// step 4: Show the total in the label
// don't forget to change the int to a String
totalLabel.setText(Integer.toString(total));
}
}
Hope this helps.
Regards,
dogBear
jhguynn
May 2nd, 2000, 11:22 AM
Thanks dogBEAR for the applet!
From the looks of the code you provided, it looks like you hit the nail on the head. But how do I insert that code into my web page? I'm using FrontPage 98....do I just switch to HTML mode and copy/paste it in? If so, where? Between the header and body tags? I tried a couple of things and keep producing a web page that displays the applet code rather than producing a page that actually performs the applet code.
A little more help for the beginner would be most appreciated!
Jeffrey Guynn
sush
May 2nd, 2000, 11:41 AM
Jhguynn,
You would need to compile the applet dogBear gave you and call the Applet.class in your <APPLET> tag,for it to work as you want it and not display the code. Therefore you'd need to download jdk1.2 and compile the applet using javac.
Hope this helps.
Sushma
jhguynn
May 2nd, 2000, 12:07 PM
Thanks Sushma,
Well, looks like more things to learn how to do. Where can I download jdk1.2 so I can compile the thing?
Jeffrey Guynn
jhguynn
May 2nd, 2000, 12:36 PM
Hey Sushma,
OK, I downloaded jdk1.2 which I found easily. I'd love to have you (or anyone) give me a few pointers on how to use the program and show me what it can really do.
Would you please e-mail me so I can get in touch with you? jhguynn@yahoo.com
Thanks.
Jeffrey Guynn
dogbear
May 3rd, 2000, 08:25 AM
Jeffrey,
Welcome to the wonderful world of Java Programming!
dogBear
jhguynn
May 3rd, 2000, 10:13 AM
Thanks dog Bear, but now that I've downloaded jdk1.2 from Sun Microsystems, I don't have a clue about how to compile the code. Any pointers?
Thanks
Jeffrey Guynn
dogbear
May 3rd, 2000, 10:33 AM
JHGuynn,
I welcomed you to the world of Java Programming not because I'm a nice guy(I consider that rumour offensive), but because there's alot you need to learn before writing your first program.
Having said that, there is tons and tonnes of material on the web that can give you tutorials and whatnot.
You should look at some tutorials on this site(codeguru.developer.com).
I can't give you any pointers in this forum because it would be too vast a subject to cover.
So I'll offer you pointers through e-mail, if you like.
thedogbear@hotmail.com
Email me there and we'll get started.
Regards,
dogBear
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.