1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Method not calculating properly

Discussion in 'HTML, Graphics & Programming' started by thedude23, Nov 9, 2017.

  1. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Hoping someone here can help, writing a program in c# that does simple calculation too someone salary, Having a problem with a method where it will multiply two numbers but wont subtract the third, it just outputs 700, it won't take the 700 off the salary, if ya get me.


    Code:
     class Employee
        {
            public string employeeName;
            public int employeeAge;
            public string employeeDepartment;
            public int employeeNumber;
            public int weeklyHours;
            public double taxRate;
            public int yearsOfService;
            public double monthlySalary;
            public double TotalYearlyIncome = 0;
            public int Months = 12;
            public double Taxpaid = 0;
           
            public Employee() {}
    
            public Employee(string inEmployeeName,int inEmployeeAge,string inEmployeeDepartment,int inEmployeeNumber,int inWeeklyHours,double inTaxRate,int inYearsOfService,double inMonthlySalary,int inMonths,double inTaxPaid)
            {
              employeeName = inEmployeeName;
              employeeAge = inEmployeeAge;
              employeeDepartment = inEmployeeDepartment;
              employeeNumber = inEmployeeNumber;
              weeklyHours = inWeeklyHours;
              taxRate = inTaxRate;
              yearsOfService = inYearsOfService;
              monthlySalary = inMonthlySalary;
              Months = inMonths;
              Taxpaid = inTaxPaid;
            }
    
            public void EmployeeDetails()
            {
               Console.WriteLine("Employee's Name             :"+employeeName);
               Console.WriteLine("Employee's Age              :"+employeeAge);
               Console.WriteLine("Employee's Department       :"+employeeDepartment);
               Console.WriteLine("Employee's Number           :"+employeeNumber);
               Console.WriteLine("Employee's Weekly Hours:    :"+weeklyHours);
               Console.WriteLine("Employee's Tax Rate         :"+taxRate);
               Console.WriteLine("Employee's Years of service :"+yearsOfService);
               Console.WriteLine("Employee's Monthly Salary   :" + monthlySalary + "\n");
             
           
            }
    
            public double CalYearlySalary(int Months, double monthlySalary)
            {
               
               
                return ( Months * monthlySalary);
           
            }
    
           public double TaxCalculation(double monthlySalary,double taxRate)
           {
             
               return (monthlySalary*taxRate);
             
           }
    
           public double PayAfterTax(double monthlySalary, double taxRate, double Taxpaid)
           {
             
               return(monthlySalary * taxRate) - Taxpaid;
         
           }
         
        }
    
    }


    Code:
     class Program
        {
            static void Main(string[] args)
            {
                Employee a1 = new Employee("Kevin O Sullivan",31,"Software Writer",5667,39,0.20,5,3500,12,0);
                a1.EmployeeDetails();
                Console.WriteLine("Your yearly Salarly is     :" + a1.CalYearlySalary(a1.Months, a1.monthlySalary) + "\n");
                Console.WriteLine("The amount of tax you pay every Month is    :"+a1.TaxCalculation(a1.monthlySalary, a1.taxRate)+"\n");
                Console.WriteLine("Your take home pay is: " + a1.PayAfterTax(a1.monthlySalary,a1.taxRate,a1.Taxpaid));
    
                Employee b2 = new Employee("Mary Murphy",28,"Software Writer",5442,39,0.20,6,3600,12,0);
                b2.EmployeeDetails();
    
                Employee c3 = new Employee("John Cusack", 35, "Software Tester", 5876, 39, 0.20, 10, 4000,12,0);
                c3.EmployeeDetails();
    
                Employee d4 = new Employee("Teresa O Reilly",30,"Software Tester",5798,39,0.20,7,3800,12,0);
                d4.EmployeeDetails();
    
                Employee e5 = new Employee("Timmy Jones",35,"Payroll",6723,39,0.42,8,4200,12,0);
                e5.EmployeeDetails();
    
                Employee f6 = new Employee("Ciara O Mahony",40,"Head Accountant",4432,50,0.42,15,6000,12,0);
                f6.EmployeeDetails();
    
                Employee g7 = new Employee("Chris Murphy",43,"Technical Writer",3445,40,0.42,12,4500,12,0);
                g7.EmployeeDetails();
    
    
                Employee h8 = new Employee("Maire Higgins",49,"CEO/Manager",4214,55,0.42,25,7000,12,0);
                h8.EmployeeDetails();
    
           
           
           
           
           
           
           
           
           
           
            }
        }
    }
    

    The problem is in the method PayAfterTax, any help appreciated.
     
  2. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,545

    return(monthlySalary * taxRate) - Taxpaid;

    will just return whats in the brackets, I think.

    Do the calc first and return the result.

    double calculatedValue = (monthlySalary * taxRate) - Taxpaid;
    return calculatedValue;

    or add more brackets

    return ((monthlySalary * taxRate) - Taxpaid);
     
  3. Moley

    Mobster

    Joined: Aug 29, 2006

    Posts: 2,938

    Location: In a world of my own

    Do you need an extra set of brackets perhaps?

    return((monthlySalary * taxRate) - Taxpaid);

    EDIT: Beaten to it!
     
  4. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Thanks guys I'll give that a go.
     
  5. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,545

    Wait, your passing in zero as tax paid? Works fine if you pass a value in. :p
     
  6. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Still won't work unfortunately.....
     
  7. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Was just thinking that there, how would go about doing it the way I want too do it? I don't want a set value you see.
     
  8. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,545

    Should just be monthlySalary - taxPaid (The result of TaxCalculation) ?

    public double PayAfterTax(double monthlySalary, double taxRate)
    {
    return monthlySalary - TaxCalculation(monthlySalary, taxRate);
    }
     
  9. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Thanks I'll give that a shot.
     
  10. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Lets say I have a method that adds two numbers,How would I pass the answer too another method too do another calculation with that answer? is it even possible too do it?

    Code:
    public int Calculate(int num1, int num2);
    
    {
               return answer = num1 +num2;
          
    }
     
  11. touch

    Capodecina

    Joined: Oct 28, 2006

    Posts: 10,278

    Location: Sufferlandria

    This wont solve any of the problems you've got but it might make it clearer for you to follow. This section:

    Code:
    Employee a1 = new Employee("Kevin O Sullivan",31,"Software Writer",5667,39,0.20,5,3500,12,0);
                a1.EmployeeDetails();
                Console.WriteLine("Your yearly Salarly is     :" + a1.CalYearlySalary(a1.Months, a1.monthlySalary) + "\n");
                Console.WriteLine("The amount of tax you pay every Month is    :"+a1.TaxCalculation(a1.monthlySalary, a1.taxRate)+"\n");
                Console.WriteLine("Your take home pay is: " + a1.PayAfterTax(a1.monthlySalary,a1.taxRate,a1.Taxpaid));
    

    As long as you have that first constructor line where you created Employee Kevin O Sullivan and supplied it with the values, a1, will remember all of it's own attributes ("a1.monthlySalary", "a1.taxRate", etc) You dont need to supply it with these values again.
    You can change those WriteLine lines to:
    Console.WriteLine("Your yearly Salarly is :" + a1.CalYearlySalary() + "\n");
    Console.WriteLine("The amount of tax you pay every Month is :"+a1.TaxCalculation()+"\n");
    Console.WriteLine("Your take home pay is: " + a1.PayAfterTax());

    Then remove the inputs to those method too:
    Code:
    public double CalYearlySalary()
            {
                return ( Months * monthlySalary);
            }
    
           public double TaxCalculation()
           {
               return (monthlySalary*taxRate);
           }
    
           public double PayAfterTax()
           {
               return(monthlySalary * taxRate) - Taxpaid;
           }
     
  12. touch

    Capodecina

    Joined: Oct 28, 2006

    Posts: 10,278

    Location: Sufferlandria


    This is how you would just store the result of that method:

    int result = Calculate(2,3);

    You could then pass this into another method:

    int secondResult = CalculateAgain(result, 2);

    Or, just skip the part where you stored the first result and pass it directly into the second method:

    int secondResult = CalculateAgain(Calculate(2,3),2);

    I've made it bold to try and show where the different methods are.
     
  13. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Thanks alot buddy, I 'll try that.
     
  14. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Anyone tell me why this won't work properly?

    Code:
       static void BankBalance()
            {
                int isValid = 0;
                do
                {
                    try
                    {
    
                        Console.WriteLine("Please enter your Bank Balance amount");
                        int amount = int.Parse(Console.ReadLine());
    
                          if (amount == 0)
                         {
                             Console.WriteLine("Thank you and goodbye");
                             break;
                         }
    
                         else if(amount >= 1 || amount < 100)
                        {
                            Console.WriteLine("You're very poor,are you sure you're not laundering money?");
                        }
                        else if(amount == 100 || amount < 200)
                        {
                            Console.WriteLine("Hmmm, really that's all you have!");
                        }
                        else if  (amount >= 200 || amount < 300)
                        {
                            Console.WriteLine("Ok your balance is beginning too look nice!");
                        }
                        else if (amount >=  300 || amount < 400)
                        {
                            Console.WriteLine("OK HAVE YOU A SECRET STASH!!!!");
                        }
                        else if (amount >= 500)
                        {
                         Console.WriteLine("Your rich *******!");
                        }
                        
    
                    }
                    catch (FormatException fe)
                    {
                        Console.WriteLine("Please enter digits only!");
                    }
    
                } while (isValid == 0);
    
    It will exit the program when you press 0 and when you type a value greater than 1 or less than 100 it prints out the first line but if you type in any other value it won't print the of the console writeline statments, any ideas?
     
  15. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,545

    || means OR

    so this is always executed if its >= 1

    else if(amount >= 1 || amount < 100)
    {
    Console.WriteLine("You're very poor,are you sure you're not laundering money?");
    }

    Should be &&

    You should probably use a "Switch" statement instead

    You are also missing some value ranges, such as between 400 and 499
     
  16. bremen1874

    Sgarrista

    Joined: Oct 20, 2008

    Posts: 8,035

    To all of the people using double for monetary values, don't! Decimal is much more suitable.
     
  17. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,545

    If you are using C# version 7+ you can do something like this.

    switch (amount)
    {
    case 0:
    Console.WriteLine("Thank you and goodbye"); // you probably wont see this line as it will exit quickly.
    Environment.Exit(0);
    break;
    case int n when (n > 0 && n <= 100):
    Console.WriteLine("You're very poor,are you sure you're not laundering money?");
    break;
    }
     
  18. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Damn, looks like I need too learn my operators better :-| I was thinking switch might be better alright.

    Thanks for the help.
     
  19. thedude23

    Wise Guy

    Joined: Jul 11, 2009

    Posts: 1,298

    Really? I've never seen used much, I'll be doing the Microsoft exam for c# in two weeks and it was mentioned in that once but our tutor never mentioned it when he was teaching us variable types!
     
  20. chroniclard

    Sgarrista

    Joined: Apr 23, 2014

    Posts: 7,545

    Well, keep up the studies. ;)