Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
As a beginner I always remain in a confusion that whether the application that I have designed is correct or not. I have searched in google for a couple of websites where I can check the design pattern of my program but didn't get anyone.

2 days ago I was asked by may teacher to create 'Blood Bank' project in c# console,and again the fear came again in my mind.I know that practice is the key to become a good programmer but still I just can't help myself.

So here is the Project of 'Blood Bank' that I have made so far...........


Just please tell me that whether the program that I have created is correct or not.
And the Member variable static in the DonorsData class is the correct way or not.

Your suggestions will be highly appreciable.


C#
 class DonorsData
    {
        string firstName, lastName, dateOfBirth, fathersName, mothersName, fathersBloodGroup, mothersBloodGroup, donorsBloodGroup,
            address, phoneNo;
    public static DonorsData contributer;
    DateTime yearOfBirth;
    int age;


        public string FirstName
        {
            get { return this.firstName; }
            set
            {
                if (string.IsNullOrEmpty(value))

                    throw new Exception("Please enter a valid first name");

                else
                    this.firstName = value;
            }
        }

        public string LastName
        {
            get { return this.lastName; }
            set
            {
                if (string.IsNullOrEmpty(value))

                    throw new Exception("Please enter a valid last name");

                else
                    this.lastName = value;
            }
        }

        public string DateOfBirth
        {
            get { return this.dateOfBirth; }
            set
            {
                if (string.IsNullOrEmpty(value))

                    throw new Exception("Please enter a valid date of birth");

                else
                    this.dateOfBirth = value;
            }
        }

        DateTime YearOfBirth
        {
            get { return DateTime.Parse(DateOfBirth); }
        }

       public int Age
        {
            get { return DateTime.Now.Year - YearOfBirth.Year; }
        }

        public string FathersName
        {
            get { return this.fathersName; }
            set
            {
                if (string.IsNullOrEmpty(value))

                    throw new Exception("Please enter a valid father's name");
                else
                    this.fathersName = value;
            }
        }

        public string MothersName
        {
            get { return this.mothersName; }
            set
            {
                if (string.IsNullOrEmpty(value))

                    throw new Exception("Please enter a valid mother's name");

                else
                    this.mothersName = value;
            }
        }

        public string Address
        {
            get { return this.address; }
            set
            {
                if (string.IsNullOrEmpty(value))

                    throw new Exception("Please enter a valid address");

                else
                    this.address = value;
            }
        }

        public string Phone
        {
            get { return this.phoneNo; }
            set
            {
                if (string.IsNullOrEmpty(value)||value.Length!=10)

                    throw new Exception("Please enter a valid phone number");

                else
                    this.phoneNo = value;

            }
        }

        public string FatherSBloodGroup
        {
            get { return this.fathersBloodGroup; }
            set
            {
                if (string.IsNullOrEmpty(value) || (value != "A+" && value != "A-" && value != "B+" && value != "B-" && value != "AB+"
                    && value != "AB-" && value != "O+" && value != "O-"))

                    throw new Exception("Please enter a valid blood group");

                else
                    this.fathersBloodGroup = value;
            }
        }

        public string MothersBloodGroup
        {
            get { return this.mothersBloodGroup; }
            set
            {
                if (string.IsNullOrEmpty(value) || (value != "A+" && value != "A-" && value != "B+" && value != "B-" && value != "AB+"
                    && value != "AB-" && value != "O+" && value != "O-"))

                    throw new Exception("Please enter a valid blood group");

                else
                    this.mothersBloodGroup = value;

            }
        }

        public string DonorsBloodGroup
        {
            get { return this.donorsBloodGroup; }
            set
            {
                if (string.IsNullOrEmpty(value) || (value != "A+" && value != "A-" && value != "B+" && value != "B-" && value != "AB+"
                    && value != "AB-" && value != "O+" && value != "O-"))

                    throw new Exception("Please enter a valid blood group");

                else
                    this.donorsBloodGroup = value;

            }
        }




   class RhFactor
    {

        public RhFactor()
        {
          Console.WriteLine(CheckingRhfactor());
        }

        string CheckingRhfactor()
        {
            string message = "";

            if ((DonorsData.contributer.FatherSBloodGroup == "A+") && (DonorsData.contributer.MothersBloodGroup == "B+")
                && (DonorsData.contributer.DonorsBloodGroup == "AB+"))
            {
                message = "Your Rh factor is B+";
            }
            else if ((DonorsData.contributer.FatherSBloodGroup == "A+") && (DonorsData.contributer.MothersBloodGroup == "A+")
            && (DonorsData.contributer.DonorsBloodGroup == "A+"))
            {
                message = "Your Rh factor is A+";
            }

            else if ((DonorsData.contributer.FatherSBloodGroup == "B+") && (DonorsData.contributer.MothersBloodGroup == "AB+")
            && (DonorsData.contributer.DonorsBloodGroup == "AB+"))
            {
                message = "Your Rh factor is B+";
            }

            else if ((DonorsData.contributer.FatherSBloodGroup == "A-") && (DonorsData.contributer.MothersBloodGroup == "A+")
            && (DonorsData.contributer.DonorsBloodGroup == "A-"))
            {
                message = "Your Rh factor is A-";
            }

            else if ((DonorsData.contributer.FatherSBloodGroup == "O+") && (DonorsData.contributer.MothersBloodGroup == "A+")
            && (DonorsData.contributer.DonorsBloodGroup == "O+"))
            {
                message = "Your Rh factor is A+";
            }

            else if ((DonorsData.contributer.FatherSBloodGroup == "AB+") && (DonorsData.contributer.MothersBloodGroup == "AB-")
            && (DonorsData.contributer.DonorsBloodGroup == "AB+"))
            {
                message = "Your Rh factor is B+";
            }

            return message;
        }
    }


class BloodBank
    {
        static Dictionary<int,> data;

        static BloodBank()
        {
            data = new Dictionary<int,>();
            DonorsData.contributer = new DonorsData();
        }
        public void AcceptingData()
        {
            Console.Write("Enter your first name:");
            DonorsData.contributer.FirstName = Console.ReadLine().ToUpper();

            Console.Write("Enter your last name:");
            DonorsData.contributer.LastName = Console.ReadLine().ToUpper();

            Console.Write("Enter your date of birth:");
            DonorsData.contributer.DateOfBirth = Console.ReadLine().ToUpper();

            Console.Write("Enter your father's name:");
            DonorsData.contributer.FathersName = Console.ReadLine().ToUpper();

            Console.Write("Enter your mother's name:");
            DonorsData.contributer.MothersName = Console.ReadLine().ToUpper();

            Console.Write("Enter your address:");
            DonorsData.contributer.Address = Console.ReadLine().ToUpper();

            Console.Write("Enter your phone no.:");
            DonorsData.contributer.Phone = Console.ReadLine().ToUpper();

            GetRhFactor();
        }

        void GetRhFactor()
        {
            Console.Write("Enter your father's blood group:");
            DonorsData.contributer.FatherSBloodGroup = Console.ReadLine().ToUpper();

            Console.Write("Enter your mother's blood group:");
            DonorsData.contributer.MothersBloodGroup = Console.ReadLine().ToUpper();

            Console.Write("Enter your donor's blood group:");
            DonorsData.contributer.DonorsBloodGroup = Console.ReadLine().ToUpper();

            RhFactor rhfactor = new RhFactor();
        }
    }

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

           BloodBank bank = new BloodBank();
           bank.AcceptingData();
       }
   }



Thank You
Posted
Updated 10-Aug-15 2:38am
v3

1 solution

The first thing I notice is that you have the same long conditional in at least three places: FatherSBloodGroup, MothersBloodGroup, and DonorsBloodGroup all conatin teh same chunk of code in the setter:
C#
if (string.IsNullOrEmpty(value) || (value != "A+" && value != "A-" && value != "B+" && value != "B-" && value != "AB+"
    && value != "AB-" && value != "O+" && value != "O-"))

    throw new Exception("Please enter a valid blood group");

else

I'd move that into a method which returns a bool to indicate "valid" or "invalid", and I'd also move the strings into an array rather than checking in code:
C#
private readonly string[] bloodGroups = { "A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-" };
private bool IsValidBloodGroup(string group)
    {
    foreach (string s in bloodGroups)
        {
        if (group == s) return true;
        }
    return false;
    }

If you are going to throw exceptions to indicate input errors, you should catch them - if I (as a user) entered all the info and miskeyed the final blood group, I'd be...annoyed...when the application crashed and lost me what I'd typed.

You should follow naming conventions for property bases: the MothersBloodGroup property should store into info in _MothersBloodGroup not mothersBloodGroup

You need to be more careful when typing names: FatherSBloodGroup shouldn;t have a capital "S" in it.

When you get info from the user, try to keep related info together: user details (all), then Fathers, then the Mothers. Makes it easier for the user to concentrate. And don't ask the user to "Enter your first name:" and then ask him to "Enter your donor's blood group:" - you will confuse his as to exactly what you mean.

It would be a good idea to allow users to not know a parent's blood group: I have no idea what my parents groups were - and can't ask since they are no longer with us.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900