Subwayã®ãµã³ãã¤ãã
ããã®ã¨ãããã£ã¨ä»äºã®ã³ã¼ãã®ifãforã®åå²ã追ã£ã¦ãã¦é ããããããã¦ã¾ããã
ãã®ããããæ¼ã«Subwayã«è¡ã£ãããããªçºæ³ãé ããé¢ããªããªã£ãã
ãµãã¦ã§ã¤ã®åºå¡ããã®é ã®ä¸ã§ã¯åçã«ifæãçµã¿ç«ã¦ããã¦ããããnotãã¼ãã³ãnotãªãªã¼ããããã++
http://twitter.com/hayashih/status/1635791461
ã³ã¼ãã«ãã¦ã¿ãã
â Main.cs
â Subway.cs
using System;namespace Hayashih.Subway
{
class MainClass
{
static Crew subwayCrew = new Crew();
public static void Main(string[] args)
{
// defaut
subwayCrew.ReceiveSandwichOrder();
Sandwich sandwich1 = subwayCrew.FinishOrder();
Console.WriteLine("sandwich1: " + sandwich1.ToString());
// add tomato,lettuce, pull out pickles, olive
subwayCrew.ReceiveSandwichOrder();
subwayCrew.AddVegetable(Vegetable.Tomato);
subwayCrew.AddVegetable(Vegetable.Lettuce);
subwayCrew.DeleleteVegetable(Vegetable.Pickles);
subwayCrew.DeleleteVegetable(Vegetable.Olive);
Sandwich sandwich2 = subwayCrew.FinishOrder();
Console.WriteLine("sandwich2: " + sandwich2.ToString());
// my favorite :)
subwayCrew.ReceiveSandwichOrder();
subwayCrew.AddVegetable(Vegetable.Olive);
subwayCrew.AddVegetable(Vegetable.Onion);
Sandwich myFavoriteSandwich = subwayCrew.FinishOrder();
Console.WriteLine("myFavoriteSandwich: " + myFavoriteSandwich.ToString());
}
}
}
注)ãã®ã³ã¼ãã¯ãåºå¡ã¯1度ã«1åã®ãµã³ãã¤ããããä½ããªããï¼ãã¤ãè¡ãåºã®åºå¡ããã¯ãæ¼æã¯1度ã«2ã3åä½ãã¾ãï¼ã客ããµã³ãã¤ããã«ã¢ã¯ã»ã¹ã§ããããåé¡ããããã®åºã§ãw
using System;
using System.Collections.Generic;namespace Hayashih.Subway
{
///
/// Subway Crew
///
public class Crew
{
public Crew(){}
private Sandwich currnetSandwich = null;
public void ReceiveSandwichOrder()
{
if( this.currnetSandwich != null ){
this.currnetSandwich = null;
}
this.currnetSandwich = new Sandwich();
}
public void AddVegetable(Vegetable vege)
{
// accepts even if said suddenly.
if( this.currnetSandwich == null )
{
this.currnetSandwich = new Sandwich();
}
this.currnetSandwich.AddVegetable(vege);
}
public void DeleleteVegetable(Vegetable vege)
{
// accepts even if said suddenly.
if( this.currnetSandwich == null )
{
this.currnetSandwich = new Sandwich();
}
this.currnetSandwich.DeleteVegetable(vege);
}
public Sandwich FinishOrder()
{
return this.currnetSandwich;
}
}
///
/// A Sandwich. It is only VEGGIE DELITE!
///
public class Sandwich
{
public Sandwich()
{
this.Ready();
}
private Listvegetables = new List ();
public void Ready()
{
this.vegetables.Add(Vegetable.Lettuce);
this.vegetables.Add(Vegetable.Olive);
this.vegetables.Add(Vegetable.Onion);
this.vegetables.Add(Vegetable.Pickles);
this.vegetables.Add(Vegetable.Pimiento);
this.vegetables.Add(Vegetable.Tomato);
}
public void AddVegetable(Vegetable vege)
{
this.vegetables.Add(vege);
}
public void DeleteVegetable(Vegetable vege)
{
this.vegetables.Remove(vege);
}
public override string ToString ()
{
string ret = String.Empty;
foreach( Vegetable v in this.vegetables)
{
ret += v.ToString() + " ";
}
return ret;
}}
public enum Vegetable
{
Tomato,
Olive,
Onion,
Pimiento,
Lettuce,
Pickles
}
}
ãã¾ã«ã¯ãããªãã ãããã¨ã§ãèãã¦ã¯ã©ã¹ã®ä½ãæ¹ãªã©ãªã©ã¡ããã¨æãåºãã¦é ã®æ´çãããã®ããããããããªãã