By GokiSoft.com| 08:34 12/10/2021|
C Sharp

[Video] Chương trình quản lý sách - Develop Book Project - Lập Trình C# - OOP trong C# - C Sharp - C2009G

Link Video Bai Giang


Chương trình quản lý sách - Develop Book Project - Lập Trình C# - OOP trong C# - C Sharp

#Program.cs


using System;

namespace BT1458
{
    class Test
    {
        static AptechBook[] bookList;

        static void Main(string[] args)
        {
            int choose;

            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch(choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        SortByYear();
                        Display();
                        break;
                    case 4:
                        SearchByName();
                        break;
                    case 5:
                        SearchByAuthor();
                        break;
                    case 6:
                        EditByName();
                        break;
                    case 7:
                        SaveFile();
                        break;
                    case 8:
                        ReadFile();
                        break;
                    case 9:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 9);
        }

        private static void EditByName()
        {
            if (bookList == null) return;

            Console.WriteLine("Nhap ten sach can sua: ");
            string bookName = Console.ReadLine();

            foreach (AptechBook book in bookList)
            {
                if (book.BooName == bookName)
                {
                    book.Input();
                    break;
                }
            }
        }

        private static void ReadFile()
        {
            string content = System.IO.File.ReadAllText(@"books.txt");
            if(content != null && content != "")
            {
                string[] lines = content.Split("\n");

                AptechBook[] dataList = new AptechBook[lines.Length];
                int index = 0;

                foreach(string line in lines)
                {
                    if (line == null || line.Trim() == "") continue;
                    AptechBook book = new AptechBook();
                    book.ParseFileLine(line);

                    dataList[index++] = book;
                }

                bookList = new AptechBook[index];
                for(int i=0;i<index;i++)
                {
                    bookList[i] = dataList[i];
                }
            }
        }

        private static void SaveFile()
        {
            if (bookList == null) return;

            //Luu tung quyen sach -> tren 1 dong.
            //Cau truc tung dong: cac thuoc tinh cach nhau boi dau ,
            string content = "";

            foreach(AptechBook book in bookList)
            {
                content += book.GetFileLine();
            }

            System.IO.File.WriteAllText(@"books.txt", content);
        }

        private static void SearchByAuthor()
        {
            if (bookList == null) return;

            Console.WriteLine("Nhap ten tac gia can tim kiem: ");
            string authorName = Console.ReadLine();

            foreach (AptechBook book in bookList)
            {
                if (book.BookAuthor == authorName)
                {
                    book.Display();
                }
            }
        }

        private static void SearchByName()
        {
            if (bookList == null) return;

            Console.WriteLine("Nhap ten sach can tim kiem: ");
            string bookName = Console.ReadLine();

            foreach(AptechBook book in bookList)
            {
                if(book.BooName == bookName)
                {
                    book.Display();
                }
            }
        }

        private static void SortByYear()
        {
            if (bookList == null) return;

            Array.Sort<Book>(bookList, (b1, b2) => {
               if(b1.YearPublishing < b2.YearPublishing)
                {
                    return 1;
                }
                return -1;
            });
        }

        private static void Display()
        {
            if (bookList == null) return;

            Console.WriteLine("Hien thi thong tin sach");
            foreach(AptechBook book in bookList)
            {
                if (book != null)
                {
                    book.Display();
                }
            }
        }

        private static void Input()
        {
            Console.WriteLine("Nhap so sach can tao N = ");
            int N = int.Parse(Console.ReadLine());

            bookList = new AptechBook[N];

            for(int i=0;i<N;i++)
            {
                AptechBook book = new AptechBook();
                book.Input();

                bookList[i] = book;
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap N quyen sach");
            Console.WriteLine("2. Hien thi");
            Console.WriteLine("3. Sap xep theo nam xb");
            Console.WriteLine("4. Tim theo ten sach");
            Console.WriteLine("5. Tim theo tac gia");
            Console.WriteLine("6. Sua sach theo ten nhap vao");
            Console.WriteLine("7. Luu File");
            Console.WriteLine("8. Doc File");
            Console.WriteLine("9. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Book.cs


using System;
namespace BT1458
{
    public class Book
    {
        public string BooName { get; set; }
        public string BookAuthor { get; set; }
        public string Producer { get; set; }
        public int YearPublishing { get; set; }
        public float Price { get; set; }

        public Book()
        {
        }

        public Book(string booName, string bookAuthor, string producer, int yearPublishing, float price)
        {
            BooName = booName;
            BookAuthor = bookAuthor;
            Producer = producer;
            YearPublishing = yearPublishing;
            Price = price;
        }

        public virtual void Input()
        {
            Console.WriteLine("Nhap ten sach: ");
            BooName = Console.ReadLine();

            Console.WriteLine("Nhap ten tac gia: ");
            BookAuthor = Console.ReadLine();

            Console.WriteLine("Nhap ten nha sx: ");
            Producer = Console.ReadLine();

            Console.WriteLine("Nhap nam xb: ");
            YearPublishing = int.Parse(Console.ReadLine());

            Console.WriteLine("Nhap gia ban: ");
            Price = float.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine(this);
        }

        public override string ToString()
        {
            return "Ten sach: " + BooName + ", tac gia: " + BookAuthor + ", nha sx: " +
                Producer + ", nam xb: " + YearPublishing + ", gia ban: " + Price;
        }

        public virtual string GetFileLine()
        {
            return BooName + "," + BookAuthor + "," +
                Producer + "," + YearPublishing + "," + Price;
        }
    }
}


#AptechBook.cs


using System;
namespace BT1458
{
    public class AptechBook : Book
    {
        public string Language { get; set; }
        public string Semester { get; set; }

        public AptechBook()
        {
        }

        public AptechBook(string language, string semester, string booName, string bookAuthor, string producer, int yearPublishing, float price) : base(booName, bookAuthor, producer, yearPublishing, price)
        {
            Language = language;
            Semester = semester;
        }

        public override void Input()
        {
            base.Input();

            Console.WriteLine("Nhap ngon ngu: ");
            Language = Console.ReadLine();

            Console.WriteLine("Nhap ky hoc: ");
            Semester = Console.ReadLine();
        }

        public override string ToString()
        {
            return base.ToString() + ", mon hoc: " + Language + ", ky hoc: " + Semester;
        }

        public override string GetFileLine()
        {
            return base.GetFileLine() + "," + Language + "," + Semester + "\n";
        }

        public void ParseFileLine(string line)
        {
            string[] elements = line.Split(",");

            BooName = elements[0];
            BookAuthor = elements[1];
            Producer = elements[2];
            YearPublishing = int.Parse(elements[3]);
            Price = float.Parse(elements[4]);
            Language = elements[5];
            Semester = elements[6];
        }
    }
}


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó