fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. Compound unknown = new Compound("Unknown");
  8. unknown.Display();
  9.  
  10. // Adapted chemical compounds
  11. Compound water = new RichCompound("Water");
  12. water.Display();
  13.  
  14. Compound benzene = new RichCompound("Benzene");
  15. benzene.Display();
  16.  
  17. Compound ethanol = new RichCompound("Ethanol");
  18. ethanol.Display();
  19.  
  20. // Wait for user
  21. Console.ReadKey();
  22. }
  23. }
  24.  
  25. class Compound
  26. {
  27. protected string _chemical;
  28. protected float _boilingPoint;
  29. protected float _meltingPoint;
  30. protected double _molecularWeight;
  31. protected string _molecularFormula;
  32.  
  33. // Constructor
  34. public Compound(string chemical)
  35. {
  36. this._chemical = chemical;
  37. }
  38.  
  39. public virtual void Display()
  40. {
  41. Console.WriteLine("\nCompound: {0} ------ ", _chemical);
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// The 'Adapter' class
  47. /// </summary>
  48. class RichCompound : Compound
  49. {
  50. private ChemicalDatabank _bank;
  51.  
  52. // Constructor
  53. public RichCompound(string name)
  54. : base(name)
  55. {
  56. }
  57.  
  58. public override void Display()
  59. {
  60. // The Adaptee
  61. _bank = new ChemicalDatabank();
  62.  
  63. _boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
  64. _meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
  65. _molecularWeight = _bank.GetMolecularWeight(_chemical);
  66. _molecularFormula = _bank.GetMolecularStructure(_chemical);
  67.  
  68. base.Display();
  69. Console.WriteLine(" Formula: {0}", _molecularFormula);
  70. Console.WriteLine(" Weight : {0}", _molecularWeight);
  71. Console.WriteLine(" Melting Pt: {0}", _meltingPoint);
  72. Console.WriteLine(" Boiling Pt: {0}", _boilingPoint);
  73. }
  74. }
  75.  
  76. /// <summary>
  77. /// The 'Adaptee' class
  78. /// </summary>
  79. class ChemicalDatabank
  80. {
  81. // The databank 'legacy API'
  82. public float GetCriticalPoint(string compound, string point)
  83. {
  84. // Melting Point
  85. if (point == "M")
  86. {
  87. switch (compound.ToLower())
  88. {
  89. case "water": return 0.0f;
  90. case "benzene": return 5.5f;
  91. case "ethanol": return -114.1f;
  92. default: return 0f;
  93. }
  94. }
  95. // Boiling Point
  96. else
  97. {
  98. switch (compound.ToLower())
  99. {
  100. case "water": return 100.0f;
  101. case "benzene": return 80.1f;
  102. case "ethanol": return 78.3f;
  103. default: return 0f;
  104. }
  105. }
  106. }
  107.  
  108. public string GetMolecularStructure(string compound)
  109. {
  110. switch (compound.ToLower())
  111. {
  112. case "water": return "H20";
  113. case "benzene": return "C6H6";
  114. case "ethanol": return "C2H5OH";
  115. default: return "";
  116. }
  117. }
  118.  
  119. public double GetMolecularWeight(string compound)
  120. {
  121. switch (compound.ToLower())
  122. {
  123. case "water": return 18.015;
  124. case "benzene": return 78.1134;
  125. case "ethanol": return 46.0688;
  126. default: return 0d;
  127. }
  128. }
  129. }
Success #stdin #stdout 0.03s 34000KB
stdin
Standard input is empty
stdout
Compound: Unknown ------ 

Compound: Water ------ 
 Formula: H20
 Weight : 18.015
 Melting Pt: 0
 Boiling Pt: 100

Compound: Benzene ------ 
 Formula: C6H6
 Weight : 78.1134
 Melting Pt: 5.5
 Boiling Pt: 80.1

Compound: Ethanol ------ 
 Formula: C2H5OH
 Weight : 46.0688
 Melting Pt: -114.1
 Boiling Pt: 78.3