Another Java Question (i think)

They have: 35 posts

Joined: Aug 2001

This is sorta an expansion on my other question.

If it possible to have it so that when the user enters a number in one text box it is multiplied by a number and an amount is subtracted dependant on the number they entered, which will follow the rules below:

IF Quantity is 24 > THEN Discount is 5000 per Ship
IF Quantity is 48 > THEN Discount is 10000 per Ship
IF Quantity is 72 > THEN Discount is 15000 per Ship

NB: Quantity is the number of ships

They have: 117 posts

Joined: Feb 2002

Yes. I'm guessing you mean JavaScript rather than Java? After you calculate the total use a switch statement.

switch (total)
{
case (total>=24 && total<48):
total=total-5000;
break;

case (total>=48 && total<72):
total=total-10000;
break;

case (total>=72):
total=total-15000;
break;

default;
break;
}

pretty sure I got the syntax correct, but regardless -- that's the general idea

They have: 35 posts

Joined: Aug 2001

I aint too good at JavaScript so I would have absolutley no idea how to intergrate that into a page...could you do me an example please?

They have: 117 posts

Joined: Feb 2002

...

They have: 117 posts

Joined: Feb 2002

Forget the switch statement. Below is a test web page including the html and the form:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Discount Test</title>
&lt;script language="JavaScript"&gt;
function do_discount()
{

var total_ordered=document.get_data.number_ordered.value;
var total_cost=document.get_data.number_ordered.value*document.get_data.cost.value

alert("non-discounted: "+total_cost);

if(total_ordered>=24 && total_ordered <48)
{
total_cost=total_cost-5000; 
}
else if(total_ordered>=48 && total_ordered<72)
{
total_cost=total_cost-10000;
}
else if(total_ordered>=72)
{
total_cost=total_cost-15000;
}

alert("discounted: "+total_cost);
}
&lt;/script&gt;
</head>
<body>
<form action="" name="get_data" id="get_data">
<table width=500 cellspacing=4 border=0>
<tr>
<td valign="top" align="right">
Cost:
</td>
<td valign="top" align="left">
<input type="text" name="cost">
</td>
</tr>
<tr>
<td valign="top" align="right">
Number:
</td>
<td valign="top" align="left">
<input type="text" name="number_ordered">
</td>
</tr>
<tr>
<td valign="top" align="right">
&nbsp;
</td>
<td valign="top" align="left">
<input type="button" value="submit" onClick="do_discount()">
</td>
</tr>
</table>
</form>
</body>
</html>
'

They have: 35 posts

Joined: Aug 2001

is it possible to do it without the popup boxes? like just have it appear in another text box?

They have: 117 posts

Joined: Feb 2002

Sure, i just use alertboxes to keep track of values when i write scripts. Just add another form element to the form... the textbox you want the answer displayed in, and when you calculate the discounted price assign it to that text box.

They have: 35 posts

Joined: Aug 2001

'm afraid I must again ask, how?

I've got the text box in but how do I change the code to make it display there?

They have: 117 posts

Joined: Feb 2002

Oh, you are new to javascript. Well, you've certainly picked an ambitious beginning project.

Short Answer:

For starters, add another row to the form and give the text element in it a unique name -- say answer. Then, after you've calculated the discount assign it to that text element like this:

document.get_data.answer.value=total_cost;

Longer Answer:

If you're going to start messing with JavaScript you're going to have to figure out the dot syntax used to path through browser objects. Fortunately, without you realizing it, knowledge of HTML has already prepared you for that. Consider the nesting of the "cost" input box in the HTML:

<body>
    <form name=get_data>
        <input element name=cost>
    </form>
</body>
'

That input element has an attribute (confusing enough, called a property in Object Oriented lingo) called value. Anything assigned to that value property will appear in the text box called "cost."

Above was the nesting scheme for it in HTML, to get to it in JavaScript you need to use the dot syntax, which is just another way of showing that nexting scheme. BTW, the body is called the document in JS. So to get to the value property we type:

document.get_data.cost.value

Trace through the relevant HTML nesting and you'll see that JS paths the same, they just use a different syntax to do it.

They have: 35 posts

Joined: Aug 2001

Discount Test
<script language="JavaScript">
function do_discount()
{

var total_ordered=document.get_data.number_ordered.value;
var total_cost=document.get_data.number_ordered.value*document.get_data.cost.value
document.get_data.answer.value=total_cost;

if(total_ordered>=0 && total_ordered <24)
{
total_cost=total_cost-5000;
}
if(total_ordered>=24 && total_ordered <48)
{
total_cost=total_cost-10000;
}
else if(total_ordered>=48 && total_ordered <72)
{
total_cost=total_cost-15000;
}
}
</script>

Number:

Row ABC

That's the HTMl code of my test page but it doesn't seem to take the discount off. Example:

Enter a quantity of 24 and you should get 13080000, where in reality it should be 12960000

They have: 117 posts

Joined: Feb 2002

In if statements multiply the discount by the number ordered, for example:

total_cost=total_cost-(5000*document.get_data.number_ordered.value);

Also delete document.get_data.answer.value=total_cost; from the top and add document.get_data.answer.value=total_cost just before the closing brace.

They have: 35 posts

Joined: Aug 2001

sorry was off for a week.

Discount Test
<script language="JavaScript">
function do_discount()
{

var total_ordered=document.get_data.number_ordered.value;
var total_cost=document.get_data.number_ordered.value*document.get_data.cost.value

if(total_ordered>=0 && total_ordered >24)
{
total_cost=total_cost-(5000*document.get_data.number_ordered.value);
}
if(total_ordered>=24 && total_ordered >48)
{
total_cost=total_cost-(1000*document.get_data.number_ordered.value);
}
else if(total_ordered>=48 && total_ordered >72)
{
total_cost=total_cost-(15000*document.get_data.number_ordered.value);
}
document.get_data.answer.value=total_cost;
}
</script>

Number:

Row ABC

The figure still don't come out right.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.