Tuesday, November 17, 2015

Objects part 1

Here is our Customer class

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

namespace ObjectExample
{
    public class Customer
    {
        //private fields--a class level variable
        //the describes elements of the class
        private string name;
        private string phone;
        private string email;
        private int rewardPoints;

        //default constructor, no arguments
        //initializes everything to lowest possible value
        public Customer()
        {
        }

        //overloaded constructor takes parameters
        //the compiler chooses which constructor to use
        //based on the number and types of arguments
        //but only one constructor is used at a time
        public Customer(string name, string phone,
            string email, int points)
        {
            Name = name;
            Phone = phone;
            Email = email;
            RewardPoints = points;
        }

        //public properties--properties make the
        //private fields accessible
        //set allows the user to change the value of
        //a field. Get allows the use to see the value
        //of a field
        public string Name
        {
            set { name = value; }
            get { return name; }
        }

        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        public string Email
        {
            get
            {
                return email;
            }

            set
            {
                email = value;
            }
        }

        public int RewardPoints
        {
            get
            {
                return rewardPoints;
            }

            set
            {
                rewardPoints = value;
            }
        }

        //public metods
        public void AddRewards(decimal amount)
        {
            RewardPoints += (int)(amount * .1M);
        }

        public void SubtractPoints(int points)
        {
            RewardPoints =- points;
        }


    }
}

Here is the Program class were we instantiate and use the Customer class

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

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

            //this instantiates an object of the customer type
            //and then uses the propeties set to assign the values
            Customer customer = new Customer();
            customer.Name = "John Lennon";
            customer.Phone = "(212)555-3132";
            customer.Email = "john@thebeatles.org";
            customer.RewardPoints = 20;

            //this instantiates another object of the customer type
            //and uses the overloaded constructor to pass in the values
            Customer customer2 = new Customer("Ringo Starr",
                "(212)555-1234", "ringo@beatles.com", 10);

            //the output uses the get element of the properties
            Console.WriteLine(customer.Name + "\n " +
                customer.Phone + "\n"
                + customer.Email + "   "
                + customer.RewardPoints);

            customer.AddRewards(60.89M);
            Console.WriteLine("*****************");

            Console.WriteLine(customer2.Name + "\n " +
               customer2.Phone + "\n"
               + customer2.Email + "   "
               + customer2.RewardPoints);

            Console.ReadKey();
         
        }
    }
}

No comments:

Post a Comment