微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 dotnet 框架在 C# 中分离业务和 UI 逻辑我该如何重构这个项目?

如何解决使用 dotnet 框架在 C# 中分离业务和 UI 逻辑我该如何重构这个项目?

我正在处理一个涉及面包店的 c# 项目。提示说我必须:

  1. 使用多个类文件
  2. 红绿重构工作流程
  3. 使用自动实现的属性
  4. 独立的用户界面和业务逻辑。

我了解自动实现的属性,但我在编写代码时有点迷茫。我真的需要一些重构方面的帮助。

https://github.com/FaisalRana/CSharp.Bakery

using System.Collections.Generic;

namespace Bakery.Models
{
  public class Program
  {
    public static void Main()
    {
      Console.ForegroundColor = ConsoleColor.Gray;
      Console.BackgroundColor = ConsoleColor.Black;
      string bakeryAscii = @"
                          ....
                      .'      `.
                    .' .-'``-._ `.
                    |  / -    - ` |
                    / |'<o>  <o> | \
                    (|    '`    |) 
                      \  -==-  /   
                       `.____.'    
                        |    |     
                  _ _.'`-.__.-'`._/_
                .'| |`-.  /\  .-'| |`.
              _.'   \ \  `'  `'  / /   `._
            { `.    | `-.____.-' |    .' }
            /`. `./ /   __  __   \ \.' .'\
            /   `.| |   /  \/  \   | |.'   \
          (    (  \ \  \      /  / /  )    )
            `.   \  Y|   `.  .'   |Y  /   .'
              \   \ ||_ _ _\/_ _ _|| /   /
              `.  \|'            `|/  .'
        _______/  _ >--------------< _  \______.##._
              ((((_(                )_))))   .##. |
            / ```` `--------------' ''''\   |  | |
            ( Welcome to Faisal's Bakery! \  |  |-'
            )                             ) `--'
            (          _        _.---.__.-'
            `-.___.--' `------'
        ";
      Console.ForegroundColor = ConsoleColor.DarkBlue;
      Console.WriteLine(bakeryAscii);
      Console.ForegroundColor = ConsoleColor.Gray;
      Console.WriteLine("Please [y] to place your order,[m] to view the menu and [n] to exit");
      string  continueAnswer = Console.ReadLine().ToLower();
      if (continueAnswer == "y") {
        Console.WriteLine("How many loaves of bread would you like to purchase today?");
        string stringNumOfBread = Console.ReadLine(); 

        int numOfBread = 0;
        bool checkBreadInput = Int32.TryParse(stringNumOfBread,out numOfBread);
          if (checkBreadInput == true) {
            if (numOfBread >= 0 ) {
              Bread bread = new Bread();
              int breadPrice = bread.CalcBread(numOfBread);
              Console.WriteLine("You have bought " + numOfBread + " loaves of bread for: $" + breadPrice);
              Console.WriteLine("Would you like to buy some Pastry's today? [y] or [n]");
              string pastryAnswer = Console.ReadLine().ToLower();
                if (pastryAnswer == "y") {
                  Console.WriteLine("Please enter the amount of pastry's you would like to buy:");
                  string stringNumOfPastry = Console.ReadLine(); 
                  int numOfPastry = 0;
                  bool checkPastryInput = Int32.TryParse(stringNumOfPastry,out numOfPastry);
                  if (checkPastryInput == true) {
                    if (numOfPastry >= 0) {
                      Pastry pastry = new Pastry();
                      int pastryPrice = pastry.CalcPastry(numOfPastry);
                      Console.WriteLine("You have bought " + numOfPastry + " pastry's");
                      Console.BackgroundColor = ConsoleColor.Black;
                      Console.ForegroundColor = ConsoleColor.Green;
                      int total = breadPrice + pastryPrice;
                      Console.WriteLine("Your total bill is $" + total);
                      Console.ForegroundColor = ConsoleColor.Gray;
                      Console.WriteLine("Goodbye");
                    } else ErrorNegativeNumber(); 
                  } else Error();
                } 
                else if (pastryAnswer == "n") {
                    Console.WriteLine("Thank you for Coming in,your total bill is $" + breadPrice);
                    Console.WriteLine("Goodbye");
                  } else Error();
              } 
              else {   
              ErrorNegativeNumber();      
            } 
          } else Error();
      } else if (continueAnswer == "m") {
        Menu();
    } else if (continueAnswer == "n") {
      Console.WriteLine("Goodbye");
    } else {
      Error();
    }
}

    public static void Menu() {
      Console.WriteLine("--------------------------------");
      Console.ForegroundColor = ConsoleColor.Yellow;
        string menu = @"Fresh Bread: $5 each (loaf)
        DelicIoUs Pastry: $2 each
        ** Special of the week **
        Bread: Buy 2,get 1 free!
        Pastry: 3 for $5!";
        Console.WriteLine(menu);
        Console.WriteLine("--------------------------------");
        Console.WriteLine("Press enter to continue");
        string menuAnswer = Console.ReadLine();
        Main();
    }
        public static void Error() {
          Console.ForegroundColor = ConsoleColor.Red;
          Console.WriteLine("Error,Invalid Input");
          Console.WriteLine("Press Enter to Restart");
          Console.ReadLine();
          Main();
    }

        public static void ErrorNegativeNumber() {
          Console.ForegroundColor = ConsoleColor.Red;
          Console.WriteLine("Error,Invalid Input. Negative Input detected.  You owe us bread!");
          Console.WriteLine("Press Enter to Restart");
          Console.ReadLine();
          Main();
    }
    }
}

这是 Program.CS 文件

我似乎无法弄清楚如何将所有这些内容放入类文件中,因为我认为任何与 Console.Read() 或 Console.Write() 相关的内容都必须放入 program.cs 文件中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。