Solved

fi statement

Posted on 2012-04-13
14
355 Views
Last Modified: 2012-06-27
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;
        }

Open in new window

0
Comment
Question by:kensy11
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 333 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

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

Author Comment

by:kensy11
ID: 37842264
I'm using c# but i thought it would be the same in different languages
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 40

Expert Comment

by:evilrix
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

by:kensy11
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;
        }

Open in new window

0
 
LVL 6

Assisted Solution

by:rkannan2000
rkannan2000 earned 167 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

by:kensy11
ID: 37842377
ooh and the year is 1985
0
 
LVL 31

Expert Comment

by:Zoppo
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;
}
...

Open in new window

ZOPPO
0
 
LVL 6

Expert Comment

by:rkannan2000
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;
        }                    

Open in new window


PS: Watchout for typos.

Regards
Kannan
0
 

Author Comment

by:kensy11
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

by:Zoppo
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

by:kensy11
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));

Open in new window

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();
            Console.ReadKey(true);
        }/*Main*/
    }/*Program*/
}/*ConsAppl1TI29*/

Open in new window


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*/

Open in new window


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*/

Open in new window

0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 333 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 );
 ...
}

Open in new window

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("Jansen", "Piet") );
 list.Add( new Lid("Roels", "Lut", 2003 ) );
 ...
 foreach ( Lid L in list )
 {
  Output( L );
 }
}

Open in new window

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question