Added tutorial 6

This commit is contained in:
Brychan Dempsey 2021-09-16 17:55:28 +12:00
parent 544ae9c070
commit e9e107e3d3
12 changed files with 202 additions and 95 deletions

View File

@ -6,14 +6,14 @@ using System.Threading.Tasks;
namespace DuckModel namespace DuckModel
{ {
class DecoyDuck : DuckBehaviour public class DecoyDuck : DuckBehaviour
{ {
public override void Display() public override void Display()
{ {
Console.WriteLine("drew a decoy duck"); Console.WriteLine("I am a decoy duck");
} }
DecoyDuck() public DecoyDuck()
{ {
base.SetFlyBehaviour(new NoFly()); base.SetFlyBehaviour(new NoFly());
base.SetQuackBehaviour(new MuteQuack()); base.SetQuackBehaviour(new MuteQuack());

View File

@ -8,12 +8,12 @@ namespace DuckModel
{ {
public interface IFlyBehaviour public interface IFlyBehaviour
{ {
void Fly(); string Fly();
} }
public interface IQuackBehaviour public interface IQuackBehaviour
{ {
void Quack(); string Quack();
} }
public abstract class DuckBehaviour public abstract class DuckBehaviour
{ {
@ -27,14 +27,14 @@ namespace DuckModel
public abstract void Display(); public abstract void Display();
void PerformQuack() public string PerformQuack()
{ {
_quackBehaviour.Quack(); return _quackBehaviour.Quack();
} }
void PerformFly() public string PerformFly()
{ {
_flyBehaviour.Fly(); return _flyBehaviour.Fly();
} }
protected void SetFlyBehaviour(IFlyBehaviour flyBehaviour) protected void SetFlyBehaviour(IFlyBehaviour flyBehaviour)
@ -50,41 +50,41 @@ namespace DuckModel
class Quack : IQuackBehaviour class Quack : IQuackBehaviour
{ {
void IQuackBehaviour.Quack() string IQuackBehaviour.Quack()
{ {
Console.WriteLine("The duck quacks"); return "The duck quacks";
} }
} }
class Squeak : IQuackBehaviour class Squeak : IQuackBehaviour
{ {
void IQuackBehaviour.Quack() string IQuackBehaviour.Quack()
{ {
Console.WriteLine("The duck squeaks"); return "The duck squeaks";
} }
} }
class MuteQuack : IQuackBehaviour class MuteQuack : IQuackBehaviour
{ {
void IQuackBehaviour.Quack() string IQuackBehaviour.Quack()
{ {
Console.WriteLine("The duck makes no sound"); return "The duck makes no sound";
} }
} }
class FlyWithWings : IFlyBehaviour class FlyWithWings : IFlyBehaviour
{ {
public void Fly() public string Fly()
{ {
Console.WriteLine("The duck flies away"); return "I can fly with my wings";
} }
} }
class NoFly : IFlyBehaviour class NoFly : IFlyBehaviour
{ {
public void Fly() public string Fly()
{ {
Console.WriteLine("The duck cannot fly"); return "I cannot fly";
} }
} }
} }

View File

@ -41,6 +41,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DuckTypes.cs" />
<Compile Include="DuckBehaviours.cs" /> <Compile Include="DuckBehaviours.cs" />
<Compile Include="DecoyDuck.cs" /> <Compile Include="DecoyDuck.cs" />
<Compile Include="RubberDucky.cs" /> <Compile Include="RubberDucky.cs" />

17
DuckModel/DuckTypes.cs Normal file
View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DuckModel
{
public enum DuckTypes
{
Mallard,
Redhead,
Rubber,
Decoy,
Default
}
}

View File

@ -6,14 +6,14 @@ using System.Threading.Tasks;
namespace DuckModel namespace DuckModel
{ {
class MallardDuck : DuckBehaviour public class MallardDuck : DuckBehaviour
{ {
public override void Display() public override void Display()
{ {
Console.WriteLine("drew a Mallard duck"); Console.WriteLine("I am a Mallard duck");
} }
MallardDuck() public MallardDuck()
{ {
base.SetFlyBehaviour(new FlyWithWings()); base.SetFlyBehaviour(new FlyWithWings());
base.SetQuackBehaviour(new Quack()); base.SetQuackBehaviour(new Quack());

View File

@ -6,14 +6,14 @@ using System.Threading.Tasks;
namespace DuckModel namespace DuckModel
{ {
class RedheadDuck : DuckBehaviour public class RedheadDuck : DuckBehaviour
{ {
public override void Display() public override void Display()
{ {
Console.WriteLine("drew a Redhead duck"); Console.WriteLine("I am a Redhead duck");
} }
RedheadDuck() public RedheadDuck()
{ {
base.SetFlyBehaviour(new FlyWithWings()); base.SetFlyBehaviour(new FlyWithWings());
base.SetQuackBehaviour(new Quack()); base.SetQuackBehaviour(new Quack());

View File

@ -6,14 +6,14 @@ using System.Threading.Tasks;
namespace DuckModel namespace DuckModel
{ {
class RubberDucky : DuckBehaviour public class RubberDucky : DuckBehaviour
{ {
public override void Display() public override void Display()
{ {
Console.WriteLine("drew a Rubber ducky"); Console.WriteLine("I am a Rubber ducky");
} }
RubberDucky() public RubberDucky()
{ {
base.SetFlyBehaviour(new NoFly()); base.SetFlyBehaviour(new NoFly());
base.SetQuackBehaviour(new Squeak()); base.SetQuackBehaviour(new Squeak());

View File

@ -1,9 +1,4 @@
MIT License
Copyright (c) <year> <copyright holders> Tutorials, scope and content are the property of Massey University. Must not be reproduced without permission.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Contributor code
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -30,105 +30,120 @@ namespace Duck_Simulator
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox1 = new System.Windows.Forms.CheckBox(); this.radioButton1 = new System.Windows.Forms.RadioButton();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.button1 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.checkBox4); this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Controls.Add(this.checkBox3); this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.checkBox2); this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Controls.Add(this.checkBox1); this.groupBox1.Controls.Add(this.radioButton4);
this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Location = new System.Drawing.Point(6, 6);
this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(644, 911); this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
this.groupBox1.Size = new System.Drawing.Size(322, 474);
this.groupBox1.TabIndex = 0; this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Duck Selector"; this.groupBox1.Text = "Duck Selector";
// //
// checkBox1 // radioButton1
// //
this.checkBox1.AutoSize = true; this.radioButton1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(39, 42); this.radioButton1.Location = new System.Drawing.Point(6, 20);
this.checkBox1.Name = "checkBox1"; this.radioButton1.Name = "radioButton1";
this.checkBox1.Size = new System.Drawing.Size(170, 29); this.radioButton1.Size = new System.Drawing.Size(88, 17);
this.checkBox1.TabIndex = 0; this.radioButton1.TabIndex = 4;
this.checkBox1.Text = "Mallard Duck"; this.radioButton1.TabStop = true;
this.checkBox1.UseVisualStyleBackColor = true; this.radioButton1.Text = "Mallard Duck";
// this.radioButton1.UseVisualStyleBackColor = true;
// checkBox2 this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(39, 77);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(186, 29);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "Redhead Duck";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox3
//
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(39, 112);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(180, 29);
this.checkBox3.TabIndex = 2;
this.checkBox3.Text = "Rubber Ducky";
this.checkBox3.UseVisualStyleBackColor = true;
//
// checkBox4
//
this.checkBox4.AutoSize = true;
this.checkBox4.Location = new System.Drawing.Point(39, 147);
this.checkBox4.Name = "checkBox4";
this.checkBox4.Size = new System.Drawing.Size(160, 29);
this.checkBox4.TabIndex = 3;
this.checkBox4.Text = "Decoy Duck";
this.checkBox4.UseVisualStyleBackColor = true;
// //
// button1 // button1
// //
this.button1.Location = new System.Drawing.Point(682, 36); this.button1.Location = new System.Drawing.Point(341, 19);
this.button1.Margin = new System.Windows.Forms.Padding(2);
this.button1.Name = "button1"; this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(134, 47); this.button1.Size = new System.Drawing.Size(67, 24);
this.button1.TabIndex = 4; this.button1.TabIndex = 4;
this.button1.Text = "Quack"; this.button1.Text = "Quack";
this.button1.UseVisualStyleBackColor = true; this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
// //
// button2 // button2
// //
this.button2.Location = new System.Drawing.Point(682, 106); this.button2.Location = new System.Drawing.Point(341, 55);
this.button2.Margin = new System.Windows.Forms.Padding(2);
this.button2.Name = "button2"; this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(134, 47); this.button2.Size = new System.Drawing.Size(67, 24);
this.button2.TabIndex = 5; this.button2.TabIndex = 5;
this.button2.Text = "Fly"; this.button2.Text = "Fly";
this.button2.UseVisualStyleBackColor = true; this.button2.UseVisualStyleBackColor = true;
// //
// button3 // button3
// //
this.button3.Location = new System.Drawing.Point(682, 179); this.button3.Location = new System.Drawing.Point(341, 93);
this.button3.Margin = new System.Windows.Forms.Padding(2);
this.button3.Name = "button3"; this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(134, 47); this.button3.Size = new System.Drawing.Size(67, 24);
this.button3.TabIndex = 6; this.button3.TabIndex = 6;
this.button3.Text = "Swim"; this.button3.Text = "Swim";
this.button3.UseVisualStyleBackColor = true; this.button3.UseVisualStyleBackColor = true;
// //
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(4, 43);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(98, 17);
this.radioButton2.TabIndex = 5;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "Redhead Duck";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(4, 66);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(89, 17);
this.radioButton3.TabIndex = 6;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "Rubber Duck";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
//
// radioButton4
//
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(4, 89);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(86, 17);
this.radioButton4.TabIndex = 7;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "EDcoy Duck";
this.radioButton4.UseVisualStyleBackColor = true;
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1497, 935); this.ClientSize = new System.Drawing.Size(748, 486);
this.Controls.Add(this.button3); this.Controls.Add(this.button3);
this.Controls.Add(this.button2); this.Controls.Add(this.button2);
this.Controls.Add(this.button1); this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1); this.Controls.Add(this.groupBox1);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "Form1"; this.Name = "Form1";
this.Text = "Form1"; this.Text = "Form1";
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
@ -140,13 +155,13 @@ namespace Duck_Simulator
#endregion #endregion
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button3;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
} }
} }

View File

@ -7,14 +7,86 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using DuckModel;
namespace Duck_Simulator namespace Duck_Simulator
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
private DuckTypes _selectedDuck = DuckTypes.Default;
private DuckBehaviour _duck;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
}
void test()
{
radioButton1.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
switch (_selectedDuck)
{
case DuckTypes.Mallard:
if (!(_duck is MallardDuck))
{
_duck = new MallardDuck();
}
break;
case DuckTypes.Redhead:
if (!(_duck is RedheadDuck))
{
_duck = new RedheadDuck();
}
break;
case DuckTypes.Rubber:
if (!(_duck is RubberDucky))
{
_duck = new RubberDucky();
}
break;
case DuckTypes.Decoy:
if (!(_duck is DecoyDuck))
{
_duck = new DecoyDuck();
}
break;
default:
// Show msg box
MessageBox.Show("Not a valid duck");
return;
}
string msg = _duck.PerformQuack();
MessageBox.Show(msg);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if ((sender as RadioButton).Checked)
_selectedDuck = DuckTypes.Mallard;
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
_selectedDuck = DuckTypes.Decoy;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
_selectedDuck = DuckTypes.Rubber;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
_selectedDuck = DuckTypes.Redhead;
} }
} }
} }

View File

@ -17,6 +17,7 @@ namespace Duck_Simulator
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); Application.Run(new Form1());
//
} }
} }
} }

View File

@ -78,5 +78,11 @@
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DuckModel\DuckModel.csproj">
<Project>{3085be3e-81c7-48a4-b62a-80e8ba453dac}</Project>
<Name>DuckModel</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>