By GokiSoft.com| 14:36 28/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 - C2010G


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

#Book.cs


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

        public Book()
        {
        }

        public Book(string bookName, string bookAuthor, string producer, string yearPublishing, int price)
        {
            BookName = bookName;
            BookAuthor = bookAuthor;
            Producer = producer;
            YearPublishing = yearPublishing;
            Price = price;
        }

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

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

            Console.WriteLine("Nhap NSX: ");
            Producer = Console.ReadLine();

            Console.WriteLine("Nhap nam xuat ban: ");
            YearPublishing = Console.ReadLine();

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

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

        public override string ToString()
        {
            return "Ten sach: " + BookName + ", ten tac gia: " + BookAuthor +
                ", NSX: " + Producer + ", nam xuat ban: " + YearPublishing +
                ", gia ban: " + Price;
        }

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


#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 bookName, string bookAuthor,
            string producer, string yearPublishing,
            int price):base(bookName, bookAuthor, producer, yearPublishing, price)
        {
            Language = language;
            Semester = semester;
        }

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

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

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

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

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

        public void ParseFileLine(string line)
        {
            string[] elements = line.Split(",");
            Language = elements[0];
            Semester = elements[1];
            BookName = elements[2];
            BookAuthor = elements[3];
            Producer = elements[4];
            YearPublishing = elements[5];
            Price = int.Parse(elements[6]);
        }
    }
}


#Program.cs


using System;
using System.IO;

namespace BT1458
{
    class Program
    {
        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:
                        SortByYearPublishing();
                        Display();
                        break;
                    case 4:
                        SearchByName();
                        break;
                    case 5:
                        SearchByAuthor();
                        break;
                    case 6:
                        SaveFile();
                        break;
                    case 7:
                        ReadFile();
                        Display();
                        break;
                    case 8:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 8);
        }

        private static void ReadFile()
        {
            //B1. Doc noi dung text -> content (danh sach lines -> object (Aptech Book)
            string content = File.ReadAllText(@"books.txt");
            if (content == null || content == "") return;

            content = content.Trim();

            //B2. content -> lines -> object
            string[] lines = content.Split("\n");
            bookList = new AptechBook[lines.Length];

            for(int i=0;i<lines.Length;i++)
            {
                if (lines[i] == "") continue;

                AptechBook aptechBook = new AptechBook();
                aptechBook.ParseFileLine(lines[i]);

                bookList[i] = aptechBook;
            }
        }

        private static void SaveFile()
        {
            //B1: Giai thuat -> cach lam & luu file -> Y tuong: object (Aptech Book) -> 1 line -> save File
            //B2: bookList -> content (danh sach line -> object (Aptech Book)
            string content = "";
            for(int i=0;i<bookList.Length;i++)
            {
                content += bookList[i].GetFileLine();
            }

            //B3. Luu content -> File
            File.WriteAllText(@"books.txt", content);
            Console.WriteLine("Luu file thanh cong!!!");
        }

        private static void SearchByAuthor()
        {
            Console.WriteLine("Nhap ten tac gia can tim: ");
            string authorName = Console.ReadLine();

            for (int i = 0; i < bookList.Length; i++)
            {
                if (bookList[i].BookAuthor == authorName)
                {
                    bookList[i].Display();
                }
            }
        }

        private static void SearchByName()
        {
            Console.WriteLine("Nhap ten sach can tim: ");
            string name = Console.ReadLine();

            for(int i=0;i<bookList.Length;i++)
            {
                if(bookList[i].BookName == name)
                {
                    bookList[i].Display();
                }
            }
        }

        private static void SortByYearPublishing()
        {
            Array.Sort<AptechBook>(bookList, (b1, b2) => {
                return b1.YearPublishing.CompareTo(b2.YearPublishing);
            });
        }

        private static void Display()
        {
            Console.WriteLine("Thong tin sach: ");
            for(int i=0;i<bookList.Length;i++)
            {
                bookList[i].Display();
            }
        }

        private static void Input()
        {
            Console.WriteLine("Nhap so sach can them N = ");
            int N = int.Parse(Console.ReadLine());
            bookList = new AptechBook[N];

            for(int i=0;i<N;i++)
            {
                Console.WriteLine("Nhap thong tin sach thu {0}", i + 1);
                AptechBook aptechBook = new AptechBook();
                aptechBook.Input();

                bookList[i] = aptechBook;
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap N sach");
            Console.WriteLine("2. Hien thi");
            Console.WriteLine("3. Sap xep theo nam xuat ban");
            Console.WriteLine("4. Tim kiem theo ten sach");
            Console.WriteLine("5. Tim kiem theo tac gia");
            Console.WriteLine("6. Luu books.txt");
            Console.WriteLine("7. Doc books.txt");
            Console.WriteLine("8. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}




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 đó