需求分析:现在有一个烧水器假设水温升高到100度为开水请用设计程序模拟整个烧水的过程,打开热水器,当水温达到95度时报警器开始发出警报,水温达到100度时,断开热水器电源。

 

我们使用常规的思维去分析这个需求,不就一个开关控制,一个警报吗?不假思索代码就好了,我们来看看下面的垃圾代码啊。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace heater
{
    class Program
    {
        static int tempture = 0;
        /// <summary>
        /// 开关
        /// </summary>
        class Switcher
        {
             public void SwitchOn()
            {
                Console.WriteLine("热水器被打开");
            }

            public void SwitchOff()
            {
                Console.WriteLine("热水器被关闭");
            }
        }
       
        /// <summary>
        /// 热水器
        /// </summary>
        class Heater
        {
            public  void BoidWater()
            {
                int i = 0;
                for (i = 0; i <= 100; i++)
                {
                    tempture = i;
                    Thread.Sleep(1000 / (i + 1));
                    if (tempture < 95 && tempture % 10 == 0)
                    {
                        Console.WriteLine("烧水中...");
                    }
                    else
                    {
                        MakeAlerm(tempture);
                        ShowTempture(tempture);
                    }
                }
            }
            public static void MakeAlerm(int tem)
            {
                if (tem > 95)
                {
                    Console.WriteLine("报警器:“嘀嘀嘀,水快烧开了.....”");
                }
            }
            private static void ShowTempture(int tempture)
            {

                if (tempture > 95)
                {
                    if (tempture == 100)
                    {
                        Console.WriteLine("\n水真的开了\n");
                    }
                    else
                    {
                        Console.WriteLine("水的温度是:{0}\n", tempture);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            Heater heater = new Heater();
            Switcher switcher = new Switcher();

            switcher.SwitchOn();
            heater.BoidWater();
            switcher.SwitchOff();

            Console.ReadLine();
        }
    }
}
View Code

相关文章: