Menu
CoddyTech

プロジェクト:注文モデル

Coddyの「Kotlin開発者のためのC#」コースのレッスン 43/48。

注文処理プログラムを、モデル化、検証、価格計算、レポート作成の4つのステップで構築します。ロックされたメイン部分とファイル構成は、すべての段階で同一です。各続きでは、前段階の完成した編集可能な解答から始めます。最初の入力行で段階ごとの仕様を選択するため、それ以前の動作も引き続きテストできます。

challenge icon

チャレンジ

簡単

注文モデルを作成します。入力はステージ 1 と true/false の VIP フラグで始まり、その後に有効な name|quantity|unit の行が 0 行以上続きます。Order.Add とパイプラインを実装します。accepted=row count;rejected=0;total=0;items= を返します。名前は空でない ASCII の英字、数字、アンダースコアで構成されます。数値と中間積は int に収まる大きさです。後のステージで使うすべてのファイルと API を残してください。

ロックされていない C# ファイルを編集します。表示されているロック済みの Main.cs は、説明されている行を読み込み、Exercise.Solve を呼び出し、入力が変更されていないことを検証して、最後に改行を 1 つ付けて結果を出力します。宣言済みの API を維持してください。2 つ目の Main を追加したり、診断出力を表示したりしないでください。

ロック済みのランチャーは、Solve ラッパーだけでなく、エクスポートされたヘルパーまたはクラスの API も個別に検査します。

自分で試してみよう

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = input[1] == "true"; string before1=Snapshot(a1);
var a2 = input.Skip(2).ToArray(); string before2=Snapshot(a2);
var orderProbe = new Order(); var separateOrder = new Order(); orderProbe.Add(new OrderLine("tea",2,10)); if (orderProbe.Count != 1 || separateOrder.Count != 0 || orderProbe.Lines.Single().Quantity != 2) throw new Exception("Order API");
if (a0 >= 2) { OrderLine parsed; if (LineParser.TryCreate("bad",out parsed) || !LineParser.TryCreate(" tea |2|10",out parsed) || parsed.Name != "tea") throw new Exception("Parser API"); }
if (a0 >= 3 && (new PricePolicy(false).Apply(11) != 11 || new PricePolicy(true).Apply(11) != 9)) throw new Exception("Project pricing API");
if (a0 >= 4 && Report.Build(4,orderProbe,false,0) != "accepted=1;rejected=0;total=20;items=tea:2") throw new Exception("Report API");
var result = Exercise.Solve(a0,a1,a2);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
if (Snapshot(a2) != before2) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}

Kotlin開発者のためのC#のすべてのレッスン

自分で練習してみよう: C#オンラインコンパイラ