# fi statement

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;
}
``````
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
you can also use a select case / switch case depending on the code language
Author Commented:
I'm using c# but i thought it would be the same in different languages
Senior Software Engineer (Avast)Commented:
If you're happy with a solution that is provided in another language then that's perfectly fine :)

Good luck.
Author Commented:
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;
}
``````
Commented:
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
Author Commented:
ooh and the year is 1985
Commented:
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
Commented:
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
Author Commented:
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
Commented:
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
Author Commented:
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*/
``````
Commented:
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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.