I am starting to create chart classes. Necessity obliges me to make a good model.
Solution for math quiz. The system should generate every problem and check the answer.
I am going to show my general classes:
There are ticket offices here
interface IProblemFactory<T> where T : Problem<T> { T Create(); } public class ArithmeticProblemFactory : IProblemFactory<Arithmetic> {
Then I have classes that contain the problem:
public abstract class Problem<T> : IEquatable<T> { public abstract int ResultCount { get; } public abstract bool CheckTheAnswer(); protected abstract bool CheckTheAnswer(params object[] results); public abstract bool Equals(T other); } public class Arithmetic : Problem<Arithmetic> { public decimal Number1 { get; set; } public Operations Operation { get; set; } public decimal Number2 { get; set; } public override int ResultCount { get { return 1; } } protected override bool CheckTheAnswer(params object[] results) { if (results.Length != ResultCount) throw new ArgumentException("Only expected " + ResultCount + " arguments."); decimal result = (decimal)results[0]; switch (Operation) { case Operations.Addition: return Number1 + Number2 == result; case Operations.Subtraction: return Number1 - Number2 == result; case Operations.Multiplication: return Number1 * Number2 == result; case Operations.Division: return Number1 / Number2 == result; default: throw new Exception("Operator unexpected"); } } public override bool Equals(Arithmetic other) { if (other == null) return false; return this.Number1 == other.Number1 && Number2 == other.Number2; } }
The problem is, I think I'm not doing a good design. Because all problems will contain CheckTheAnswer (params obj ..). But all problems have different results.
For example, in arithmetic only a decimal value is required, in comparison 2 I need to save two values, for others I need to save the result as a fraction class.
Maybe I need to separate them. Arithmetic can contain only two properties: the problem and the answer, but I'm not sure.
source share