5. Deleting data on the array


Easy Steps:

1.     Edit ContactsUI.cs  with the following code:
using System;
using MarioSoft.PhoneBook.BO;
using MarioSoft.PhoneBook.BAL;

namespace MarioSoft.PhoneBook.UI
{
    class ContactsUI
    {
        public ContactsUI() {
            CreateUI();
         
        }

        private void CreateUI()
        {
            int option;
            do
            {
                menu();
                option = ReadIntegerInput("Select Menu[1-4]:");
                switch (option)
                {
                    case 1:
                        Insert();
                    break;

                    case 2:
                       Edit();
                    break;

                    case 3:
                        Delete();
                    break;

                }

            } while ( option!= 4);
        }

        private void menu()
        {
            WriteLine("---Menu---");
            WriteLine("1. Add Contact");
            WriteLine("2. Edit Contact");
            WriteLine("3. Delete Contact");
            WriteLine("4. Quit");
        }


        private void DisplayContacts()
        {
            Contacts[] contacts = ContactsBAL.GetContacts();
            WriteLine("\n----------------------------------------------");
            WriteLine("ID\tName\tAddress\tContactNo");
           
            foreach (Contacts contact in contacts)
            {
                if (contact ==null) break;
                WriteLine(contact.Id + "\t" + contact.Name + "\t" + contact.Address + "\t" + contact.ContactNo);
            }
            WriteLine("----------------------------------------------\n");
        }

        private void Insert()
        {

            if (ContactsBAL.Insert(new Contacts
            {
                Id = ReadIntegerInput("ID:"),
                Name = ReadStringInput("Name:"),
                Address = ReadStringInput("Address:"),
                ContactNo = ReadStringInput("ContactNo:")
            }))
            {
                WriteLine("Save Successfully.");
                DisplayContacts();
            }
        }

        private void Edit()
        {
            int id = ReadIntegerInput("ID:");

            if (ContactsBAL.IsFound(id))
            {
                ContactsBAL.Edit(new Contacts
                {
                    Id = id,
                    Name = ReadStringInput("Name:"),
                    Address = ReadStringInput("Address:"),
                    ContactNo = ReadStringInput("ContactNo:")
                });

                WriteLine("Edited Successfully.");
                DisplayContacts();
            }
            else
            {
                WriteLine("Not Found.");
            }

        }

        private void Delete()
        {
              int id = ReadIntegerInput("ID:");

              if (ContactsBAL.IsFound(id))
              {
                  WriteLine("Deleted Successfully.");
                  ContactsBAL.Delete(id);
                  DisplayContacts();
              }
              else
              {
                  WriteLine("Not Found.");
              }
        }

        private void Write(string str) {
            Console.Write(str);
        }
        private void WriteLine(string str) {
            Console.WriteLine(str);
        }

        private int ReadIntegerInput(String str)
        {
            Write(str);
           return int.Parse(Console.ReadLine());
        }

        private string ReadStringInput( string str)
        {
            Write(str);
            return Console.ReadLine();
        }

    }
}

2.     On the ContactsBO.cs write the following code:
using System;

namespace MarioSoft.PhoneBook.BO
{
    class Contacts
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string address;

        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        private string contactNo;

        public string ContactNo
        {
            get { return contactNo; }
            set { contactNo = value; }
        }
    }
}

3.     On the ContactsBAL.cs write the following code:
using System;

using MarioSoft.PhoneBook.BO;
using MarioSoft.PhoneBook.DAL;


namespace MarioSoft.PhoneBook.BAL
{
    class ContactsBAL
    {
        public static bool Insert(Contacts contact)
        {
            ContactsDAL ContactDAL = new ContactsDAL();
            return ContactDAL.Insert(contact);
        }

        public static Contacts[] GetContacts()
        {
            ContactsDAL ContactDAL = new ContactsDAL();
            return ContactDAL.GetContacts();
        }

        public static void Edit(Contacts contact)
        {
            ContactsDAL ContactDAL = new ContactsDAL();
            ContactDAL.Edit(contact);
        }

        public static bool IsFound(int id)
        {
            ContactsDAL ContactDAL = new ContactsDAL();
            return ContactDAL.IsFound(id);
        }

        public static void Delete(int id)
        {
            ContactsDAL ContactDAL = new ContactsDAL();
            ContactDAL.Delete(id);
        }
    }
}

4.     On the ContactsDAL.cs write the following code:
using System;

using MarioSoft.PhoneBook.BO;

namespace MarioSoft.PhoneBook.DAL
{
   
    class ContactsDAL
    {
        static Contacts[] contacts = new Contacts[100];
        static int ndx;

        public bool Insert(Contacts contact)
        {
            bool inserted = false;

            if (contacts[ndx] == null)
            {
                contacts[ndx] = contact;
                ndx++;
                inserted = true;
            }

            return inserted;
        }

        public Contacts [] GetContacts()
        {
            return contacts;
        }

        public void Edit(Contacts contact)
        {
            for (int ndx = 0; ndx < contacts.Length; ndx++)
                {
                    if (contacts[ndx].Id == contact.Id)
                    {
                        contacts[ndx] = contact;
                        break;
                    }
                }

        }

        public void Delete(int id)
        {
            for (int ndx = 0; ndx < contacts.Length; ndx++)
            {
                if (contacts[ndx].Id == id)
                {
                    DeleteContact(ndx);
                    break;
                }
            }
        }

        private void DeleteContact(int i)
        {
            for (int ii = i; ii < contacts.Length; i++)
            {
                if (contacts[i] == null) break;
                contacts[i] = contacts[i + 1];
            }
            ndx--;
        }

        public bool IsFound(int id)
        {
            bool found = false;
            foreach (Contacts contact in contacts)
            {
                if (contact == null) break;
                if (contact.Id == id)
                {
                    found = true;
                    break;
                }
            }
            return found;   
        }

    }
}

5.     Put your code to the test, perfect!, nice jobJ

1 comment:

  1. Good Day, Sir!

    How do we implement it using the Window Application Output type? It is very possible right? Thanks for the help.

    ReplyDelete