3月6日 储物柜练习讲解

Profile Picture
- Published on Mar 6, 2020🌏 Public

一. 储物柜 1. 储物格的类有存放和取件两个方法,存放的时候,开门提示输入存放的东西;取件的时候,提示取出了xxx 2. 启动时,输入储物柜的格子总数,进行储物柜的初始化。储物柜的主界面

1.存
2.取

存取时,都要输入储物格,然后再调用对应储物格的存取方法。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

namespace Test4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("系统启动中...");
            Thread.Sleep(1000);
            Console.WriteLine("储物格初始化中...");
            Thread.Sleep(1000);
            Console.WriteLine("请输入储物格个数...");

            Sark sark = new Sark(Convert.ToInt32(Console.ReadLine()));

            sark.ShowMenu();
        }
      
    }

    /// <summary>
    ///     储物柜
    /// </summary>
    class Sark
    {
        private List<Box> Boxes=new List<Box>();

        public Sark(int boxCount) {
            for (int i = 0; i < boxCount; i++)
            {
                //添加一个格子实例到列表中
                Boxes.Add(new Box());
            }
        }

        public void ShowMenu() {
            Console.WriteLine("1.存\n2.取");
            var key=Console.ReadKey();
            switch (key.KeyChar)
            {
                case '1':
                    Store();
                     break;
                case '2':
                    Take();
                    break;
                default:
                    Console.WriteLine("输入有误");
                    break;
            }
            ShowMenu();
        }
        /// <summary>
        /// 快递柜存储物品
        /// </summary>
        public void Store() {
            Console.WriteLine("请输入要使用第几个储物格,1~{0}之间的数值。", Boxes.Count);
            var num = Convert.ToInt32(Console.ReadLine());
            var box=Boxes[num - 1];
            if (box.IsEmpty())
            {
                Console.WriteLine("请存放物品");
                box.Store(Console.ReadLine());
            }
            else
            {
                Console.WriteLine("当前格子已被使用,请选择其他格子");
                Store();
            }
        }

        /// <summary>
        /// 快递柜取物品
        /// </summary>
        public void Take()
        {
            Console.WriteLine("请输入要取走第几个储物格的物品,1~{0}之间的数值。", Boxes.Count);
            var num = Convert.ToInt32(Console.ReadLine());
            var box = Boxes[num - 1];
            if (box.IsEmpty())
            {
                Console.WriteLine("当前格子为空");
                Take();
            }
            else
            {
                box.Take();
            }
        }
    }

    /// <summary>
    ///     储物格
    /// </summary>
    class Box
    {
        /// <summary>
        ///     存储的东西
        /// </summary>
        private String Thing { get; set; }


        /// <summary>
        ///     存入物品
        /// </summary>
        /// <param name="thing">要存放的物品</param>
        public void Store(string thing) {
            Thing = thing;
        }

        /// <summary>
        ///     获取格子是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty() {
            return Thing == null;
        }

        /// <summary>
        ///     取出物品
        /// </summary>
        public void Take() {
            if (IsEmpty())
            {
                Console.Write("当前储物格为空");
            }
            else
            {
                Console.WriteLine("取出了" + Thing);
                Thing = null;
            }
        }
    }


}

二. 储物柜升级版 有两种储物格,分别为大型和小型。做成大储物格和小储物格两种类型 1. 储物格的类有存放和取件两个方法,存放的时候,开门提示输入存放的东西;取件的时候,提示取出了xxx 2. 启动时,分别输入大小储物柜的格子总数,进行储物柜的初始化。储物柜的主界面

1.存
2.取

点存的时候

1.大格
2.小格

根据他的选择为他提供一个储物格并告知储物格号码,如果已经没有可用储物格要提示。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

namespace Test4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("系统启动中...");
            Thread.Sleep(1000);
            Console.WriteLine("储物格初始化中...");
            Thread.Sleep(1000);
            Console.WriteLine("请分别输入小储物格和大储物格个数...");

            Sark sark = new Sark(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()));

            sark.ShowMenu();
        }
      
    }

    /// <summary>
    ///     储物柜
    /// </summary>
    class Sark
    {
        private ArrayList Boxes=new ArrayList();

        public Sark(int smallBoxCount,int bigBoxCount) {
            for (int i = 0; i < smallBoxCount; i++)
            {
                //添加一个小格子实例到列表中
                Boxes.Add(new SmallBox());
            }
            for (int i = 0; i < bigBoxCount; i++)
            {
                //添加一个大格子实例到列表中
                Boxes.Add(new BigBox());
            }
        }

        public void ShowMenu() {
            Console.WriteLine("1.存\n2.取");
            var key=Console.ReadKey();
            switch (key.KeyChar)
            {
                case '1':
                    Store();
                     break;
                case '2':
                    Take();
                    break;
                default:
                    Console.WriteLine("输入有误");
                    break;
            }
            ShowMenu();
        }
        /// <summary>
        /// 快递柜存储物品
        /// </summary>
        public void Store() {
            Console.WriteLine("1.大储物格\n2.小储物格");
            var key = Console.ReadKey();
            switch (key.KeyChar)
            {
                case '1':
                    //寻找一个空的大格子
                    BigBox findedBox=null;
                    for (int i = 0; i < Boxes.Count; i++)
                    {
                        if (Boxes[i].GetType().FullName == "Test4.BigBox")
                        {
                            BigBox box = (BigBox) Boxes[i];
                            if (box.IsEmpty())
                            {
                                Console.WriteLine("当前正在使用第" + (i + 1) + "个储物格");
                                findedBox = box;
                                break;
                            }
                        }
                    }

                    if (findedBox == null)
                    {
                        Console.WriteLine("没有找到空格子。");
                        ShowMenu();
                    }
                    else
                    {
                        Console.WriteLine("请存放物品");
                        findedBox.Store(Console.ReadLine());
                    }
                 
                    break;
                case '2':
                    //寻找一个空的小格子
                    SmallBox findedSmallBox = null;
                    for (int i = 0; i < Boxes.Count; i++)
                    {
                        if (Boxes[i].GetType().FullName == "Test4.SmallBox")
                        {
                            SmallBox box = (SmallBox)Boxes[i];
                            if (box.IsEmpty())
                            {
                                Console.WriteLine("当前正在使用第" + (i + 1) + "个储物格");
                                findedSmallBox = box;
                                break;
                            }
                        }
                    }

                    if (findedSmallBox == null)
                    {
                        Console.WriteLine("没有找到空格子。");
                        ShowMenu();
                    }
                    else
                    {
                        Console.WriteLine("请存放物品");
                        findedSmallBox.Store(Console.ReadLine());
                    }
                    break;
                default:
                    Console.WriteLine("输入有误");
                    Store();
                    break;
            }
           
        }

        /// <summary>
        /// 快递柜取物品
        /// </summary>
        public void Take()
        {
            Console.WriteLine("请输入要取走第几个储物格的物品,1~{0}之间的数值。", Boxes.Count);
            var num = Convert.ToInt32(Console.ReadLine());
            var box = Boxes[num - 1];
            if (box.GetType().FullName == "Test4.BigBox")
            {
                var bigbox = (BigBox)box;
                if (bigbox.IsEmpty())
                {
                    Console.WriteLine("当前格子为空");
                    Take();
                }
                else
                {
                    bigbox.Take();
                }
            }
            else
            {
                var sb = (SmallBox)box;
                if (sb.IsEmpty())
                {
                    Console.WriteLine("当前格子为空");
                    Take();
                }
                else
                {
                    sb.Take();
                }
            }
        }
    }

    /// <summary>
    ///     储物大格
    /// </summary>
    class BigBox
    {
        /// <summary>
        ///     存储的东西
        /// </summary>
        private String Thing { get; set; }


        /// <summary>
        ///     存入物品
        /// </summary>
        /// <param name="thing">要存放的物品</param>
        public void Store(string thing) {
            Thing = thing;
        }

        /// <summary>
        ///     获取格子是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty() {
            return Thing == null;
        }

        /// <summary>
        ///     取出物品
        /// </summary>
        public void Take() {
            if (IsEmpty())
            {
                Console.Write("当前储物格为空");
            }
            else
            {
                Console.WriteLine("取出了" + Thing);
                Thing = null;
            }
        }
    }

    /// <summary>
    ///     储物小格
    /// </summary>
    class SmallBox
    {
        /// <summary>
        ///     存储的东西
        /// </summary>
        private String Thing { get; set; }


        /// <summary>
        ///     存入物品
        /// </summary>
        /// <param name="thing">要存放的物品</param>
        public void Store(string thing)
        {
            Thing = thing;
        }

        /// <summary>
        ///     获取格子是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return Thing == null;
        }

        /// <summary>
        ///     取出物品
        /// </summary>
        public void Take()
        {
            if (IsEmpty())
            {
                Console.Write("当前储物格为空");
            }
            else
            {
                Console.WriteLine("取出了" + Thing);
                Thing = null;
            }
        }
    }


}