fork download
  1. public class LoadPaymentMethodOperation
  2. {
  3. private readonly Employee employee;
  4. private readonly string methodCode;
  5. private readonly SqlConnection connection;
  6. private PaymentMethod method;
  7. private delegate void PaymentMethodCreator(DataRow row);
  8. private PaymentMethodCreator paymentMethodCreator;
  9. private string tableName;
  10.  
  11. public LoadPaymentMethodOperation(Employee employee, string methodCode, SqlConnection connection)
  12. {
  13. this.employee = employee;
  14. this.methodCode = methodCode;
  15. this.connection = connection;
  16. }
  17.  
  18. public void Execute()
  19. {
  20. Prepare();
  21. DataRow row = LoadData();
  22. CreatePaymentMethod(row);
  23. }
  24.  
  25. public void CreatePaymentMethod(DataRow row)
  26. {
  27. paymentMethodCreator(row);
  28. }
  29.  
  30. public void Prepare()
  31. {
  32. if(methodCode.Equals("hold"))
  33. paymentMethodCreator = new PaymentMethodCreator(CreateHoldMethod);
  34. else if(methodCode.Equals("directdeposit"))
  35. {
  36. tableName = "DirectDepositAccount";
  37. paymentMethodCreator = new PaymentMethodCreator(CreateDirectDepositMethod);
  38. }
  39. else if(methodCode.Equals("mail"))
  40. {
  41. tableName = "PaycheckAddress";
  42. paymentMethodCreator = new PaymentMethodCreator(CreateMailMethod);
  43. }
  44. }
  45.  
  46. private DataRow LoadData()
  47. {
  48. if(tableName != null)
  49. return LoadEmployeeOperation.LoadDataFromCommand(Command);
  50. else
  51. return null;
  52. }
  53.  
  54. public PaymentMethod Method
  55. {
  56. get { return method; }
  57. }
  58.  
  59. public SqlCommand Command
  60. {
  61. get
  62. {
  63. string sql = String.Format("select * from {0} where EmpId=@EmpId", tableName);
  64. SqlCommand command = new SqlCommand(sql, connection);
  65. command.Parameters.Add("@EmpId", employee.EmpId);
  66. return command;
  67. }
  68. }
  69.  
  70. public void CreateDirectDepositMethod(DataRow row)
  71. {
  72. string bank = row["Bank"].ToString();
  73. string account = row["Account"].ToString();
  74. method = new DirectDepositMethod(bank, account);
  75. }
  76.  
  77. private void CreateHoldMethod(DataRow row)
  78. {
  79. method = new HoldMethod();
  80. }
  81.  
  82. private void CreateMailMethod(DataRow row)
  83. {
  84. string address = row["Address"].ToString();
  85. method = new MailMethod(address);
  86. }
  87. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty