I have 3 products. All the products have the same price, I named it : price
Each product has a discount. I defined its variables as follows:
discount1, discount2, discount3
But there is a different tax for each product when the discount equals 100% (for example) as follows:
tax1, tax2, tax3
I wrote this code and it works well: -
if discount1 = 100 and discount2 = 100 and discount3 = 100 then
-
total_price = price - tax1 - tax2 - tax3
-
-
elseif discount2 = 100 and discount3 = 100 then
-
total_price = price - tax2 - tax3
-
-
elseif discount1 = 100 and discount3 = 100 then
-
total_price = price - tax1 - tax3
-
-
elseif discount1 = 100 and discount2 = 100 then
-
total_price = price - tax1 - tax2
-
-
elseif discount1 = 100 then
-
total_price = price - tax1
-
-
elseif discount2 = 100 then
-
total_price = price - tax2
-
-
elseif discount3 = 100 then
-
total_price = price - tax3
-
-
else
-
total_price = price
-
-
end if
-
But I need a simple and short code that do the same functionality because when new products are added, the code will be large and difficult to follow.
So then you don't actually have fields named tax1, discount1, etc.?
Then everything can be handled with a SQL statement. It depends on what SQL server you're using but for Microsoft SQL Server, I would do something like this - SELECT (SUM(price) -
-
SUM(CASE discount WHEN 100 THEN tax ELSE 0 END)
-
) AS total_price
-
FROM Table1
19 3001 - total_price = price
-
If discount1 = 100 then
-
total_price = total_price - tax1
-
End if
-
If discount2 = 100 then
-
total_price = total_price - tax2
-
End if
-
If discount3 = 100 then
-
total_price = total_price - tax3
-
End if
but it would be even better if it was arrays or something similar that you could loop through.
Jared
- total_price = price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100)
@Jhardman: The problem is that the 3 condition can happen at the same time
His code accounts for that. So does mine. Although that depends on if the language you're using has an integer division operator. If not, you can use an integer conversion function or floor function.
@Rabbit: Your statement is not what I need. If you can simplify it using arrays or for loop.
The results will be the same. Why do you have to use a loop?
The issue is that: We subtract the (tax) variable from the (price) only when the (discount) equals 100
Exactly. Both jhardman's code and my code do what you want. Well, for mine you may need to use a different function if asp doesn't have an integer division operator but the concept is the same.
Oh sorry, Jhardman's code is correct. But your code is not correct. suppose the discount is 50 so we don't have to subtract the tax from the price.
I know. It accounts for that.
Also, suppose we have more than three variables for example
discount1, discount2,......, discount100 and tax1, tax2,...., tax100
I think it will be hard to maintain the code. I think we should have an array or a for loop, but how can I accomplish that. so any solution?
Thanks.
It sounds like you're having trouble understanding the formula. Let's break it down.
Given: - price = 1234
-
tax1 = 200
-
discount1 = 50
-
tax2 = 30
-
discount2 = 100
-
tax3 = 4
-
discount3 = 4
-
-
discount1 \ 100 = 50 \ 100 = 0
-
discount2 \ 100 = 100 \ 100 = 1
-
discount3 \ 100 = 100 \ 100 = 1
-
-
tax1 * (discount1 \ 100) = 200 * 0 = 0
-
tax2 * (discount2 \ 100) = 30 * 1 = 30
-
tax3 * (discount3 \ 100) = 4 * 1 = 4
-
-
price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100)
-
price - 0 - 30 - 4 = 1234 - 0 - 30 - 4 = 1200
It sounds like you have the data in a database. What is the table structure?
Yes, I have the data in a database. Here is the table structure:
student_id
Year
Price
Discount
Tax
I need to calculate the total price in all years and when the discoutn = 100 we subtrat the tax from the related price.
Thanks alot for helping me.
Can you give me some sample data?
2100694445,2007,3200,100,150
2100694445,2008,3600,100,250
2100694445,2009,2200,100,50
2100694445,2010,4500,100,250
Thanks.
So then you don't actually have fields named tax1, discount1, etc.?
Then everything can be handled with a SQL statement. It depends on what SQL server you're using but for Microsoft SQL Server, I would do something like this - SELECT (SUM(price) -
-
SUM(CASE discount WHEN 100 THEN tax ELSE 0 END)
-
) AS total_price
-
FROM Table1
Yeah, the query rabbit gave last is the best solution, you will need to make sure that syntax works in oracle, but otherwise, nice solution.
Jared
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Curious |
last post by:
Hi List,
Obviously my google-fu is very weak and I can't find a simple sample
code for C++ that allows me to simply download the source of a
specified web page.
I'm looking at downloading the...
|
by: Dave |
last post by:
I am looking for a Javascript code to preload 4 images, and display a different
image in their place during a mouseover. In other words, 8 images total. I'm not
quite finding anything appropriate....
|
by: Tommy |
last post by:
Hi all. I am studying computer security, and I got this short and
simple (?) c-code. Something is logical wrong in this code, and if
used in the wrong hands of someone, it could be taken advantage...
|
by: Visual Systems AB \(Martin Arvidsson\) |
last post by:
Hi!
I have written an application in studio 2005, when designing all the buttons
and so on are displayed just fine. When i execute the application i get the
boring buttons like there were in...
|
by: Trickynick1001 |
last post by:
Hi, a newbie here. I don't have a real firm grasp on the idea of
Javascript, as I'm used to programming in Qbasic and C. I'm not used
to OOP. Anyway, I really don't have any idea what the...
|
by: Howard |
last post by:
Just curious
What is the largest program you have written in terms of source code length
and complexity?
Howard
|
by: mistral |
last post by:
Need compile python code, source is in html and starts with parameters:
#!/bin/sh -
"exec" "python" "-O" "$0" "$@"
I have installed ActivePython for windows.
|
by: santoshsri |
last post by:
Hello,
Please let me know..
How to check whether the VB code is written in VB3 or VB6 ?
Thanks,
San
|
by: amuven |
last post by:
Hi All,
I'm new to python I have written very basic and small code and I request to help for the following senario.For my project i want to write a python code which should open an already...
|
by: papayrus |
last post by:
I been trying to simply make a directory in C++ but I dont get what all these people are talking about and they never put a complete code in there I can use that includes the includes ect. Can you...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |