Best way of passing data to a Method

Let us say that we have 2 Classes Expense1 and Expense 2
which class implementation is preferred over the other or which is considered closer to being object oriented.

I always thought that doing Exp2.Calculate(1.5M,2) is more readable than
exp1.Calculate() and using the properties of exp1 Class as the needed values for the calculate Method

this is my Expense1 Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


    public class Expense1
    {
        public decimal ExpenseValue { get; set; }
        public int NumberOfItems { get; set; }
        private decimal result;

        public decimal Result
        {
            get
            {
                return this.NumberOfItems * this.ExpenseValue;
            }
        }

        public void Calculate()
        {
            this.result = this.ExpenseValue * this.NumberOfItems;
        }

        public void expense1()
        {
            this.ExpenseValue = 0;
            this.NumberOfItems = 0;
        }
    }

this is my Expense2 Class

 class Expense2
    {
        public decimal Calculate(decimal expenseValue, int numberOfItems)
        {
            return expenseValue * numberOfItems;
        }
    }

This is My Implementation of both classes

class Program
    {
        static void Main(string[] args)
        {

            Expense1 exp1 = new Expense1();
            exp1.NumberOfItems = 2;
            exp1.ExpenseValue = 1.5M ;
            exp1.Calculate();
            Console.WriteLine("Expense1:" + exp1.Result.ToString());




            Expense2 exp2 = new Expense2();
            string result = string.Empty;
            result = exp2.Calculate(1.5M,2).ToString();
            Console.WriteLine("Expense2:" + result);
            Console.ReadKey();
        }
}


Source: oop

Leave a Reply