開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の3章(オブジェクト指向になる! - わかりやすいコードにする)、エクササイズ(p. 113)を取り組んでみる。
コード
MainPage.xaml
<Page
x:Class="App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Button x:Name="button1" Content="ジョーに1000円渡す" HorizontalAlignment="Left" Margin="292,722,0,0" VerticalAlignment="Top" Click="button1_ClickAsync"/>
<Button x:Name="button2" Content="ボブから500円受け取る" HorizontalAlignment="Left" Margin="909,722,0,0" VerticalAlignment="Top" Click="button2_Click"/>
<TextBlock x:Name="joesCash" HorizontalAlignment="Left" Margin="292,235,0,0" Text="TextBlock" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Segoe UI"/>
<TextBlock x:Name="bobsCash" HorizontalAlignment="Left" Margin="292,406,0,0" Text="TextBlock" TextWrapping="Wrap" VerticalAlignment="Top"/>
<TextBlock x:Name="bankCash" HorizontalAlignment="Left" Margin="292,537,0,0" Text="TextBlock" TextWrapping="Wrap" VerticalAlignment="Top"/>
</Grid>
</Page>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;
// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください
namespace App
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
Guy joe;
Guy bob;
int bank = 10000;
public MainPage()
{
this.InitializeComponent();
bob = new Guy();
bob.name = "ボブ";
bob.cash = 10000;
joe = new Guy();
joe.name = "ジョー";
joe.cash = 5000;
UpdatePage();
}
private void UpdatePage()
{
joesCash.Text = joe.name + "の所持金は" + joe.cash + "円";
bobsCash.Text = bob.name + "の所持金は" + bob.cash + "円";
bankCash.Text = "銀行の所持金は" + bank + "円";
}
private async void button1_ClickAsync(object sender, RoutedEventArgs e)
{
if (bank >= 1000)
{
bank -= joe.ReceiveCashAsync(1000).Result;
UpdatePage();
}
else
{
MessageDialog md =
new MessageDialog("銀行にお金がありません。");
await md.ShowAsync();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
bank += bob.GiveCash(500);
UpdatePage();
}
}
class Guy
{
public string name;
public int cash;
public int GiveCash(int amount)
{
if (amount <= cash && amount > 0)
{
cash -= amount;
return amount;
}
MessageDialog md =
new MessageDialog(
"私はもう" + amount + "円もお金がありません。",
name + "が言った…");
md.ShowAsync();
return 0;
}
public async System.Threading.Tasks.Task<int> ReceiveCashAsync(int amount)
{
if (amount > 0)
{
cash += amount;
return amount;
}
MessageDialog md =
new MessageDialog(
amount + "円ではたりません",
name + "が言った…");
await md.ShowAsync();
return 0;
}
}
}
0 コメント:
コメントを投稿