head firstC#宴会消费源码

081948_cy4T_2369789.png082006_b0BQ_2369789.png

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

namespace Budget
{
    public class Party
    {
        const int CostOfFoodPerson = 25;
        private bool fancyDecorations;
        public decimal CostOfDecorations = 0;
        private int numberOfPeople;
     public Party(int numberOfPeople,bool fancyDecorations){
         this.fancyDecorations = fancyDecorations;
         this.NumberOfPeople = numberOfPeople;

        }
     public virtual int NumberOfPeople {
         set {
             numberOfPeople = value;
             
         }
         get {
             return numberOfPeople;
         }
     }

     public void CalculateCostOfDecorations(bool fancy) {
         fancyDecorations = fancy;
         if (fancy)
         {
             CostOfDecorations=(NumberOfPeople*15.00M)+50M;
         }
         else {
             CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;
         }

     }

     public virtual decimal CalculateCost() { 
     
         decimal TotalCost = CostOfDecorations+(CostOfFoodPerson*NumberOfPeople);
         if (NumberOfPeople > 12)
         {
             TotalCost += 100;

         }
         return TotalCost;
     }

    }
   
}
 

 

 

 

 

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

namespace Budget
{
    class DinnerParty:Party
    {

        private int numberOfPeople;
        public decimal CostOfBeveragesPerPerson;
        public decimal CostOfDecorations=0;
        public const int CostOfFoodPerPerson=25;
        private bool fancyDecorations;
        public int NumberOfPeople {

            get {
                return numberOfPeople;
            }
            set { numberOfPeople = value;
            CalculateCostOfDecorations(fancyDecorations);
            }
        }
        //含有三个参数的构造函数
        public DinnerParty(int numberOfPeople,bool healtyOption,bool fancyDecorations) :base(numberOfPeople,fancyDecorations){
            this.NumberOfPeople = numberOfPeople;
            this.fancyDecorations = fancyDecorations;
            SetHealtyOption(healtyOption);
            CalculateCostOfDecorations(fancyDecorations);
        }

        public void SetHealtyOption(bool healtyOption) {
            if (healtyOption)
            {
                CostOfBeveragesPerPerson = 5.00M;

            }
            else {
                CostOfBeveragesPerPerson = 20.00M;
            
            }
        
        }
        public void CalculateCostOfDecorations(bool fancy)
        {
            if (fancy) {
                CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;
            
            }else{
                CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;
                
            }
            
        }
        public decimal CalculateCost(bool healtyOption)
        {
            decimal totalCost = base.CalculateCost() + ((CostOfBeveragesPerPerson + CostOfFoodPerPerson) * NumberOfPeople);
            if(healtyOption){
                return totalCost*.95M;

            }else{
            return totalCost;
            }
        }

    }
}
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Budget
{
    class BirthdayParty:Party
    {
        public const int CostOfFoodPerPerson = 25;
        public decimal CostOfDecorations=0;
        public int CakeSize;
        private bool fancyDecorations;
        private string cakeWriting = "";
        private int numberOfPeople;

        public BirthdayParty(int numberOfPeople, bool fancyDecorations, string cakeWriting):base(numberOfPeople,fancyDecorations)
        {
        //    this.numberOfPeople = numberOfPeople;
        //    this.fancyDecorations = fancyDecorations;
            CalculateCakeSize();
            this.cakeWriting = cakeWriting;
            CalculateCostOfDecorations(fancyDecorations);

        }
        private void CalculateCakeSize() {
            if (numberOfPeople <= 4)
            {
                CakeSize = 8;
            }
            else {
                CakeSize = 16;
            }
        
        }
        public string CakeWriting {
            get {
                return this.cakeWriting;
            }
            set {
                int maxLength;
                if (CakeSize == 8)
                {
                    maxLength = 16;
                }
                else {
                    maxLength = 40;
                }
                if (value.Length > maxLength)
                {
                    MessageBox.Show("Too many letter for a " + CakeSize + "inch cake");
                    if (maxLength > this.cakeWriting.Length)
                    {
                        maxLength = this.cakeWriting.Length;
                        this.cakeWriting = cakeWriting.Substring(0, maxLength);
                    }
                }
                else {
                    this.cakeWriting = value;
                }


            }
        
        }

        public override decimal CalculateCost() {
          //  decimal TotalCost=CostOfDecorations+(CostOfFoodPerPerson*NumberOfPeople);
            decimal CakeCost;
            if (CakeSize == 8)
            {
                CakeCost = 40M + cakeWriting.Length * 0.25M;

            }
            else {
                CakeCost = 75M + cakeWriting.Length * 0.25M;
         
            }
            return base.CalculateCost()+CakeCost;
        }
        public override int NumberOfPeople
        {
            get {
                return numberOfPeople;
            }
            set {
               base.NumberOfPeople = value;
               // CalculateCostOfDecorations(fancyDecorations);
                CalculateCakeSize();
                this.CakeWriting = cakeWriting;

            }
        
        }
        public void CalculateCostOfDecorations(bool fancy) {

            fancyDecorations = fancy;
            if (fancy)
            {
                CostOfDecorations = (NumberOfPeople * 15.00M) + 50.00M;
            }
            else {
                CostOfDecorations = (NumberOfPeople * 7.5M) + 75M;
            }
        
        }

      
    }

    
}
 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Budget
{
    public partial class Form1 : Form
    {
        DinnerParty dinnerParty;
        BirthdayParty birthdayParty;
        public Form1()
        {
            InitializeComponent();
            dinnerParty = new DinnerParty((int)numericUpDown1.Value, checkBox1.Checked, checkBox2.Checked);

            DisplayDinnerPartyCost();
            birthdayParty = new BirthdayParty((int)numericUpDown2.Value,birthdayset.Checked,textBox1.Text);
            DisplayBirthdayPartyCost();
           


        }
        private void DisplayDinnerPartyCost() {
            decimal Cost = dinnerParty.CalculateCost(checkBox2.Checked);
            label3.Text = Cost.ToString("c");
        }
        private void DisplayBirthdayPartyCost()
        {
            textBox1.Text = birthdayParty.CakeWriting;
            decimal cost = birthdayParty.CalculateCost();
            birthdaycost.Text = cost.ToString("c");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.CalculateCostOfDecorations(checkBox1.Checked);
            DisplayDinnerPartyCost();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.SetHealtyOption(checkBox2.Checked);
            DisplayDinnerPartyCost();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            dinnerParty.NumberOfPeople = (int)numericUpDown1.Value;
            DisplayDinnerPartyCost();
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            birthdayParty.NumberOfPeople = (int)numericUpDown2.Value;
            DisplayBirthdayPartyCost();
        }

        private void birthdayset_CheckedChanged(object sender, EventArgs e)
        {
            birthdayParty.CalculateCostOfDecorations(birthdayset.Checked);
            DisplayBirthdayPartyCost();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            birthdayParty.CakeWriting = textBox1.Text;
            DisplayBirthdayPartyCost();
        }
      
      
    }
}
 

转载于:https://my.oschina.net/MStart/blog/719853

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值