[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

fi statement

Posted on 2012-04-13
Medium Priority
361 Views
hello,

I want to make these conditions :

If a user is member less then 5 years then the price is 135

if a user is member less then 10 years then he gets 10% discount
if a user is member less then 15 years then he gets 15% discount
if a user is member more then 15 years then he gets 30% discount

I like to code this but i'm stock

`````` public uint Lidgeld(uint year) {
uint money = 135;
uint temp = 0;
if ((DateTime.Now.Year - year ) > 4 ){

money = 135;
}

if ((DateTime.Now.Year - money) >= 9)
{
temp = money / 100 * 10;
money = temp - money;
}
return money;
}
``````
0
Question by:kensy11
• 5
• 4
• 2
• +2

LVL 31

Accepted Solution

Zoppo earned 1332 total points
ID: 37842237
Hi kensy11,

the first if isn't needed at all, in the second if you subtract money instead of year. I would suggest to calculate the time difference only once and compare it from the biggest to the smallest, in words 'if more than 15 years ... else if more than 10 years ... else if more than 5 years ...'.

ZOPPO
0

LVL 8

Expert Comment

ID: 37842261
you can also use a select case / switch case depending on the code language
0

Author Comment

ID: 37842264
I'm using c# but i thought it would be the same in different languages
0

LVL 40

Expert Comment

ID: 37842278
If you're happy with a solution that is provided in another language then that's perfectly fine :)

Good luck.
0

Author Comment

ID: 37842366
Thanks, ZOPPO

i have done what you told me, but its still not working fine

``````        public uint Lidgeld(uint year) {
uint money = 135;
uint temp = 0;
uint difference = Convert.ToUInt16(DateTime.Now.Year - year);
if (difference > 15)
{
temp = (135 / 100) * 30;
money = 135 - temp;

}
return money;
}
``````
0

LVL 6

Assisted Solution

rkannan2000 earned 668 total points
ID: 37842369
Hi Kensy

Start with the highest discount slab will be the best approach. Just rephrasing the objective.

Price is 135

if a user is member more than 15 years then he gets 30% discount
Else if a user is member more than 10 years then he gets 15% discount
Else if a user is member more than 5 years then he gets 10% discount
Else No discount  ( i.e. user is member less than 5 years)

This can be achieved in different ways in different programming languages. A Simple If-Else statement or a Case statement might help.

Regards
Kannan
0

Author Comment

ID: 37842377
ooh and the year is 1985
0

LVL 31

Expert Comment

ID: 37842395
What exactly is going wrong?

The way you calculate the result is a bit complicated, you can do it easier - and I guess you need to use greater or equal comparsion, i.e.:
``````money = 135;
if (difference >= 15)
{
money = money * 70 / 100;
}
else if ( difference >= 10)
{
money = money * 85 / 100;
}
...
``````
ZOPPO
0

LVL 6

Expert Comment

ID: 37842488
Hi,

This might help!

``````   public uint Lidgeld (int year) {
uint money = 135;
DateTime now = DateTime.Now;
DateTime origin = new DateTime(year, 01, 01);
int Difference = now.Year - origin.Year;

money = ((difference >15)? 135-  (135 / 100) * 30: (difference >10)? 135-  (135 / 100) * 15: (difference >5)? 135-  (135 / 100) * 10:135)

return money;
}
``````

PS: Watchout for typos.

Regards
Kannan
0

Author Comment

ID: 37842527
thank you all, i get it now

one other question how can i make the price look like this  50.00
i want the to see two numbers after the point like this  90.25
0

LVL 31

Expert Comment

ID: 37842620
For this IMO you should use floating point numbers instead of uint.

>> i want the to see two numbers after the point ...
This depends a little bit on how you display it. I guess easiest is to convert the number to a string with Format - take a look here about this: http://www.csharp-examples.net/string-format-double/

ZOPPO
0

Author Comment

ID: 37842805
my program looks like this now , but i have one other problem as you can see in

ConsCode.cs i am using
``````Console.WriteLine(L1.voornaam + " " + L1.naam + " " + ":" + "\t{0:0.00}", L1.Lidgeld(L1.jaar));
``````
5times is there any way so i can write less and get the same result ??

thanks again

Program.cs

``````using System;

namespace ConsAppl1TI29
{
class Program
{
static void Main(string[] args)
{
ConsCode Cc = new ConsCode();
Cc.ExecuteProgram();
}/*Main*/
}/*Program*/
}/*ConsAppl1TI29*/
``````

Lid.cs
``````using System;

namespace ConsAppl1TI29
{
class Lid
{
private string Naam;
private string Voornaam;
private uint Jaar;

public Lid(string Naam, string Voornaam, uint Jaar)
{
this.Naam = Naam;
this.Voornaam = Voornaam;
this.Jaar = Jaar;
}

public Lid (string Naam, string voornaam) : this(Naam, voornaam, Convert.ToUInt16( DateTime.Now.Year))
{

}

public string naam
{
get { return Naam; }
set { Naam = value; }
}

public string voornaam
{
get { return Voornaam; }
set { Voornaam = value; }
}

public uint jaar
{
get {
if (Jaar == 0 || Jaar < 1932)
{
Jaar = Convert.ToUInt16(DateTime.Now.Year);
}
return Jaar; }
set {Jaar = value; }
}

public decimal Lidgeld(uint year) {
decimal money = 135;

uint difference = Convert.ToUInt16(DateTime.Now.Year - year);
if (difference >= 15)
{
money = money * 70 / 100;
}
else if (difference >= 10)
{
money = money * 85 / 100;
}
else if (difference >= 5)
{
money = money * 90 / 100;
}

return money;
}

}/*Lid*/
}/*ConsAppl1TI29*/
``````

ConsCode.cs

``````using System;

namespace ConsAppl1TI29
{
class ConsCode
{
public void ExecuteProgram()
{
Console.Title = ("Lidgelden jaar 2012");
Leden();

}/*ExecuteProgram*/
private void Leden()
{
Lid L1 = new Lid("Jansen", "Piet");
Lid L2 = new Lid("Roels", "Lut", 2003);
Lid L3 = new Lid("Adams", "Tom", 1998);
Lid L4 = new Lid("Dhondt", "Leen", 1985);
Lid L5 = new Lid("Baert", "Jan", 1920);

Console.WriteLine(L1.voornaam + " " + L1.naam + " " + ":" + "\t{0:0.00}", L1.Lidgeld(L1.jaar));
Console.WriteLine(L2.voornaam + " " + L2.naam + " " + ":" + "\t{0:0.00}", L2.Lidgeld(L2.jaar));
Console.WriteLine(L3.voornaam + " " + L3.naam + " " + ":" + "\t{0:0.00}", L3.Lidgeld(L3.jaar));
Console.WriteLine(L4.voornaam + " " + L4.naam + " " + ":" + "\t{0:0.00}", L4.Lidgeld(L4.jaar));
Console.WriteLine(L5.voornaam + " " + L5.naam + " " + ":" + "\t{0:0.00}", L5.Lidgeld(L5.jaar));

}
}/*ConsCode*/
}/*ConsAppl1TI29*/
``````
0

LVL 31

Assisted Solution

Zoppo earned 1332 total points
ID: 37842869
First I would write a function for the output, something like
``````private void Output( Lid L )
{
Console.WriteLine(L.voornaam + " " + L.naam + " " + ":" + "\t{0:0.00}", L.Lidgeld(L.jaar));
}

private void Leden()
{
...
Ouput( L1 );
Ouput( L2 );
...
}
``````
Anyhow you still have to call Output five times this way. It would be better to put the L1 to L5 into a container (i.e. a list or vector), somehow like this:
``````private void Leden()
{
List< Lid > list = new List< Lid >();
list.Add( new Lid("Roels", "Lut", 2003 ) );
...
foreach ( Lid L in list )
{
Output( L );
}
}
``````
I'm not sure if this is all syntactically correct since I very seldom do something in C# and didn't test the code, but I hope the idea is clear ...

ZOPPO
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to aâ€¦
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a feâ€¦
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month18 days, 16 hours left to enroll