Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package ca.bcit.cst.comp2526.assign3.a00752818.operations;
  2.  
  3.  
  4. /**
  5. * Create operations.
  6. *
  7. * @author D'Arcy Smith
  8. * @version 1.0
  9. */
  10. public class OperationFactory
  11. {
  12. public static OperationType opera;
  13. /**
  14. * Represents the operation object.
  15. */
  16. protected Operation operation;
  17. /**
  18. * Represents the addition operation.
  19. */
  20. public static final String ADDITION_OPERATION_KEY = "+";
  21. /**
  22. * Represents the subtraction operation.
  23. */
  24. public static final String SUBTRATION_OPERATION_KEY = "-";
  25. /**
  26. * Represents the factorial operation.
  27. */
  28. public static final String FACTORIAL_OPERATION_KEY = "!";
  29.  
  30. /**
  31. * The different types of operations.
  32. */
  33. public static enum OperationType
  34. {
  35. /**
  36. * Addition operation.
  37. */
  38. ADDITION,
  39. /**
  40. * Subtraction operation.
  41. */
  42. SUBTRACTION,
  43. /**
  44. * Factorial operation.
  45. */
  46. FACTORIAL;
  47.  
  48. /**
  49. * Check if an operation is binary (takes two operands).
  50. *
  51. * @return true if the operation is binary, false otherwise.
  52. */
  53. public boolean isBinary()
  54. {
  55. boolean value = false;
  56. if(OperationFactory.OperationType.ADDITION.equals(this))
  57. {
  58. value = true;
  59. //return true;
  60. }
  61. else if(OperationFactory.OperationType.SUBTRACTION.equals(this))
  62. {
  63. value = true;
  64. //return true;
  65. }
  66. else if(OperationFactory.OperationType.FACTORIAL.equals(this))
  67. {
  68. value = false;
  69. //return false;
  70. }
  71. return value;
  72. }
  73.  
  74. /**
  75. * Check if an operation is unary (takes one operand).
  76. *
  77. * @return true if the operation is unary, false otherwise.
  78. */
  79. public boolean isUnary()
  80. {
  81. boolean value = false;
  82. if(OperationFactory.OperationType.ADDITION.equals(this))
  83. {
  84. value = false;
  85. }
  86. if(OperationFactory.OperationType.SUBTRACTION.equals(this))
  87. {
  88. value = false;
  89. }
  90. if(OperationFactory.OperationType.FACTORIAL.equals(this))
  91. {
  92. value = true;
  93. }
  94. return value;
  95. }
  96.  
  97. /**
  98. * Convert a string to an OperationType.
  99. *
  100. * @param key the string to base the value on.
  101. * @return the operation type associated with the key
  102. * @throws InvalidOperationException if the key is not recognized.
  103. */
  104. public static OperationType fromString(final String key)
  105. throws InvalidOperationException
  106. {
  107. if(key == null)
  108. {
  109. throw new IllegalArgumentException("key cannot be " + key);
  110. }
  111. else if(key.equals(SUBTRATION_OPERATION_KEY))
  112. {
  113. return SUBTRACTION;
  114. }
  115. else if(key.equals(ADDITION_OPERATION_KEY))
  116. {
  117. return ADDITION;
  118. }
  119. else if(key.equals(FACTORIAL_OPERATION_KEY))
  120. {
  121. return FACTORIAL;
  122. }
  123.  
  124. throw new InvalidOperationException(key);
  125.  
  126. }
  127. }
  128.  
  129. /**
  130. * Get an instance of an Operation based in the type.
  131. *
  132. * @param type the type of operation to get.
  133. * @return an instance of the operation represented by the type.
  134. */
  135. public Operation getInstance(final OperationType type)
  136. {
  137. if(type == null)
  138. {
  139. throw new IllegalArgumentException("type cannot be null");
  140. }
  141. if(type == OperationType.ADDITION)
  142. {
  143. operation = new AdditionOperation();
  144. }
  145. else if(type == OperationType.FACTORIAL)
  146. {
  147. operation = new FactorialOperation();
  148. }
  149. else if(type == OperationType.SUBTRACTION)
  150. {
  151. operation = new SubtractionOperation();
  152. }
  153. return operation;
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement