By GokiSoft.com| 17:28 29/10/2021|
C Sharp

Bài tập - Quản lý rạp chiều phim C# - Lập trình C#

Rạp chiếu phim quốc gia hiện nay đang cần thiết kế 1 chương trình nhằm kiểm soát toàn bộ hoạt động tại rạp bào gồm

- Quầy bán vé (ticket counter) => gồm các trạng thái (trống, khách hàng ít, khách hàng vừa phải và đang rất đông, thời điểm)

- Bãi đỗ xe (Parking) => gồm các trạng thái (trống, ít, vừa phải, full, thời điểm)

- Phòng xem phim => gồm các thuộc tính số ghế, số ghế đang ngồi, thời điểm

- Quầy bán đồ ăn => gồm các trạng thái (trống, khách hàng ít, khách hàng vừa phải và đang rất đông, thời điểm)

Viết chương trình quản lý thực hiện các yêu cầu sau

- Tạo 1 giao diện IStatus => có 1 phương thức onStatus => làm nhiệm vụ hiển thị trạng thái của từng địa điểm

- Thiết kế các lớp đối tượng trên => kế thừa từ IStatus

- Thiết kế các hàm nhập, hiển thị cho từng đối tượng trên

Trong class Main

- Nhập thông tin gồm 3 quầy bán vé, 2 quầy bán đồ ăn, 2 bãi đỗ xe và 5 phòng chiếu phim

- Tạo 1 phương thức followStatus(List<IStatus> statusList) thực hiện hiển thị trạng thái của tất cả các địa điểm => yêu cầu sử dụng hàm này cho >>> 3 quầy bán vé, 2 quầy bán đồ ăn, 2 bãi đỗ xe và 5 phòng chiếu phim >> trong chương trình trên

- Tạo 1 delegate đặt tên: void ShowStatus() -> Khai báo đối tượng delegate quản lý tất cả các hàm onStatus của các đối tượng trên -> Thực hiện test và delegate đó.

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

5

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

Đào Mạnh Dũng [C2010L]
Đào Mạnh Dũng

2021-10-26 15:16:10


#Cinema.cs


using System;

namespace _2509
{
    internal class Cinema : IStatus
    {
        private int seats;
        private int seatsSitting;
        private DateTime time;

        public Cinema()
        {
        }

        public Cinema(int seats, int seatsSitting, DateTime time)
        {
            this.seats = seats;
            this.seatsSitting = seatsSitting;
            this.time = time;
        }

        public void Input()
        {
            Console.WriteLine("nhap số ghế ngồi");
            this.seats = lib.Utility.ReadInt();
            Console.WriteLine("nhap số ghế đang ngồi");
            this.seatsSitting = lib.Utility.ReadInt();
            Console.WriteLine("nhap số thời điểm");
            this.time = lib.Utility.ReadDate();
        }

        public void onStatus()
        {
            Console.WriteLine(seatsSitting + " / " + seats);
        }
    }
}


#FoodStall.cs


using System;

namespace _2509
{
    internal class FoodStall : IStatus
    {
        public Status status;
        private DateTime time;

        public FoodStall()
        {
        }

        public FoodStall(Status status, DateTime time)
        {
            this.status = status;
            this.time = time;
        }

        public void Input()
        {
            Console.WriteLine("nhap status");
            this.status = _status.ReadStatus();
            Console.WriteLine("nhap số thời điểm");
            this.time = lib.Utility.ReadDate();
        }

        public void onStatus()
        {
            Console.WriteLine(status);
        }
    }
}


#IStatus.cs


namespace _2509
{
    internal interface IStatus
    {
        public void onStatus();
    }
}


#Parking.cs


using System;

namespace _2509
{
    internal class Parking : IStatus
    {
        public Status status;
        private DateTime time;

        public Parking()
        {
        }

        public Parking(Status status, DateTime time)
        {
            this.status = status;
            this.time = time;
        }

        public void Input()
        {
            Console.WriteLine("nhap status");
            this.status = _status.ReadStatus();
            Console.WriteLine("nhap số thời điểm");
            this.time = lib.Utility.ReadDate();
        }

        public void onStatus()
        {
            Console.WriteLine(status);
        }
    }
}


#Program.cs


using System.Collections.Generic;

namespace _2509
{
    internal class Program
    {
        private delegate void ShowStatus();

        private static void Main(string[] args)
        {
            //- Nhập thông tin gồm 3 quầy bán vé, 2 quầy bán đồ ăn, 2 bãi đỗ xe và 5 phòng chiếu phim
            List<IStatus> statusList = new List<IStatus>();

            TicketCounter[] ticketCounter = new TicketCounter[3];
            FoodStall[] foodStall = new FoodStall[2];
            Parking[] parking = new Parking[2];
            Cinema[] cinema = new Cinema[5];
            for (int i = 0; i < 3; i++)
            {
                ticketCounter[i] = new TicketCounter();
                ticketCounter[i].Input();
                statusList.Add(ticketCounter[i]);
            }
            for (int i = 0; i < 2; i++)
            {
                foodStall[i] = new FoodStall();
                foodStall[i].Input();
                statusList.Add(foodStall[i]);
            }
            for (int i = 0; i < 2; i++)
            {
                parking[i] = new Parking();
                parking[i].Input();
                statusList.Add(parking[i]);
            }
            for (int i = 0; i < 5; i++)
            {
                cinema[i] = new Cinema();
                cinema[i].Input();
                statusList.Add(cinema[i]);
            }
            ShowStatus showStatus = null;
            foreach (var item in statusList)
            {
                showStatus += item.onStatus;
            }
            showStatus();
        }
    }
}


#Status.cs


namespace _2509
{
    public enum Status
    {
        empty, few, moderate, full
    }

    public class _status
    {
        public static Status ReadStatus()
        {
            while (true)
            {
                for (int i = 0; i < 4; i++)
                {
                    System.Console.WriteLine("{0}, {1}", i + 1, (Status)i);
                }
                System.Console.WriteLine("chon trang thai ");
                int chosse = lib.Utility.ReadInt() - 1;
                if (chosse > 3 || chosse < 0)
                {
                    System.Console.WriteLine("nhap sai roi nhap lai");
                    continue;
                }
                return (Status)chosse;
            }
        }
    }
}


#TicketCounter.cs


using System;

namespace _2509
{
    internal class TicketCounter : IStatus
    {
        public Status status;
        private DateTime time;

        public TicketCounter()
        {
        }

        public TicketCounter(Status status, DateTime time)
        {
            this.status = status;
            this.time = time;
        }

        public void Input()
        {
            Console.WriteLine("nhap status");
            this.status = _status.ReadStatus();
            Console.WriteLine("nhap số thời điểm");
            this.time = lib.Utility.ReadDate();
        }

        public void onStatus()
        {
            Console.WriteLine(status);
        }
    }
}


#Utility.cs


using Newtonsoft.Json;
using System;
using System.IO;

namespace lib
{
    internal delegate void SwitchCase();

    internal class Utility
    {
        public static void Menu(SwitchCase[] menu)
        {
            int choose;
            while (true)
            {
                menu[0]();
                choose = Utility.ReadInt();
                if (choose <= menu.Length)
                {
                    menu[choose]();
                }
                else Console.WriteLine("Dau vao khong hop le! ");
            }
        }

        public static void Exist()
        {
            Environment.Exit(0);
        }

        public static int ReadInt()
        {
            int integer;
            while (true)
            {
                try
                {
                    integer = int.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return integer;
        }

        public static float ReadFloat()
        {
            float _float;
            while (true)
            {
                try
                {
                    _float = float.Parse(System.Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return _float;
        }

        public static DateTime ReadDate()
        {
            DateTime date;
            while (true)
            {
                try
                {
                    date = ConvertDateTime(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return date;
        }

        public static string ConvertDateTime(DateTime myDate)
        {
            return myDate.ToString("dd-MM-yyyy");
        }

        public static DateTime ConvertDateTime(string str)
        {
            return DateTime.ParseExact(str, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }

        public static T ReadfileJson<T>(string path)
        {
            return JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(path));
        }

        public static void SavefileJson(object obj, string path)
        {
            System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(obj));
            Console.WriteLine("save file json is success!");
        }

        public static void SaveObject(object serialize, string path)
        {
            using (Stream stream = File.Open(path, FileMode.Create))
            {
                new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(stream, serialize);
                Console.WriteLine("save file object is success!");
            }
        }

        public static object ReadObject(string path)
        {
            using (Stream stream = File.Open(path, FileMode.Open))
            {
                return new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(stream);
            }
        }

        public static void directoryCreate(string path)
        {
            string directoryPath = path;

            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            if (!directory.Exists)
            {
                directory.Create();
            }

            Console.WriteLine("Create directory {0} is success!", directory.Name);
        }
    }
}