開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承 - オブジェクトの系図)、エクササイズ(p. 204)を取り組んでみる。
コード
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:App1" x:Class="App1.MainPage"> <ContentPage Title="夕食会"> <StackLayout> <Label Text="参加人数"/> <Label x:Name="label1" Text="5人"/> <Slider x:Name="numberDinner" Maximum="20" Value="5" ValueChanged="numberDinner_ValueChanged"/> <Label Text="デコレーション" /> <Switch x:Name="fancyDinner" IsToggled="True" Toggled="fancyDinner_Toggled"/> <Label Text="ヘルシーオプション"/> <Switch x:Name="healthyDinner" IsToggled="False" Toggled="healthyDinner_Toggled"/> <Label x:Name="dinnerCost" Text="総額"/> </StackLayout> </ContentPage> <ContentPage Title="誕生会"> <StackLayout> <Label Text="参加人数"/> <Label x:Name="label2" Text="5人"/> <Slider x:Name="numberBirthday" Maximum="20" Value="5" ValueChanged="numberBirthday_ValueChanged"/> <Label Text="デコレーション" /> <Switch x:Name="fancyBirthday" IsToggled="True" Toggled="fancyBirthday_Toggled"/> <Label Text="ケーキに書き込む文字"/> <Editor x:Name="cakeWriting" Text="Happy Birthday" TextChanged="cakeWriting_TextChanged"/> <Label x:Name="birthdayCost" Text="総額"/> </StackLayout> </ContentPage> </TabbedPage>
MainPage.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace App1 { public partial class MainPage : TabbedPage { DinnerParty dinnerParty; BirthdayParty birthdayParty; public MainPage() { InitializeComponent(); dinnerParty = new DinnerParty( (int)numberDinner.Value, fancyDinner.IsToggled, healthyDinner.IsToggled); birthdayParty = new BirthdayParty( (int)numberBirthday.Value, fancyBirthday.IsToggled, cakeWriting.Text); DisplayDinnerPartyCost(); DisplayBirthdayPartyCost(); } private void numberDinner_ValueChanged(object sender, ValueChangedEventArgs e) { label1.Text = numberDinner.Value + "人"; dinnerParty.NumberOfPeople = (int)numberDinner.Value; DisplayDinnerPartyCost(); } private void fancyDinner_Toggled(object sender, ToggledEventArgs e) { dinnerParty.CalculateCostOfDecorations(fancyDinner.IsToggled); DisplayDinnerPartyCost(); } private void healthyDinner_Toggled(object sender, ToggledEventArgs e) { DisplayDinnerPartyCost(); } private void numberBirthday_ValueChanged(object sender, ValueChangedEventArgs e) { label2.Text = numberBirthday.Value + "人"; birthdayParty.NumberOfPeople = (int)numberBirthday.Value; DisplayBirthdayPartyCost(); } private void fancyBirthday_Toggled(object sender, ToggledEventArgs e) { birthdayParty.CalculateCostOfDecorations(fancyBirthday.IsToggled); DisplayBirthdayPartyCost(); } private void cakeWriting_TextChanged(object sender, TextChangedEventArgs e) { birthdayParty.CakeWriting = cakeWriting.Text; DisplayBirthdayPartyCost(); } private void DisplayDinnerPartyCost() { decimal cost = dinnerParty.CalculateCost(healthyDinner.IsToggled); dinnerCost.Text = cost.ToString("c"); ; } private void DisplayBirthdayPartyCost() { decimal cost = birthdayParty.CalculateCost(); birthdayCost.Text = cost.ToString("c"); } } public class Party: ContentPage { const int costOfFoodPerPerson = 25; private int numberOfPeople; private decimal costOfDecorations = 0; private bool fancyDecorations; public Party(int numberOfPeople, bool fancyDecorations) { this.numberOfPeople = numberOfPeople; this.fancyDecorations = fancyDecorations; } public virtual int NumberOfPeople { get { return numberOfPeople; } set { numberOfPeople = value; CalculateCostOfDecorations(fancyDecorations); } } public decimal CostOfDecorations { get { return costOfDecorations; } } 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 + costOfFoodPerPerson * numberOfPeople; if (numberOfPeople > 12) { totalCost += 10000M; } return totalCost; } } public class DinnerParty : Party { private decimal costOfBeveragesPerPerson; public DinnerParty( int numberOfPeople, bool fancyDecorations, bool healthyOption): base(numberOfPeople, fancyDecorations) { SetHealthyOption(healthyOption); CalculateCostOfDecorations(fancyDecorations); } public void SetHealthyOption(bool healthyOption) { costOfBeveragesPerPerson = healthyOption ? 5.00M : 20.00M; } public decimal CalculateCost(bool healthOption) { decimal totalCost = base.CalculateCost() + (costOfBeveragesPerPerson * NumberOfPeople); return healthOption ? totalCost * 0.95M : totalCost; } } public class BirthdayParty : Party { private int cakeSize; private string cakeWriting = ""; public BirthdayParty (int numberOfPeople, bool fancyDecorations, string cakeWriting) :base(numberOfPeople, fancyDecorations) { CalculateCakeSize(); this.cakeWriting = cakeWriting; CalculateCostOfDecorations(fancyDecorations); } public override int NumberOfPeople { get => base.NumberOfPeople; set { base.NumberOfPeople = value; CalculateCakeSize(); this.CakeWriting = cakeWriting; } } public string CakeWriting { get { return this.cakeWriting; } set { int maxLength = cakeSize == 7 ? 16 : 40; if (value.Length > maxLength) { DisplayAlert( "", cakeSize + "号のケーキには文字が多すぎます。", "OK"); if (maxLength > this.cakeWriting.Length) { maxLength = this.cakeWriting.Length; } this.cakeWriting = cakeWriting.Substring(0, maxLength); } else { this.cakeWriting = value; } } } private void CalculateCakeSize() { cakeSize = NumberOfPeople <= 4 ? 7 : 15; } public override decimal CalculateCost() { decimal cakeCost = cakeSize == 7 ? 400M + cakeWriting.Length * 25M : 750M + cakeWriting.Length * 25M; return base.CalculateCost() + cakeCost; } } }
App.xaml
<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App1.App"> <Application.Resources> </Application.Resources> </Application>
App.xaml.cs
using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation(XamlCompilationOptions.Compile)] namespace App1 { public partial class App : Application { public App() { InitializeComponent(); // MainPage = new MainPage(); MainPage = new NavigationPage( new MainPage() { Title = "パーティーの計画" }); } protected override void OnStart() { // Handle when your app starts } protected override void OnSleep() { // Handle when your app sleeps } protected override void OnResume() { // Handle when your app resumes } } }
0 コメント:
コメントを投稿