Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.bcit.cst.comp2526.assign3.a00752818.operations;
- /**
- * Create operations.
- *
- * @author D'Arcy Smith
- * @version 1.0
- */
- public class OperationFactory
- {
- public static OperationType opera;
- /**
- * Represents the operation object.
- */
- protected Operation operation;
- /**
- * Represents the addition operation.
- */
- public static final String ADDITION_OPERATION_KEY = "+";
- /**
- * Represents the subtraction operation.
- */
- public static final String SUBTRATION_OPERATION_KEY = "-";
- /**
- * Represents the factorial operation.
- */
- public static final String FACTORIAL_OPERATION_KEY = "!";
- /**
- * The different types of operations.
- */
- public static enum OperationType
- {
- /**
- * Addition operation.
- */
- ADDITION,
- /**
- * Subtraction operation.
- */
- SUBTRACTION,
- /**
- * Factorial operation.
- */
- FACTORIAL;
- /**
- * Check if an operation is binary (takes two operands).
- *
- * @return true if the operation is binary, false otherwise.
- */
- public boolean isBinary()
- {
- boolean value = false;
- if(OperationFactory.OperationType.ADDITION.equals(this))
- {
- value = true;
- //return true;
- }
- else if(OperationFactory.OperationType.SUBTRACTION.equals(this))
- {
- value = true;
- //return true;
- }
- else if(OperationFactory.OperationType.FACTORIAL.equals(this))
- {
- value = false;
- //return false;
- }
- return value;
- }
- /**
- * Check if an operation is unary (takes one operand).
- *
- * @return true if the operation is unary, false otherwise.
- */
- public boolean isUnary()
- {
- boolean value = false;
- if(OperationFactory.OperationType.ADDITION.equals(this))
- {
- value = false;
- }
- if(OperationFactory.OperationType.SUBTRACTION.equals(this))
- {
- value = false;
- }
- if(OperationFactory.OperationType.FACTORIAL.equals(this))
- {
- value = true;
- }
- return value;
- }
- /**
- * Convert a string to an OperationType.
- *
- * @param key the string to base the value on.
- * @return the operation type associated with the key
- * @throws InvalidOperationException if the key is not recognized.
- */
- public static OperationType fromString(final String key)
- throws InvalidOperationException
- {
- if(key == null)
- {
- throw new IllegalArgumentException("key cannot be " + key);
- }
- else if(key.equals(SUBTRATION_OPERATION_KEY))
- {
- return SUBTRACTION;
- }
- else if(key.equals(ADDITION_OPERATION_KEY))
- {
- return ADDITION;
- }
- else if(key.equals(FACTORIAL_OPERATION_KEY))
- {
- return FACTORIAL;
- }
- throw new InvalidOperationException(key);
- }
- }
- /**
- * Get an instance of an Operation based in the type.
- *
- * @param type the type of operation to get.
- * @return an instance of the operation represented by the type.
- */
- public Operation getInstance(final OperationType type)
- {
- if(type == null)
- {
- throw new IllegalArgumentException("type cannot be null");
- }
- if(type == OperationType.ADDITION)
- {
- operation = new AdditionOperation();
- }
- else if(type == OperationType.FACTORIAL)
- {
- operation = new FactorialOperation();
- }
- else if(type == OperationType.SUBTRACTION)
- {
- operation = new SubtractionOperation();
- }
- return operation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement