-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
198 lines (133 loc) · 4.19 KB
/
Copy pathProgram.cs
File metadata and controls
198 lines (133 loc) · 4.19 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
namespace AdapterPattern
{
internal class Program
{
private static void Main(string[] args)
{
// Non-adapted chemical compound
var unknown = new Compound("Unknown");
unknown.Display();
// Adapted chemical compounds
Compound water = new RichCompound("Water");
water.Display();
Compound benzene = new RichCompound("Benzene");
benzene.Display();
Compound ethanol = new RichCompound("Ethanol");
ethanol.Display();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'Target' class
/// </summary>
internal class Compound
{
protected float _boilingPoint;
protected string _chemical;
protected float _meltingPoint;
protected string _molecularFormula;
protected double _molecularWeight;
// Constructor
public Compound(string chemical)
{
_chemical = chemical;
}
public virtual void Display()
{
Console.WriteLine("\nCompound: {0} ------ ", _chemical);
}
}
/// <summary>
/// The 'Adapter' class
/// </summary>
internal class RichCompound : Compound
{
private ChemicalDatabank _bank;
// Constructor
public RichCompound(string name)
: base(name)
{
}
public override void Display()
{
// The Adaptee
_bank = new ChemicalDatabank();
_boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
_meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
_molecularWeight = _bank.GetMolecularWeight(_chemical);
_molecularFormula = _bank.GetMolecularStructure(_chemical);
base.Display();
Console.WriteLine(" Formula: {0}", _molecularFormula);
Console.WriteLine(" Weight : {0}", _molecularWeight);
Console.WriteLine(" Melting Pt: {0}", _meltingPoint);
Console.WriteLine(" Boiling Pt: {0}", _boilingPoint);
}
}
/// <summary>
/// The 'Adaptee' class
/// </summary>
internal class ChemicalDatabank
{
// The databank 'legacy API'
public float GetCriticalPoint(string compound, string point)
{
// Melting Point
if (point == "M")
{
switch (compound.ToLower())
{
case "water":
return 0.0f;
case "benzene":
return 5.5f;
case "ethanol":
return -114.1f;
default:
return 0f;
}
}
// Boiling Point
switch (compound.ToLower())
{
case "water":
return 100.0f;
case "benzene":
return 80.1f;
case "ethanol":
return 78.3f;
default:
return 0f;
}
}
public string GetMolecularStructure(string compound)
{
switch (compound.ToLower())
{
case "water":
return "H20";
case "benzene":
return "C6H6";
case "ethanol":
return "C2H5OH";
default:
return "";
}
}
public double GetMolecularWeight(string compound)
{
switch (compound.ToLower())
{
case "water":
return 18.015;
case "benzene":
return 78.1134;
case "ethanol":
return 46.0688;
default:
return 0d;
}
}
}
}