Simple Class and Method Example

mypicturefaded 1 Tallied Votes 139 Views Share

I wanted to make something for beginners, to show and easy way of creating and using a class and it's methods.

So here is a customer class, and a Console application that takes advantage of the class. It is mainly set, and get methods, with one or two extra methods you can play around with. It also has its own toString method to display the contents of the Customer object.

You can plug this into VS, and it will compile and run correctly.

You can use this for whatever you need, play around with it and add constructors or methods, and see what you can learn.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.ReadLine();

            Console.WriteLine("Please enter your name: ");
            string name = Console.ReadLine();

            Console.WriteLine("Please enter your address: ");
            string address = Console.ReadLine();

            Console.WriteLine("Please enter your balance with the company: ");
            double balance = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("");

            //This is how you declare a new object so it meets the criteria.
            //Remember the constructor needed these 3 items to create the object
            Customer NewCustomer1 = new Customer(name, address, balance);
            NewCustomer1.toString();

            //Append the City, State, and Zipcode to what the user entered.
            NewCustomer1.AppendAddress("Atlanta, Georgia, 74504 ");
            NewCustomer1.toString();
            Console.ReadLine();

        }
    }

    public class Customer
    {
        string name, address;
        double balance;

        //This is the constructor. If you create a Customer object, it must include 
        //these three fields.
        public Customer(string inName, string inAddress, double inBalance)
        {
            name = inName;
            address = inAddress;
            balance = inBalance;
        }

        //Gets the name of the customer.
        public string getName()
        {
            return name;
        }

        //Gets the address of the customer.
        public string getAddress()
        {
            return address;
        }

        //Gets the balance of a customer.
        public double getBalance()
        {
            return balance;
        }

        //Set a new name to the Customer object.
        public void setName(string inName)
        {
            name = inName;
        }

        //Set a new address to the Customer object.
        public void setAddress(string inAddress)
        {
            address = inAddress;
        }

        //Set a new balance to the Customer object.
        public void setBalance(double inBalance)
        {
           balance = inBalance;
        }

        //This method adds a number to the total of the current balance.
        public void AddToBalance(double inBalance)
        {
            balance = inBalance + balance;
        }

        //This method adds more to the end of the address.
        public void AppendAddress(string inAddress)
        {
            address = address + " " + inAddress; 
        }

        //toString method to display the contents of the Customer object
        public void toString()
        {
            Console.WriteLine("Customer Name: " + name);
            Console.WriteLine("Customer Address: " + address);
            Console.WriteLine("Customer Balance: " + balance.ToString("$#.##"));
            Console.WriteLine("");
            Console.WriteLine("-------------");
            Console.WriteLine("");
        }
    }
}
ddanbe 2,724 Professional Procrastinator Featured Poster

Nice for beginners. I too am still learning BTW. Hope you know there exists something like "Properties"...

mypicturefaded -2 Light Poster

Yes, personally I am well aware of them! I really get I just wanted to show the use of methods, and creating them in your own class. get and set might have been a bad thing to use, but I thought it got the point accross about creating them. I suppose I thought if a beginner could understand the concept of a method, it would make the get and set properties a little easier to comprehend.

I am also constantly learning in C#. As I haven't been doing it very long either. :) Thanks for the feedback.

ddanbe 2,724 Professional Procrastinator Featured Poster

Carry on!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.