how would I make an if else function do this? I am new to Java and have no clue. All I know is I am supposed to use if else statements
Make one function
On each of the selection boxes, when a change occurs, use the onchange
event handler to call a function.
Inside that function
if a hand tool is selected and a power tool is selected, calculate the costs
else if a hand tool is selected and a power tool is not selected,
calculate the costs
else if a hand tool is not selected and a power tool is selected,
calculate the costs
if no hand tool is selected and no power tool is selected, zero out the field
How would I make an if else function do this?
Page 1 of 15 Replies - 381 Views - Last Post: 25 April 2012 - 09:31 PM
Replies To: How would I make an if else function do this?
#2
Re: How would I make an if else function do this?
Posted 25 April 2012 - 08:21 PM
You need a GUI for that with JCheckBox
By one fuction your teacher surely means only one actionPerformed
By one fuction your teacher surely means only one actionPerformed
class MyPanel extends JPanel implements ActionListener {
JCheckBox a,b,c,d;
// constructor
MyPanel() {
a = new JCheckBox("Hammer");
a.addActionListener(this);
add(a);
...
}
public void actionPerformed(ActionEvent e) {
// a checkbox has been changed
// check the status of all of them and build invoice accordingly
}
#3
Re: How would I make an if else function do this?
Posted 25 April 2012 - 08:27 PM
This is what I have been able to do so far but only one of the dropdown menus works. I dont understand why the other one won't work
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
Mumal Ahmed
</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-us" />
<!-- This is where your link tag goes when linking to an external style sheet. -->
<!--This is the format you will use when entering Javascript -->
<script type="text/javascript">
/* <![CDATA[ */
window.onload=function() {
if (document.getElementById) {
document.getElementById("selection").onchange=function() { switchme(this); }
}
}
function switchme(SNewSel) {
var ind = SNewSel.selectedIndex;
var txt = document.getElementById('TextField1');
var txt2 = document.getElementById('TextField2');
var txt3 = document.getElementById('TextField3');
var txt4 = document.getElementById('TextField4');
switch (ind) {
case 1: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 2: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 3: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 3: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 4: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 5: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
case 6: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
default:
txt.value='$20';
}
}
/* ]]> */
</script>
</head>
<body>
<div>
<!-- Your XHTML here -->
<h1>Purchase Order</h1>
<form action="FormProcessor.html" name="contact_form" method="get" enctype="application/x-www-form-urlencoded" onsubmit="return confirmSubmit();" onreset="return confirmReset();">
<h3>Products</h3>
<table>
<tr>
<td>
<p>Hand Tools <br />$20.00</p>
</td>
<td>
<select name="hand_tool" size="1" id="selection">
<option value="1">Hand Tool</option>
<option value="2">Saw</option>
<option value="3">Hammer</option>
<option value="4">Screwdriver</option>
<option value="5">Wrench</option>
<option value="6">Pliers</option>
</select>
</td>
<td>
<p>Power Tools <br />$30.00</p>
</td>
<td>
<select name="power_tool" size="1" id="select">
<option value="7">Power Tool</option>
<option value="8">Circular Saw</option>
<option value="9">Sabre Saw</option>
<option value="10">Drill</option>
<option value="11">Belt Sander</option>
<option value="12">Table Saw</option>
</select>
</td>
</tr>
</table>
<h3>Shipping Fees</h3>
<table>
<tr>
<td>Item 1: <input id="TextField1" type="text" name="TextField1" value="" /></td>
<td>Item 2: <input id="TextField2" type="text" name="TextField2" value="" /></td>
</tr>
<tr>
<td>Shipping: <input id="TextField3" type="text" name="shipping" value="" /></td>
<td>Total: <input id="TextField4" type="text" name="total" value="" /></td>
</tr>
</table>
<h3>Customer Information</h3>
<p>First Name
<input type="text" name="first_name" value="FirstName" onclick="document.forms[0].first_name.value = '';" onchange="confirmSubmit();"/>
Last Name
<input type="text" name="last_name" value="LastName" onclick="document.forms[0].last_name.value = '';" onchange="confirmSubmit();" /></p>
<p>Street Address 1
<input type="text" name="address1" value="Address1" onclick="document.forms[0].address1.value = '';" onchange="confirmSubmit();" /></p>
<p>City
<input type="text" name="city" value="City" onclick="document.forms[0].city.value = '';" onchange="confirmSubmit();" />
State
<input type="text" name="state" value="State" onclick="document.forms[0].state.value = '';" onchange="confirmSubmit();" />
Zip
<input type="text" name="zip" value="Zip" onchange="return checkForNumber(this.value);" onclick="document.forms[0].zip.value = '';" /></p>
<p>Phone
<input type="text" name="phone" value="Phone" onchange="return checkForNumber(this.value);" onclick="document.forms[0].phone.value = '';" />
Fax
<input type="text" name="fax" value="Fax" onclick="document.forms[0].fax.value = '';" onchange="return checkForNumber(this.value);" /></p>
<p>Payment Method?
<input type="radio" name="credit_card" />Visa
<input type="radio" name="credit_card" />MasterCard
<input type="radio" name="credit_card" />American Express</p>
<p>Credit Card Number<input type="text" name="cardNumber" value="CardNumber" size="50" onclick="document.forms[0].cardNumber.value = '';" onchange="return checkForNumber(this.value);" /></p>
<table>
<tr>
<td>
<p>Expiration Month</p>
</td>
<td>
<select name="month" size="1" style="width: 200px">
<option value="months">Month</option>
<option value="january">January </option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</td>
<td>
<p>Expiration Year</p>
</td>
<td>
<select name="expiration" size="1">
<option value="2009">2012</option>
<option value="2010">2013</option>
<option value="2011">2014</option>
<option value="2012">2015</option>
<option value="2013">2016</option>
<option value="2014">2017</option>
</select>
</td>
</tr>
</table>
<p><input type="image" alt="image of a submit button" src="submitButton.gif" onchange="confirmSubmit()" /></p>
<p><input type="reset" value="Reset Form" src="resetButton.gif" onchange="confirmReset();" /></p>
</form>
<!-- your XHTML validation icon-->
<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict" height="31" width="88"
style="border: 0px;" /></a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="http://www.austincc.edu/jscholl/images/vcss.png"
alt="Valid CSS!" height="31" width="88" style="border: 0px;" /></a>
</p>
</div>
</body>
</html>
pbl, on 25 April 2012 - 08:21 PM, said:
You need a GUI for that with JCheckBox
By one fuction your teacher surely means only one actionPerformed
By one fuction your teacher surely means only one actionPerformed
class MyPanel extends JPanel implements ActionListener {
JCheckBox a,b,c,d;
// constructor
MyPanel() {
a = new JCheckBox("Hammer");
a.addActionListener(this);
add(a);
...
}
public void actionPerformed(ActionEvent e) {
// a checkbox has been changed
// check the status of all of them and build invoice accordingly
}
#4
Re: How would I make an if else function do this?
Posted 25 April 2012 - 09:25 PM
This code is JavaScrip, I think, you are here in the Java forum
beside the first 4 letters, these two has nothing in common
beside the first 4 letters, these two has nothing in common
#5
Re: How would I make an if else function do this?
Posted 25 April 2012 - 09:29 PM
#6
Re: How would I make an if else function do this?
Posted 25 April 2012 - 09:31 PM
how would I make an if else function do this? I am supposed to use if else statements
Make one function
On each of the selection boxes, when a change occurs, use the onchange
event handler to call a function.
Inside that function
if a hand tool is selected and a power tool is selected, calculate the costs
else if a hand tool is selected and a power tool is not selected,
calculate the costs
else if a hand tool is not selected and a power tool is selected,
calculate the costs
This is what I have so far but only 1 dropdown menu works and I cant figure out why.
Make one function
On each of the selection boxes, when a change occurs, use the onchange
event handler to call a function.
Inside that function
if a hand tool is selected and a power tool is selected, calculate the costs
else if a hand tool is selected and a power tool is not selected,
calculate the costs
else if a hand tool is not selected and a power tool is selected,
calculate the costs
This is what I have so far but only 1 dropdown menu works and I cant figure out why.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-us" />
<!-- This is where your link tag goes when linking to an external style sheet. -->
<!--This is the format you will use when entering Javascript -->
<script type="text/javascript">
/* <![CDATA[ */
window.onload=function() {
if (document.getElementById) {
document.getElementById("selection").onchange=function() { switchme(this); }
}
}
function switchme(SNewSel) {
var ind = SNewSel.selectedIndex;
var txt = document.getElementById('TextField1');
var txt2 = document.getElementById('TextField2');
var txt3 = document.getElementById('TextField3');
var txt4 = document.getElementById('TextField4');
switch (ind) {
case 1: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 2: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 3: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 3: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 4: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
case 5: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
case 6: // hand tool
txt.value = "$20";
txt2.value = "$0";
txt3.value = "$5";
txt4.value = "$25";
break;
default:
txt.value='$20';
}
}
/* ]]> */
</script>
</head>
<body>
<div>
<h1>Purchase Order</h1>
<form action="FormProcessor.html" name="contact_form" method="get" enctype="application/x-www-form-urlencoded" onsubmit="return confirmSubmit();" onreset="return confirmReset();">
<h3>Products</h3>
<table>
<tr>
<td>
<p>Hand Tools <br />$20.00</p>
</td>
<td>
<select name="hand_tool" size="1" id="selection">
<option value="1">Hand Tool</option>
<option value="2">Saw</option>
<option value="3">Hammer</option>
<option value="4">Screwdriver</option>
<option value="5">Wrench</option>
<option value="6">Pliers</option>
</select>
</td>
<td>
<p>Power Tools <br />$30.00</p>
</td>
<td>
<select name="power_tool" size="1" id="select">
<option value="7">Power Tool</option>
<option value="8">Circular Saw</option>
<option value="9">Sabre Saw</option>
<option value="10">Drill</option>
<option value="11">Belt Sander</option>
<option value="12">Table Saw</option>
</select>
</td>
</tr>
</table>
<h3>Shipping Fees</h3>
<table>
<tr>
<td>Item 1: <input id="TextField1" type="text" name="TextField1" value="" /></td>
<td>Item 2: <input id="TextField2" type="text" name="TextField2" value="" /></td>
</tr>
<tr>
<td>Shipping: <input id="TextField3" type="text" name="shipping" value="" /></td>
<td>Total: <input id="TextField4" type="text" name="total" value="" /></td>
</tr>
</table>
<h3>Customer Information</h3>
<p>First Name
<input type="text" name="first_name" value="FirstName" onclick="document.forms[0].first_name.value = '';" onchange="confirmSubmit();"/>
Last Name
<input type="text" name="last_name" value="LastName" onclick="document.forms[0].last_name.value = '';" onchange="confirmSubmit();" /></p>
<p>Street Address 1
<input type="text" name="address1" value="Address1" onclick="document.forms[0].address1.value = '';" onchange="confirmSubmit();" /></p>
<p>City
<input type="text" name="city" value="City" onclick="document.forms[0].city.value = '';" onchange="confirmSubmit();" />
State
<input type="text" name="state" value="State" onclick="document.forms[0].state.value = '';" onchange="confirmSubmit();" />
Zip
<input type="text" name="zip" value="Zip" onchange="return checkForNumber(this.value);" onclick="document.forms[0].zip.value = '';" /></p>
<p>Phone
<input type="text" name="phone" value="Phone" onchange="return checkForNumber(this.value);" onclick="document.forms[0].phone.value = '';" />
Fax
<input type="text" name="fax" value="Fax" onclick="document.forms[0].fax.value = '';" onchange="return checkForNumber(this.value);" /></p>
<p>Payment Method?
<input type="radio" name="credit_card" />Visa
<input type="radio" name="credit_card" />MasterCard
<input type="radio" name="credit_card" />American Express</p>
<p>Credit Card Number<input type="text" name="cardNumber" value="CardNumber" size="50" onclick="document.forms[0].cardNumber.value = '';" onchange="return checkForNumber(this.value);" /></p>
<table>
<tr>
<td>
<p>Expiration Month</p>
</td>
<td>
<select name="month" size="1" style="width: 200px">
<option value="months">Month</option>
<option value="january">January </option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</td>
<td>
<p>Expiration Year</p>
</td>
<td>
<select name="expiration" size="1">
<option value="2009">2012</option>
<option value="2010">2013</option>
<option value="2011">2014</option>
<option value="2012">2015</option>
<option value="2013">2016</option>
<option value="2014">2017</option>
</select>
</td>
</tr>
</table>
<p><input type="image" alt="image of a submit button" src="submitButton.gif" onchange="confirmSubmit()" /></p>
<p><input type="reset" value="Reset Form" src="resetButton.gif" onchange="confirmReset();" /></p>
</form>
</body>
</html>
This post has been edited by Dormilich: 25 April 2012 - 11:36 PM
Reason for edit:: note: merged from a previous JS forum post
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote



|