Answer to Question #189885 in C# for santhosh asrith

Question #189885

Develop a Console Application to maintain the Contact details of Guests staying at Guest House. Use Layering concept. The class Diagram (The faculty will share the copy of the class Diagram)


Validations :

• All fields compulsory

• guestID should be 3 digits long

• guestName can accept alphabets only. It should start with Capital Alphabet and should have minimum 3 characters

• contactNumber should have 10 digits exctly. It should start with 6 or 7 or 8 or 9.


1
Expert's answer
2021-05-07T10:50:24-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace C_SHARP
{
    class Guest {
        //guestID should be 3 digits long
        private int guestID;


        public int GuestID
        {
            get { return guestID; }
            set { guestID = value; }
        }
        //guestName can accept alphabets only. It should start with Capital Alphabet and should have minimum 3 characters
        private string guestName;


        public string GuestName
        {
            get { return guestName; }
            set { guestName = value; }
        }
        //contactNumber should have 10 digits exctly. It should start with 6 or 7 or 8 or 9.
        private long contactNumber;


        public long ContactNumber
        {
            get { return contactNumber; }
            set { contactNumber = value; }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public Guest() { }


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="guestID"></param>
        /// <param name="guestName"></param>
        /// <param name="contactNumber"></param>
        public Guest(int guestID, string guestName, long contactNumber)
        {
            this.guestID = guestID;
            this.guestName = guestName;
            this.contactNumber = contactNumber;
        }
        /// <summary>
        /// display info
        /// </summary>
        public void display() {
            Console.WriteLine("Gest ID: {0}", this.guestID);
            Console.WriteLine("Gest name: {0}", this.guestName);
            Console.WriteLine("Gest contact number: {0}\n", this.contactNumber);
        }
    }


    class GuestDB {
        private List<Guest> guests;
        public GuestDB() {
            this.guests = new List<Guest>();
        }
        /// <summary>
        /// Adds a new guest
        /// </summary>
        /// <param name="guest"></param>
        public void AddGuest(Guest guest)
        {
            this.guests.Add(guest);
        }
        /// <summary>
        /// Display all guests
        /// </summary>
        public void displayAllGuests() {
            for (int i = 0; i < this.guests.Count; i++) {
                this.guests[i].display();   
            }
        }


    }


    class UI {


        private GuestDB guestDB;
        public UI() {
            this.guestDB = new GuestDB();
        }


        public void run() {
            for (int g = 0; g < 5; g++)
            {
                Guest guest = new Guest();
                int ID = 0;
                long contactNumber = 0;
                string inputValue = "";
                while (inputValue == "")
                {
                    Console.Write("Enter a guest ID: ");
                    inputValue = Console.ReadLine();
                    if (inputValue.Length != 3)
                    {
                        inputValue = "";
                        Console.WriteLine("\nGuest ID should be 3 digits long.\n");


                    }
                    else
                    {


                        if (!int.TryParse(inputValue, out ID))
                        {
                            Console.WriteLine("\nGuest ID should be 3 digits long.\n");
                        }
                    }
                }
                guest.GuestID = ID;
                // can accept alphabets only. It should start with Capital Alphabet and should have minimum 3 characters
                inputValue = "";
                while (inputValue == "")
                {
                    Console.Write("Enter a guest name: ");
                    inputValue = Console.ReadLine();
                    if (inputValue.Length < 3)
                    {
                        inputValue = "";
                        Console.WriteLine("\nGuest name should have minimum 3 characters.\n");


                    }
                    else
                    {
                        if (!char.IsUpper(inputValue[0]))
                        {
                            inputValue = "";
                            Console.WriteLine("\nGuest name should start with Capital Alphabet.\n");
                        }
                        else
                        {
                            bool isLetter = true;
                            for (int i = 0; i < inputValue.Length; i++)
                            {
                                if (!char.IsLetter(inputValue[i]))
                                {
                                    isLetter = false;
                                    break;
                                }
                            }
                            if (!isLetter)
                            {
                                inputValue = "";
                                Console.WriteLine("\nGuest name should be a alphabet value.\n");
                            }
                        }
                    }
                }
                guest.GuestName = inputValue;
                //should have 10 digits exctly. It should start with 6 or 7 or 8 or 9.
                inputValue = "";
                while (inputValue == "")
                {
                    Console.Write("Enter a guest contact number: ");
                    inputValue = Console.ReadLine();
                    if (inputValue.Length != 10)
                    {
                        inputValue = "";
                        Console.WriteLine("\nGuest contact number should have 10 digits exctly.\n");


                    }
                    else
                    {
                        if (inputValue[0] != '6' && inputValue[0] != '7' && inputValue[0] != '8' && inputValue[0] != '9')
                        {
                            inputValue = "";
                            Console.WriteLine("\nGuest contact number should start with 6 or 7 or 8 or 9.\n");
                        }
                        else
                        {
                            if (!long.TryParse(inputValue, out contactNumber))
                            {
                                inputValue = "";
                                Console.WriteLine("\nGuest ID should be digits only.\n");
                            }
                        }
                    }
                }
                guest.ContactNumber = contactNumber;
                guestDB.AddGuest(guest);
            }
            guestDB.displayAllGuests();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            UI UI = new UI();
            UI.run();
            Console.ReadKey();
        }


    }
}



Example:



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS