Party
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dinnerp
{
class Party
{
public const int CostOfFoodPerPerson = 25;//人均花费
public decimal CostOfDecorations = 0;
public bool fancyDecorations;
public Party(int numberOfPeople, bool fancyDecorations)
{
NumberOfPeople = numberOfPeople;
this.fancyDecorations = fancyDecorations;
CalculateCostOfDecorations(fancyDecorations);
}
public int numberOfPeople;
public virtual int NumberOfPeople
{
get
{ return numberOfPeople; }
set
{
numberOfPeople = value;
CalculateCostOfDecorations(fancyDecorations);
}
}
public void CalculateCostOfDecorations(bool fancy)
{
fancyDecorations = fancy;
if (fancy)
CostOfDecorations = (NumberOfPeople * 15.00m) + 50m;
else
CostOfDecorations = (NumberOfPeople * 7.00m) + 30m;
}
public virtual decimal CalculateCost()
{
decimal TotalCost = CostOfDecorations + (CostOfFoodPerPerson * NumberOfPeople);
if (NumberOfPeople >12)
{
TotalCost += 100m;
}
return TotalCost;
}
}
}
DinnerParty
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dinnerp
{
class DinnerParty:Party
{
public decimal CostOfBeveraagesPerPerson;//酒水的花费
public DinnerParty(int numberOfPeople, bool healthyOption, bool fancyDecorations)
:base(numberOfPeople,fancyDecorations)
{
NumberOfPeople = numberOfPeople;
this.fancyDecorations = fancyDecorations;
SetHealthyOption(healthyOption);
CalculateCostOfDecorations(fancyDecorations);
}
/// <summary>
/// 计算酒水价格
/// </summary>
/// <param name="healthyOption"></param>
public void SetHealthyOption(bool healthyOption){
if (healthyOption){
CostOfBeveraagesPerPerson = 5.00m;
}
else{
CostOfBeveraagesPerPerson = 20.00m;
}
}
/// <summary>
/// 计算在总价格
/// </summary>
/// <param name="healthyOption">健康选择</param>
/// <returns></returns>
public decimal CalculateCost(bool healthyOption){
decimal totalCost = base.CalculateCost() +
((CostOfBeveraagesPerPerson + CostOfFoodPerPerson) * NumberOfPeople);
if (healthyOption){
return totalCost * .95m;
}
else
return totalCost;
}
}
}
BirthdayParty
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace Dinnerp{
class BirthdayParty:Party{
public int CakeSize;//蛋糕尺寸字段
/// <summary>
/// 构造函数
/// </summary>
/// <param name="numberOfPeople">人数</param>
/// <param name="fancyDecorations">类型</param>
/// <param name="cakeWriting">字母</param>
public BirthdayParty(int numberOfPeople, bool fancyDecorations, string cakeWriting)
: base(numberOfPeople, fancyDecorations)
{
this.numberOfPeople = numberOfPeople;
this.fancyDecorations = fancyDecorations;
CalculateCakesize();
this.CakeWriting = cakeWriting;
CalculateCostOfDecorations(fancyDecorations);
}
public override int NumberOfPeople {
get { return numberOfPeople; }
set { numberOfPeople = value;
CalculateCostOfDecorations(fancyDecorations);
CalculateCakesize();
this.CakeWriting = cakeWriting;
}
}
private string cakeWriting = "";//蛋糕字母属性
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 letters for a "+CakeSize + "inch cake");
if (maxLength >this.cakeWriting.Length)
maxLength = this.cakeWriting.Length;
this.cakeWriting = cakeWriting.Substring(0,maxLength);
}
else
this.cakeWriting = value;
}
}
/// <summary>
/// 尺寸计算
/// </summary>
public void CalculateCakesize(){
if (NumberOfPeople <= 4)
CakeSize = 8;
else
CakeSize = 16;
}
/// <summary>
/// 花费计算
/// </summary>
/// <returns>钱总数</returns>
public override decimal CalculateCost()
{
decimal cakeCost;
if (CakeSize == 8)
cakeCost = 40m + cakeWriting.Length * .25m;
else
cakeCost = 75m + cakeWriting.Length * .25m;
return base.CalculateCost() + cakeCost;
}
}
}
Forms1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dinnerp
{
public partial class Form1 : Form
{
DinnerParty dinnerParty;
BirthdayParty birthdayParty;
public Form1()
{
InitializeComponent();
dinnerParty = new DinnerParty((int)peopleNumUD.Value,checkBox2.Checked,checkBox1.Checked);
DisplayDinnerPartuCost();
birthdayParty = new BirthdayParty((int)birthdayNumPeoole.Value,birthCheck.Checked,cakeWri.Text);
DisplayBirrhdayPartyCost();
}
private void DisplayBirrhdayPartyCost()
{
decimal cost = birthdayParty.CalculateCost();
birthCost.Text = cost.ToString("c");
}
private void DisplayDinnerPartuCost()
{
decimal cost = dinnerParty.CalculateCost(checkBox2.Checked);
costLable.Text = cost.ToString("c");
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
dinnerParty.NumberOfPeople = (int)peopleNumUD.Value;
DisplayDinnerPartuCost();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
dinnerParty.CalculateCostOfDecorations(checkBox1.Checked);
DisplayDinnerPartuCost();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
dinnerParty.SetHealthyOption(checkBox2.Checked);
DisplayDinnerPartuCost();
}
private void birthCheck_CheckedChanged(object sender, EventArgs e)
{
birthdayParty.CalculateCostOfDecorations(birthCheck.Checked);
DisplayBirrhdayPartyCost();
}
private void birthdayNumPeoole_ValueChanged(object sender, EventArgs e)
{
birthdayParty.NumberOfPeople = (int)birthdayNumPeoole.Value;
DisplayBirrhdayPartyCost();
}
private void cakeWri_TextChanged(object sender, EventArgs e)
{
birthdayParty.CakeWriting = cakeWri.Text;
DisplayBirrhdayPartyCost();
}
}
}