id_vg | ou_name | ou_info
12 ezzaez eaezaeae
13 tbtrb grtrr
14 hyht yhty
3
SELECT id_vg, ou_name, ou_info
FROM vignette_ou AS vg
INNER JOIN ref_vg_ou AS ref
ON vg.vg_id = ref.vg_id
WHERE ref.ref = ?
“ input.txt”文件包含上述信息。第一行是不同巧克力的数量,其余的是这样的:
巧克力类型;可可量;材料(其中3种);以及巧克力是否优质。
如果是高级版本,则必须创建一个PremiumChocolateFactory对象,否则,只需创建一个普通ChocolateFactory对象。我的高级巧克力对象在控制台上显示的很好,但是普通的巧克力却变得混乱不堪。 PremiumChocolateFactory继承自ChocolateFactory。我在ChocolateFactory类中重写了ToString方法。
SELECT vg.id_vg, vg.ou_name, vg.ou_info
FROM vignette_ou AS vg
INNER JOIN ref_vg_ou AS ref
ON vg.vg_id = ref.vg_id
WHERE ref.ref = ?
我知道Console.WriteLines不应在while循环内,但是如果它们不在大声笑之外,则会出现错误。
答案 0 :(得分:0)
我将更改您的代码中的某些内容,以针对您的两个类提供更简洁的界面。首先,这两个类应该是这样的
public class ChocolateFactory
{
public string ChocolateType { get; set; }
public int CocoaAmount { get; set; }
public List<string> Materials {get;set;} = new List<string>();
public override string ToString()
{
return $"Chocolate type: {ChocolateType} | Materials: {string.Join(", ", Materials)} | Cocoa amount: {CocoaAmount}%, IsPremium=No";
}
}
public class PremiumChocolateFactory : ChocolateFactory
{
public override string ToString()
{
return $"Chocolate type: {ChocolateType} | Materials: {string.Join(", ", Materials)} | Cocoa amount: {CocoaAmount}%, IsPremium=Yes";
}
}
因此,现在这些类对材料有一个公共List<string>
,因此无需在代码中拥有自己的列表
List<ChocolateFactory> myChocos = new List<ChocolateFactory>();
using (StreamReader sr = new StreamReader(@"e:\temp\input.txt"))
{
int lineCount = int.Parse(sr.ReadLine());
while (!sr.EndOfStream)
{
string[] line = sr.ReadLine().Split(';');
bool isPremium = line[line.Length - 1] == "premium";
int length = isPremium ? line.Length - 1 : line.Length;
var t = isPremium ? new PremiumChocolateFactory()
: new ChocolateFactory();
t.ChocolateType = line[0];
t.CocoaAmount = Int32.Parse(line[1]);
for (int i = 2; i < length; i++)
t.Materials.Add(line[i]);
myChocos.Add(t);
}
}
....
foreach(var x in myChocos)
Console.WriteLine(x);