Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace operationFactory
- {
- class Program
- {
- static void Main(string[] args)
- {
- Operation oper;
- oper = Factory.createOperation("+");
- oper.NumberA = 100;
- oper.NumberB = 100;
- double result = oper.getResult();
- Console.WriteLine(oper.NumberA.ToString() + " + " + oper.NumberB.ToString() + " = " + result.ToString());
- Console.Read();
- }
- }
- public class Operation
- {
- private double _numA;
- private double _numB;
- public double NumberA
- {
- get { return _numA; }
- set { _numA = value; }
- }
- public double NumberB
- {
- get { return _numB; }
- set { _numB = value; }
- }
- public virtual double getResult()
- {
- return 0;
- }
- }
- class OperationAdd : Operation
- {
- public override double getResult()
- {
- double result = 0;
- result = NumberA + NumberB;
- return result;
- }
- }
- class OperationSub : Operation
- {
- public override double getResult()
- {
- double result = 0;
- result = NumberA - NumberB;
- return result;
- }
- }
- class OperationMul : Operation
- {
- public override double getResult()
- {
- double result = 0;
- result = NumberA * NumberB;
- return result;
- }
- }
- class OperationDiv : Operation
- {
- public override double getResult()
- {
- double result = 0;
- if (NumberB == 0)
- throw new Exception("Error!");
- result = NumberA / NumberB;
- return result;
- }
- }
- public class Factory
- {
- public static Operation createOperation(string operate)
- {
- Operation oper = null;
- switch (operate)
- {
- case "+":
- oper = new OperationAdd();
- break;
- case "-":
- oper = new OperationSub();
- break;
- case "*":
- oper = new OperationMul();
- break;
- case "/":
- oper = new OperationDiv();
- break;
- default:
- throw new Exception("Error!");
- }
- return oper;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment