简单工厂模式

本文介绍了一个模拟工厂生产笔记本的系统,用户可以输入所需品牌,系统将输出相应笔记本信息。此外,还模拟了超市打折系统,用户可以选择不同的打折方式,系统会计算出打折后的价格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、模拟工厂生产笔记本

输入需要的品牌电脑,输出其所得到的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 模拟工厂生产笔记本
{
    class Program
    {
        static void Main(string[] args)
        {
            //写一个方法来模拟工厂生产笔记本
            Console.WriteLine("请问需要什么品牌的笔记本");
            string brand = Console.ReadLine();
            NoteBook nb = GetNoteBook(brand);
            nb.SayHello();
            Console.ReadKey();
        }
        public static NoteBook GetNoteBook(string brand)
        {
            NoteBook nb = null;
            switch(brand)
            {
                case "IBM":nb = new IBM();
                    break;
                case "Lenovo":nb = new Lenovo();
                    break;
                case "Acer":nb = new Acer();
                    break;
                case "Dell":nb = new Dell();
                    break;
            }
            return nb;
        }
    }
    public abstract class NoteBook//抽象父类
    {
        public abstract void SayHello();
    }
    public class IBM:NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是IBM笔记本");
        }
    }
    public class Lenovo : NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是联想笔记本");
        }
    }
    public class Acer : NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是鸿基笔记本");
        }
    }
    public class Dell:NoteBook
    {
        public override void SayHello()
        {
            Console.WriteLine("我是戴尔笔记本");
        }
    }
}

2、模拟超市打折

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 
{
    class Program
    {
        static void Main(string[] args)
        {
            //1--不打折  2--打N折   3--买M元减N元
            Console.WriteLine("请输入你要选择的打折方式");
            Console.WriteLine("1--不打折  2--打95折 3打85折  4买300减50 5买500送100");
            string input = Console.ReadLine();
            Console.WriteLine("您打折应付439.8元");
            DZFather dz = GetDZ(input);
            double realMoney = dz.GetMoney(439.8);
            Console.WriteLine("打折后应付{0}元",realMoney);
            Console.ReadKey();
        }

        public static DZFather GetDZ(string input)
        {
            DZFather dz = null;
            switch (input)
            {
                case "1": dz = new NormalDZ();
                    break;
                case "2": dz = new RateDZ(0.95);
                    break;
                case "3": dz = new RateDZ(0.85);
                    break;
                case "4": dz = new MNDZ(300, 50);
                    break;
                case "5": dz = new MNDZ(500, 100);
                    break;
            }
            return dz;
        }
    }

    public abstract class DZFather
    {
        public abstract double GetMoney(double totalMoeny);
    }
    public class NormalDZ : DZFather
    {

        /// <summary>
        /// 打折前多少钱 打折后就多少钱
        /// </summary>
        /// <param name="totalMoeny">打折前应付的价钱</param>
        /// <returns>打折后应付的价钱</returns>
        public override double GetMoney(double totalMoeny)
        {
            return totalMoeny;
        }
    }
    public class RateDZ : DZFather
    {
        public double Rate
        {
            get;
            set;
        }

        /// <summary>
        /// 传入折扣率
        /// </summary>
        /// <param name="rate">要打的折扣</param>
        public RateDZ(double rate)
        {
            this.Rate = rate;
        }
        public override double GetMoney(double totalMoeny)
        {
            return totalMoeny * this.Rate;
        }
    }
    public class MNDZ : DZFather
    {
        public double M
        {
            get;
            set;
        }

        public double N
        {
            get;
            set;
        }

        public MNDZ(double m, double n)
        {
            this.M = m;
            this.N = n;
        }

        //500 100 1300  2.6   260
        public override double GetMoney(double totalMoeny)
        {
            if (totalMoeny > this.M)
            {
                return totalMoeny - ((int)(totalMoeny / this.M)) * this.N;
            }
            else
            {
                return totalMoeny;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值