₦airaland Forum

Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,210,716 members, 4,825,328 topics. Date: Wednesday, 20 March 2019 at 06:32 PM

Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD (3562 Views)

[php Tutorial] - Building A Rest Api / Laravel Tutorial: Building An E-commerce Web App - Part1 / Creating UWP Application In C# Or C++ (xaml) (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 1:43pm On Aug 07, 2015
i'm sorry i haven't updated and conclude this simple tut.

Lost my phone. (my hotspot lolz). I would torrent all the remain updates when i secure a good internet source.

thanks for your contributions and followership
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:25am On Aug 10, 2015
Finally.. Back online
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:28am On Aug 10, 2015
I noted a Veeeeery BIG logical error in the Whole App.... I feel very ashamed wink... But why can't you guys point it out in the first Place...

Calculations for Addition and the rest are totally wrong !!!!....
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 11:30am On Aug 10, 2015
Check My Posts Again...Updated the Posts
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 1:43pm On Aug 10, 2015
BEGINNING TESTS: TESTING ADDITION OF ORDINARY FRACTIONS 2
The next Test Would be straight forward.
We’d be leveraging on C#’s operator Overloading support to create a nice addition interface.
Let’s write the Test !!!.
Update the Test Method testOrdinaryFractionsAdditionsUsingOpOverloading to


[TestMethod]
public void testOrdinaryFractionsAdditionsUsingOpOverloading()
{

Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction + secondFraction;

Assert.AreEqual(75, result.Numerator);
Assert.AreEqual(90, result.Denominator);

}

Notice the Red wiggly under firstFraction + secondFraction and the message : Operator '+' cannot be applied to operands of type 'FractionLibrary.Fraction' and 'FractionLibrary.Fraction';



C# allow us to overload the + operator when use it on our class. This is done by creating method named operator followed by the + sign.
Let’s Implement it to just [b] pass this test.
Add this method to [b] Fraction.cs
class

public static Fraction operator +(Fraction firstFraction, Fraction secondFraction)
{
return new Fraction(75, 90);
}

Weird Method Huh? You can Learn More about Operator Overloading via Google or Books.
Notice the Red wiggly line is gone.
Let’s Run the Test Again
Passed !!!


Now Let’s make how test fail so that we can compute the values.
Update the Test Method testOrdinaryFractionsAdditionsUsingOpOverloading

[TestMethod]
public void testOrdinaryFractionsAdditionsUsingOpOverloading()
{

Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction + secondFraction;

Assert.AreEqual(75, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction + secondFraction;

Assert.AreEqual(190, result.Numerator);
Assert.AreEqual(200, result.Denominator);

}

Run the Tests..
It Should Fail, With the Message : Expected<190> .Actual<75>

Now Let’s Make it Pass By Updating the Overload Method to do actual Calculation.

public static Fraction operator +(Fraction firstFraction, Fraction secondFraction)
{
int newNumerator = (firstFraction.Numerator * secondFraction.Denominator) +
(secondFraction.Numerator * firstFraction.Denominator);
int newDenominator = firstFraction.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);
}


Save and Run the test File Again.
Tests should Pass


FractionArithmeticTests.cs

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using FractionLibrary;

namespace FractionTest
{
[TestClass]
public class FractionArithmeticTests
{

[TestMethod]
public void testOrdinaryFractionCreation()
{
Fraction firstFraction = new Fraction();

Assert.AreEqual(0, firstFraction.Numerator);
Assert.AreEqual(0, firstFraction.Denominator);

firstFraction = new Fraction(5,10);
Assert.AreEqual(5, firstFraction.Numerator);
Assert.AreEqual(10, firstFraction.Denominator);

firstFraction = new Fraction(10, 5);
Assert.AreEqual(10, firstFraction.Numerator);
Assert.AreEqual(5, firstFraction.Denominator);

try{

firstFraction = new Fraction(0, 1);
Assert.Fail("Shouldn't be able to Create a Fraction with Zero Numerator"wink;
}
catch (ArgumentOutOfRangeException ex){}

try
{

firstFraction = new Fraction(1, 0);
Assert.Fail("Shouldn't be able to Create a Fraction with Zero Denominator"wink;
}
catch (ArgumentOutOfRangeException ex) { }


}

[TestMethod]
public void testOrdinaryFractionsAdditionsUsingMethod()
{
Fraction firstFraction = new Fraction(5,10);
Fraction secondFraction = new Fraction(3,9);
Fraction result = firstFraction.Add(secondFraction);

Assert.AreEqual(75, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction.Add(secondFraction);

Assert.AreEqual(190, result.Numerator);
Assert.AreEqual(200, result.Denominator);


}

[TestMethod]
public void testOrdinaryFractionsAdditionsUsingOpOverloading()
{

Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction + secondFraction;

Assert.AreEqual(75, result.Numerator);
Assert.AreEqual(90, result.Denominator);



}

}
}


Fraction.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FractionLibrary
{
public class Fraction
{

public Fraction(int numerator, int denominator)
{
// TODO: Complete member initialization
if (numerator == 0) { throw new ArgumentOutOfRangeException("Numerator Can't be Zero"wink; }
if (denominator == 0) { throw new ArgumentOutOfRangeException("Denominator Can't be Zero"wink; }

this.Numerator = numerator;
this.Denominator = denominator;
}

public Fraction()
{
// TODO: Complete member initialization
}

public int Numerator
{
get;
set;
}

public int Denominator
{
get;
set;

}

public Fraction Add(Fraction secondFraction)
{
int newNumerator = (this.Numerator * secondFraction.Denominator) +
(secondFraction.Numerator * this.Denominator);
int newDenominator = this.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);

}

public static Fraction operator +(Fraction firstFraction, Fraction secondFraction)
{
int newNumerator = (firstFraction.Numerator * secondFraction.Denominator) +
(secondFraction.Numerator * firstFraction.Denominator);
int newDenominator = firstFraction.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);

}
}
}




FROM THE ABOVE, IMPLEMENTING SUBTRACTION, MULTIPLICATION AND DIVISION WOULD BE VERY EASY SO I WON’T DISCUSS MUCH ON IT BUT WOULD PASTE THE TESTS AND METHODS. I’D ADVISE YOU FOLLOW THE STEPS I FOLLOWED WHILE IMPLEMENTING THE ADDITION OPERATION. FIRST MAKE THE TEST COMPILE BY ADDING A RETURN VALUE THAT WOULD JUST MAKE IT PASS. THEN ADD ANOTHER TEST THAT WOULD MAKE IT FAIL THEN, FINALLY, FIX THE FUNCTION TO MAKE THE THIS PASS.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 1:59pm On Aug 10, 2015
IMPLEMENTING SUBTRACTION, MULTIPLICATION AND DIVISION
TESTS FOR SUBTRACTION

[TestMethod]
public void testOrdinaryFractionsSubstractionUsingMethod()
{
Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction.Substract(secondFraction);

Assert.AreEqual(15, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction.Substract(secondFraction);

Assert.AreEqual(50, result.Numerator);
Assert.AreEqual(200, result.Denominator);


}

[TestMethod]
public void testOrdinaryFractionsSubstractionUsingOpOverloading()
{

Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction - secondFraction;

Assert.AreEqual(15, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction - secondFraction;

Assert.AreEqual(50, result.Numerator);
Assert.AreEqual(200, result.Denominator);

}






METHODS FOR SUBSTRACTION

public Fraction Substract(Fraction secondFraction)
{
int newNumerator = (this.Numerator * secondFraction.Denominator) -
(secondFraction.Numerator * this.Denominator);
int newDenominator = this.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);

}

public static Fraction operator -(Fraction firstFraction, Fraction secondFraction)
{
int newNumerator = (firstFraction.Numerator * secondFraction.Denominator) -
(secondFraction.Numerator * firstFraction.Denominator);
int newDenominator = firstFraction.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);

}

TESTS FOR MULTIPLICATION

[TestMethod]
public void testOrdinaryFractionsMultiplicationUsingMethod()
{
Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction.Multiply(secondFraction);

Assert.AreEqual(15, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction.Multiply(secondFraction);

Assert.AreEqual(42, result.Numerator);
Assert.AreEqual(200, result.Denominator);


}
[TestMethod]
public void testOrdinaryFractionsMultiplicationUsingOpOverloading()
{

Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction * secondFraction;

Assert.AreEqual(15, result.Numerator);
Assert.AreEqual(90, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction * secondFraction;

Assert.AreEqual(42, result.Numerator);
Assert.AreEqual(200, result.Denominator);

}


METHODS FOR MULTIPLICATION

public Fraction Multiply(Fraction secondFraction)
{
int newNumerator = this.Numerator * secondFraction.Numerator;

int newDenominator = this.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);
}

public static Fraction operator *(Fraction firstFraction, Fraction secondFraction)
{
int newNumerator = firstFraction.Numerator * secondFraction.Numerator;

int newDenominator = firstFraction.Denominator * secondFraction.Denominator;
return new Fraction(newNumerator, newDenominator);

}

TESTS FOR DIVISION

[TestMethod]
public void testOrdinaryFractionsDivisionUsingMethod()
{
Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction.Divide(secondFraction);

Assert.AreEqual(45, result.Numerator);
Assert.AreEqual(30, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction.Divide(secondFraction);

Assert.AreEqual(120, result.Numerator);
Assert.AreEqual(70, result.Denominator);


}
[TestMethod]
public void testOrdinaryFractionsDivisionUsingOpOverloading()
{

Fraction firstFraction = new Fraction(5, 10);
Fraction secondFraction = new Fraction(3, 9);
Fraction result = firstFraction / secondFraction;

Assert.AreEqual(45, result.Numerator);
Assert.AreEqual(30, result.Denominator);

firstFraction = new Fraction(6, 10);
secondFraction = new Fraction(7, 20);
result = firstFraction / secondFraction;

Assert.AreEqual(120, result.Numerator);
Assert.AreEqual(70, result.Denominator);

}



METHODS FOR DIVISION


public Fraction Divide(Fraction secondFraction)
{
int newNumerator = this.Numerator * secondFraction.Denominator ;

int newDenominator = this.Denominator * secondFraction.Numerator;
return new Fraction(newNumerator, newDenominator);
}

public static Fraction operator /(Fraction firstFraction, Fraction secondFraction)
{
int newNumerator = firstFraction.Numerator * secondFraction.Denominator;

int newDenominator = firstFraction.Denominator * secondFraction.Numerator;
return new Fraction(newNumerator, newDenominator);

}

Next, Run the tests.
Passed !!!
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:06pm On Aug 10, 2015
IMPLEMETING INCREMENT, DECREMENT, AND SYNATACTIC SUGARS

Incrementing a fraction involves adding One to the fraction. But it is not that straight forward. To add 1 to a fraction we create a fraction with the Numerator and Denominator set to 1 and then we Add the fraction to our fraction. The same goes for decrement but we’ll do subtraction



Ok. Just like Addition, we can implement increment and decrement using method and operator overloading.
Let’s begin by writing our tests in the tests class

[TestMethod]
public void testOrdinaryFractionsIncrementUsingMethod()
{
Fraction firstFraction = new Fraction(1, 2);
Fraction result = firstFraction.Increment();

Assert.AreEqual(3, result.Numerator);
Assert.AreEqual(2, result.Denominator);
}
[TestMethod]
public void testOrdinaryFractionsIncrementUsingOpOverloading()
{

Fraction firstFraction = new Fraction(1, 2);

Fraction result = ++firstFraction;

Assert.AreEqual(3, result.Numerator);
Assert.AreEqual(2, result.Denominator);

}




Our Code Won’t Compile because of the unimplemented method and operator. For the sake of brevity I would implement them directly not just implementing it just to make current test pass BUT you should follow the method I followed earlier by implementing a simple method that would make it pass then add another test that would fail the make it pass.
Let’s implement them in the Fraction class.

public Fraction Increment()
{
Fraction One = new Fraction(1,1);

return One + this;
}

public static Fraction operator ++(Fraction firstFraction)
{
Fraction One = new Fraction(1, 1);

return One + firstFraction;

}




Run the Tests
Passed !!!



Let’s quickly implement Decrement in the tests class
Our Tests for Decrement

[TestMethod]
public void testOrdinaryFractionsDecrementUsingMethod()
{
Fraction firstFraction = new Fraction(3, 2);
Fraction result = firstFraction.Decrement();

Assert.AreEqual(1, result.Numerator);
Assert.AreEqual(2, result.Denominator);
}
[TestMethod]
public void testOrdinaryFractionsDecrementUsingOpOverloading()
{

Fraction firstFraction = new Fraction(3, 2);

Fraction result = --firstFraction;

Assert.AreEqual(1, result.Numerator);
Assert.AreEqual(2, result.Denominator);

}




Our Code Won’t Compile because of the unimplemented method and operator. For the sake of brevity I would implement them directly not just implementing it just to make current test pass.
Let’s implement them in the Fraction class.

public Fraction Decrement()
{
Fraction One = new Fraction(1, 1);

return this - One;
}

public static Fraction operator --(Fraction firstFraction)
{
Fraction One = new Fraction(1, 1);

return firstFraction - One;

}



Run the Tests
Passed !!!

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:13pm On Aug 10, 2015
IMPLEMENTING SYNTACTIC SUGARS

What if we want to achieve something like this



We can implement this by implementing a static implicit cast method on Strings;
To best illustrate this syntactic sugar, let’s first write the test in the tests Class.

[TestMethod]
public void testOrdinaryFractionsStringToOrdinaryFraction()
{

Fraction result = "1/2";

Assert.AreEqual(1, result.Numerator);
Assert.AreEqual(2, result.Denominator);

result = "5/10";

Assert.AreEqual(5, result.Numerator);
Assert.AreEqual(10, result.Denominator);

try
{
result = "1g/2f";
Assert.Fail("Shouldn't be able to Create a Fraction with invalid Values"wink;
}
catch (Exception ex) { }

}




Let’s implement the method in the Fraction Class (NOTE: FOLLOW THE PASS -> FAIL -> PASS METHOD)

public static implicit operator Fraction(String value)
{
string[] v = value.Split('/');
int newNumerator = int.Parse(v[0]);
int newDenominator = int.Parse(v[1]);
return new Fraction(newNumerator, newDenominator);

}




NOTE: the int.Parse method throws an Exception if the input String is not valid.
Run the tests !!!..
They should pass. (NOTE: FOLLOW THE PASS -> FAIL -> PASS METHOD)



Now Let’s implement the ToString method.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:18pm On Aug 10, 2015
IMPLEMENTING THE TOSTRING METHOD

Overriding C#’s ToString method would enable us to define how we want our Fraction class to be displayed as a String.
So that we can achieve the below



Let’s Write the Tests in the tests class.

[TestMethod]
public void testOrdinaryFractionsToString()
{

Fraction result = new Fraction(1,2);

Assert.AreEqual("1/2", result.ToString());

result = "5/10";

Assert.AreEqual("5/10", result.ToString());

}




Let’s implement the method in the Fraction Class (NOTE: FOLLOW THE PASS -> FAIL -> PASS METHOD)

public override string ToString()
{
return string.Format("{0}/{1}", this.Numerator,
this.Denominator);
}




Run the tests !!! …. They should all pass ..




Now Let’s implement the Console App for Our Ordinary Fraction Operations.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:23pm On Aug 10, 2015
Implementing the Console App for Our Ordinary Fraction Operations
Before we can make use of our Fraction Class in the Console App project we need to add a reference to the FractionLibrary project.
1) Right Click On References under FractionConsoleApp project in the Solution Explorer -> Click Add Reference



2) Check FractionLibrary in the list. Click Ok



3) Update Program.cs in FractionConsoleApp[/b]project, adding [b] using FractionLibrary;
Our Program.cs Class now

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FractionLibrary;

namespace FractionConsoleApp
{
class Program
{
static void Main(string[] args)
{

}
}
}

How do we go about implementing the operations……
Lets first write a simple algorithm …
1) Display Welcome Message
2) Display Available Operations
3) Accept Type of Operation to be performed
4) If Operation to be performed is a binary Operation
a. Accept FirstFraction
1) If accepted value is not valid, request for another until correct value inputted
b. Accept SecondFraction
1) If accepted value is not valid, request for another until correct value inputted
c. Perform Selected Operation on two fractions
5) If Operation to be performed is a Unary Operation
a. Accept SingleFraction
1) If accepted value is not valid, request for another until correct value inputted
b. Perform Selected Operation on fraction
6) Output result

1. Display Welcome Message: Let’s create a method named displayWelcomeMessage to display necessary welcome message

public static void displayWelcomeMessage()
{
Console.Write("Welcome to the Most Powerful "wink;
Console.WriteLine("Fractions Calculator in World!!!\n"wink;
}


2. Display Available Operations: We can write a method named displayAvailableOperations to display the Operations

Console.WriteLine(" Available Operations"wink;
Console.WriteLine("======================"wink;
Console.WriteLine("(1). Addition"wink;
Console.WriteLine("(2). Subtraction"wink;
Console.WriteLine("(3). Division"wink;
Console.WriteLine("(4). Multiplication"wink;
Console.WriteLine("(5). Decrement"wink;
Console.WriteLine("(6). Increment"wink;

3. Accept Type of Operation to be performed: We’d write a method named getTypeOfOperationToPerform to accept and return an integer value representing the type of operation the user wants to perform
 
public static int getTypeOfOperationToPerform()
{
int operation = 0;
while (operation == 0)
{
Console.Write("Please, Enter Operation to perform [1-6]: "wink;
String op = Console.ReadLine();
int.TryParse(op, out operation);
if (operation == 0 || operation >= 7)
{
Console.WriteLine("Invalid Input Value"wink;
operation = 0; //Reset
}
}
return operation;

}

4. If Operation to be is a binary Operation: Operations 1 to 4 requires two fractions, we need a condition to check this. So that we can accept two fractions.

if (operation <= 4)
{

}


5. Accept FirstFraction and Accept SecondFraction: Since these two steps are doing the same thing but with a different prompt. we can write a method named getSingleFractionFromUser to accept a fraction from the user which the two methods , getFirstFraction and getSecondFraction, we’re going to create, would call.


private static Fraction getSingleFractionFromUser(){
Fraction result = new Fraction();
bool correctValueAccepted = false;
while (!correctValueAccepted)
{
String acceptedFraction = Console.ReadLine();
try{
result = acceptedFraction;
correctValueAccepted = true;
}catch(Exception e){
Console.WriteLine("Invalid/Unable to Parse Input Fraction"wink;
Console.Write("Please, Enter a Fraction [e.g. 1/2]: "wink;
}
}
return result;
}

private static Fraction getFirstFraction(){
Console.Write("Please, Enter a First Fraction [e.g. 1/2]: "wink;
Fraction FirstFraction = getSingleFractionFromUser();
return FirstFraction;
}
private static Fraction getSecondFraction(){
Console.Write("Please, Enter a Second Fraction [e.g. 1/2]: "wink;
Fraction SecondFraction = getSingleFractionFromUser();
return SecondFraction;
}


6. Perform Selected Operation on two fractions : To Implement this, we would need a condition to Determine which of the 4 binary operations is chosen. So that the need operation is performed. We’d create a method named performBinaryOperation that accepts, as arguments, the operation to be performed and the first and second Fraction. The method then returns the result.

private static Fraction performBinaryOperation(int operation,Fraction firstFraction, Fraction secondFraction)
{
Fraction result;
switch (operation)
{
case 1:
result = firstFraction + secondFraction;
break;
case 2:
result = firstFraction - secondFraction;
break;
case 3:
result = firstFraction / secondFraction;
break;
default: //Case 4
result = firstFraction * secondFraction;
break;
}
return result;
}

7. If Operation to be performed is a Unary Operation: Operations 5 and 6 requires One fraction, we need a condition to check this. So that we can accept One fraction.

if (operation > 4 && operation <= 6)
{

}


8. Accept SingleFraction: Let’s Create a method named getSingleFraction

private static Fraction getSingleFraction()
{
Console.Write("Please, Enter a Fraction [e.g. 1/2]: "wink;
Fraction fraction = getSingleFractionFromUser();
return fraction;
}

9. Perform Selected Operation on fraction: To Implement this, we would need a condition to Determine which of the 2 Unary operations is chosen. So that the need operation is performed. We’d create a method named performUnaryOperation that accepts, as arguments, the operation to be performed and Fraction. The method then returns the result.

private static Fraction performUnaryOperation(int operation, Fraction fraction)
{
Fraction result = new Fraction();
if (operation == 5) result = --fraction;
if (operation == 6) result = ++fraction;
return result;
}

10. Output result: Let’s make this simple

Console.WriteLine("The result is: {0}", result);

PUTTING IT ALL TOGETHER
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FractionLibrary;

namespace FractionConsoleApp
{
class Program
{
static void Main(string[] args)
{
Fraction firstFraction = new Fraction();
Fraction secondFraction = new Fraction();
Fraction result = new Fraction();

displayWelcomeMessage();
displayAvailableOperations();
int operation = getTypeOfOperationToPerform();
if (operation <= 4)
{
firstFraction = getFirstFraction();
secondFraction = getSecondFraction();
result = performBinaryOperation(operation,firstFraction, secondFraction);
}
if (operation > 4 && operation <= 6)
{
firstFraction = getSingleFraction();
result = performUnaryOperation(operation,firstFraction);
}

Console.WriteLine("The result is: {0}", result);
Console.Read();
}

private static Fraction performUnaryOperation(int operation, Fraction fraction)
{
Fraction result = new Fraction();
if (operation == 5) result = --fraction;
if (operation == 6) result = ++fraction;
return result;
}

private static Fraction performBinaryOperation(int operation,Fraction firstFraction, Fraction secondFraction)
{
Fraction result;
switch (operation)
{
case 1:
result = firstFraction + secondFraction;
break;
case 2:
result = firstFraction - secondFraction;
break;
case 3:
result = firstFraction / secondFraction;
break;
default: //Case 4
result = firstFraction * secondFraction;
break;
}
return result;
}

public static void displayWelcomeMessage()
{
Console.Write("Welcome to the Most Powerful "wink;
Console.WriteLine("Fractions Calculator in World !!!\n"wink;
}
public static void displayAvailableOperations()
{
Console.WriteLine(" Available Operations"wink;
Console.WriteLine("======================"wink;
Console.WriteLine("(1). Addition"wink;
Console.WriteLine("(2). Subtraction"wink;
Console.WriteLine("(3). Division"wink;
Console.WriteLine("(4). Multiplication"wink;
Console.WriteLine("(5). Decrement"wink;
Console.WriteLine("(6). Increment"wink;
}

public static int getTypeOfOperationToPerform()
{
int operation = 0;
while (operation == 0)
{
Console.Write("Please, Enter Operation to perform [1-6]: "wink;
String op = Console.ReadLine();
int.TryParse(op, out operation);
if (operation == 0 || operation >= 7)
{
Console.WriteLine("Invalid Input Value"wink;
operation = 0; //Reset
}
}
return operation;

}

private static Fraction getSingleFractionFromUser(){
Fraction result = new Fraction();
bool correctValueAccepted = false;
while (!correctValueAccepted)
{
String acceptedFraction = Console.ReadLine();
try{
result = acceptedFraction;
correctValueAccepted = true;
}catch(Exception e){
Console.WriteLine("Invalid/Unable to Parse Input Fraction"wink;
Console.Write("Please, Enter a Fraction [e.g. 1/2]: "wink;
}
}
return result;
}

private static Fraction getFirstFraction(){
Console.Write("Please, Enter a First Fraction [e.g. 1/2]: "wink;
Fraction FirstFraction = getSingleFractionFromUser();
return FirstFraction;
}
private static Fraction getSecondFraction(){
Console.Write("Please, Enter a Second Fraction [e.g. 1/2]: "wink;
Fraction SecondFraction = getSingleFractionFromUser();
return SecondFraction;
}

private static Fraction getSingleFraction()
{
Console.Write("Please, Enter a Fraction [e.g. 1/2]: "wink;
Fraction fraction = getSingleFractionFromUser();
return fraction;
}

}
}

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 3:28pm On Aug 10, 2015
We'd Implement Mixed Fraction Later...

Your thoughts ?
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by dhtml(m): 5:33pm On Aug 10, 2015
jacob05:
I noted a Veeeeery BIG logical error in the Whole App.... I feel very ashamed wink... But why can't you guys point it out in the first Place...

Calculations for Addition and the rest are totally wrong !!!!....
What makes you think your viewers know how to add and subtract in the first place? So how will they even correct you? Anyway, carry go o jare.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 5:36pm On Aug 10, 2015
dhtml:

What makes you think your viewers know how to add and subtract in the first place? So how will they even correct you? Anyway, carry go o jare.

Lolz.....aba
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 7:58pm On Aug 10, 2015
RUN

Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by Nobody: 7:32pm On Aug 13, 2015
The master-troll is now back full-force(from a trollin vacation) on this thread. I shall be launching my IDE very soon and start testing out all these jagbajantis you call c# codes to see if they are authentic before i move a motion for your banishment from this board!
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 8:20pm On Aug 13, 2015
dhtml18:
The master-troll is now back full-force(from a trollin vacation) on this thread. I shall be launching my IDE very soon and start testing out all these jagbajantis you call c# codes to see if they are authentic before i move a motion for your banishment from this board!
lolz.. Pls do sir !!!.
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by jacob05(m): 9:47pm On Aug 14, 2015
Created a Blog !!!...

You can now view all posts, with syntax highlighting and better layout on my blog.

PART 1: https://naijadeveloper.wordpress.com/2015/08/14/tutorial-part-1-building-a-simple-fraction-arithmetic-program-in-c-using-tdd/
PART 2: https://naijadeveloper.wordpress.com/2015/08/14/tutorial-part-2-building-a-simple-fraction-arithmetic-program-in-c-using-tdd/

would keep updating nairaland's topic. but for "better" reading experience visit the above links
Re: Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD by kapersky(m): 11:52pm On Aug 14, 2015
THERE IS A FOUNDATION IN EVERY THING WE DO IN LIFE,,,,,,, SO FOR YOU TO BE A GOOD PROGRAMMER YOU MUST BEGIN BUILDING THE FOUNDATION NOW..... I MEAN NOW FOR FREE.......

Learn how to begin program using BASIC programming language for Beginners...
WATCH, SHARE AND SUBSCRIBE BELOW
For more information.. Drop your comments or visit www.thecomlovers.blogspot.com for more info..


QBASIC Programming Tutorial – 3 – Running Your First Program



https://www.youtube.com/watch?v=N82rznFP1pc?list=PLJNW2DKDGQplcLdvQ9kJMHBjMKNCx_BPQ




QBASIC Programming Tutorial – 4 – Variables



https://www.youtube.com/watch?v=P3cH7jCKYuE


PLEASE I CANT UPLOAD ALL THE VIDEOS HERE BECAUSE OF ADMIN'S RULES VIOLATION
WATCH THE COMPLETE VIDEO AND CHECK FOR MORE AT.....

https://www.youtube.com/playlist?list=PLJNW2DKDGQplcLdvQ9kJMHBjMKNCx_BPQ

OR VISIT www.thecomlovers.blogspot.com or call 08145655380 for more info.....
DON'T FORGET TO SUBSCRIBE.........

SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........SUBSCRIBE.........

(1) (2) (Reply)

Creating UWP Application In C# Or C++ (xaml) / A Very Simple Loan Calculator In Vb6 Needed / I Need A Good Hacker

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)

Nairaland - Copyright © 2005 - 2019 Oluwaseun Osewa. All rights reserved. See How To Advertise. 210
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.