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();
}
}
}