-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (125 loc) · 4.29 KB
/
Copy pathProgram.cs
File metadata and controls
140 lines (125 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
namespace GeneticPredictor
{
public class ChildPredictor
{
public string MomHair { get; }
public string MomEyes { get; }
public string DadHair { get; }
public string DadEyes { get; }
internal string PredictedHair { get; }
internal string PredictedEyes { get; }
public ChildPredictor(string mHair, string mEyes, string fHair, string fEyes)
{
MomHair = mHair;
MomEyes = mEyes;
DadHair = fHair;
DadEyes = fEyes;
PredictedHair = TraitCalculator.CalculateHair(mHair, fHair);
PredictedEyes = TraitCalculator.CalculateEyes(mEyes, fEyes);
}
public void ShowPrediction()
{
Console.WriteLine("\n-Prediction Result-");
Console.WriteLine($"Hair color: {PredictedHair}");
Console.WriteLine($"Eye color: {PredictedEyes}");
}
private class TraitCalculator
{
public static string CalculateHair(string h1, string h2)
{
if (h1 == "Black" && h2 == "Black")
{
return "Black";
}
else if (h1 == "Brown" && h2 == "Brown")
{
return "Brown";
}
else if (h1 == "Blonde" && h2 == "Blonde")
{
return "Blonde";
}
else if (h1 == "Red" && h2 == "Red")
{
return "Red";
}
else if ((h1 == "Red" && h2 == "Blonde") || (h1 == "Blonde" && h2 == "Red"))
{
return "Blonde";
}
else if (h1 == "Blonde" || h2 == "Blonde")
{
return "Brown";
}
else if (h1 == "Brown" || h2 == "Brown")
{
return "Brown";
}
else
{
return "Black";
}
}
public static string CalculateEyes(string e1, string e2)
{
if (e1 == "Black" || e2 == "Black")
{
return "Black";
}
else if (e1 == "Brown" || e2 == "Brown")
{
return "Brown";
}
else if (e1 == "Blue" && e2 == "Blue")
{
return "Blue";
}
else
{
return "Green";
}
}
}
}
class Program
{
static void Main()
{
Console.WriteLine("-Child Genetic Predictor-");
string[] validHair = { "Brown", "Blonde", "Black", "Red" };
string[] validEyes = { "Brown", "Blue", "Green", "Black" };
string motherHair = ReadValidOption("Please specify Mother's hair color", validHair);
string motherEyes = ReadValidOption("Please specify Mother's eye color", validEyes);
string fatherHair = ReadValidOption("Please specify Father's hair color", validHair);
string fatherEyes = ReadValidOption("Please specify Father's eye color", validEyes);
ChildPredictor predictor =
new ChildPredictor(motherHair, motherEyes, fatherHair, fatherEyes);
predictor.ShowPrediction();
Console.WriteLine("\nThank you for using the predictor!");
}
static string ReadValidOption(string message, string[] validOptions)
{
while (true)
{
try
{
Console.Write($"{message} ({string.Join("| ", validOptions)}): ");
string input = Console.ReadLine() ?? string.Empty;
foreach (string option in validOptions)
{
if (string.Equals(input, option, StringComparison.OrdinalIgnoreCase))
{
return option;
}
}
Console.WriteLine("Invalid option. Please choose one of the listed values.");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred. Error details: {ex.Message}");
}
}
}
}
}