package com.company.objects;

import java.util.Scanner;

public class FractionMain {
	public static void main(String[] args) {
		// declarations
		Scanner input = new Scanner(System.in);
		String userInput;
		int userChoice, userNum;
		char userSubChoice;
		Fraction harmonicSum = new Fraction(), eX = new Fraction(), frac;
		long[] fraction, mixed;
		
		do {
			// main menu
			System.out.println("\\nMAIN MENU");
			System.out.println("0 - Exit");
			System.out.println("1 - Find the harmonic sum of a number                                      x"); // x is for the superscript for option 2
			System.out.println("2 - Approximate e to the power of a number using the Maclaurin Series for e");
			System.out.println("3 - Convert improper fraction to mixed number");
			System.out.println("4 - Convert mixed number to improper fraction");
			System.out.println("5 - See the simplified form of a fraction");
			System.out.println("6 - Fraction calculator");
			System.out.println("7 - Convert decimal to fraction");
			System.out.println("8 - Sort a list of fractions from least to greatest"); // DO LATER
			System.out.print("Enter your choice: ");
			userInput = input.nextLine();
			userChoice = makeInt(userInput);
			
			// corresponding choices
			if (userChoice == 1) {
				System.out.println("\\nHARMONIC SUM");
				
				do {
					System.out.print("Enter a natural number to find its harmonic sum: ");
					userInput = input.nextLine();
					userNum = makeInt(userInput); // convert user's input to an int if possible (-1 if the input was not a whole number)
					
					if (userNum != -1 && userNum != 0) { // if the input is a natural number, then display the harmonic sum by calling the method
						harmonicSum = calcHarmonicSum(userNum);
					} else if (userNum == -1 && userInput.length() > 0 && userInput.charAt(0) == '-' && makeInt(userInput.substring(1)) != -1) { // if the user entered a number but it was not positive, then user its absolute value instead
						userNum = makeInt(userInput.substring(1));
						
						if (userNum > 0) { // make sure the user's number is not 0
							System.out.println("Although you inputted a negative number, here is the harmonic sum of its absolute value.");
							harmonicSum = calcHarmonicSum((int) Math.abs(userNum)); // display the harmonic sum of the absolute value of the user's number
						}
					}
					// tell user to try again
					if (userNum == -1 || userNum == 0) {
						System.out.println("Remember, it must be a natural number (positive integer). Try again.");
					}
				} while (userNum == -1 || userNum == 0);

			} else if (userChoice == 2) {
				System.out.println("                      x"); // to imitate superscript for e^x in the console
				System.out.println("MACLAURIN SERIES FOR e");
				
				do {
					// get user input & convert to integer
					System.out.print("Enter an integer, x, to find the approximation of e to the power of x: ");
					userInput = input.nextLine();
					userNum = makeInt(userInput);
					
					if (userNum != -1) { // whole number
						eX = calcMaclaurinSeries(userNum);
						
					} else if (userNum == -1 && userInput.length() > 0 && userInput.charAt(0) == '-' && makeInt(userInput.substring(1)) != -1) { // negative integer
						userNum = -makeInt(userInput.substring(1));
						
						eX = calcMaclaurinSeries(userNum);
					} else { // invalid input message
						System.out.println("Remember, it must be an integer. Try again.");
					}
				} while (userNum == -1);
				
			} else if (userChoice == 3) {
				System.out.println("\\nIMPROPER FRACTION --> MIXED NUMBER");
				
				// initialize fraction array
				fraction = new long[2];
				
				// display options and make user choose
				do {
					// options
					System.out.println("A - Convert the harmonic sum to mixed number        x");
					System.out.println("B - Convert the Maclaurin Series approximation of e  to mixed number");
					System.out.println("C - Convert a new fraction to a mixed number");
					System.out.print("Enter your choice: ");
					userInput = input.nextLine();
					
					// convert to correct char
					if (userInput.length() != 1) {
						userSubChoice = 'z';
					} else {
						userSubChoice = userInput.toUpperCase().charAt(0);
					}
					
					// invalid message
					if (userSubChoice < 65 || userSubChoice > 67) {
						System.out.println("That is invalid. Try again.");
					}
					
				} while (userSubChoice < 65 || userSubChoice > 67); // keeps looping while it isn't A, B, or C
				
				if (userSubChoice == 'A') { // harmonic sum
					fraction[0] = harmonicSum.getNumerator();
					fraction[1] = harmonicSum.getDenominator();
				} else if (userSubChoice == 'B') { // e^x
					fraction[0] = eX.getNumerator();
					fraction[1] = eX.getDenominator();
				} else { // user fraction
					do {
						System.out.print("Enter a fraction to display as a mixed number: ");
						fraction = checkValidFraction(input.nextLine());
					} while (fraction[1] == 0); // while invalid fraction (aka denominator = 0)
				}
				
				// construct frac
				frac = new Fraction(fraction[0], fraction[1]);
				
				// convert to mixed number & display
				displayConvertToMixed(frac);
				
			} else if (userChoice == 4) {
				System.out.println("\\nMIXED NUMBER --> IMPROPER FRACTION");
				
				// initialize mixed array
				mixed = new long[3];
				
				// get input
				do {
					System.out.print("Enter a mixed number to display as an improper fraction (e.g. 2 5/13): ");
					mixed = checkValidMixed(input.nextLine().trim()); // remove spaces from start & end to help with checking for valid mixed
				} while (mixed[2] == 0); // denominator = 0 --> invalid input
				
				// construct frac
				frac = new Fraction(mixed[0], mixed[1], mixed[2]); // whole number, numerator, denominator
				
				// convert to improper & display
				displayConvertToImproper(frac);
				
			} else if (userChoice == 5) {
				System.out.println("\\nFRACTION IN SIMPLEST FORM");
				
				// initialize fraction arr
				fraction = new long[2];
				
				// get input
				do {
					System.out.print("Enter a fraction to simplify: ");
					fraction = checkValidFraction(input.nextLine());
				} while (fraction[1] == 0); // keep looping until a valid fraction is entered
				
				// construct frac & simplify it
				frac = new Fraction(fraction[0], fraction[1]);
				displaySimplifyFraction(frac);
				
			} else if (userChoice == 6) {
				System.out.println("\\nFRACTION CALCULATOR");
				
				// get valid input
				do {
					System.out.print("Enter an expression (use +, _, *, |): "); // subtraction & division operators are different to avoid confusion with negatives and fraction bars
					userInput = checkValidExpression(input.nextLine());
					
					if (userInput.equals("invalid")) {
						System.out.println("Invalid expression. Try again.");
					}
					
				} while (userInput.equals("invalid")); // keep asking for a valid expression
				
				calculate(userInput);
				
			} else if (userChoice == 7) {
				System.out.println("\\nDECIMAL TO FRACTION");
				
				// construct the fraction object
				frac = new Fraction();
				
				do {
					System.out.print("Enter a decimal: ");
					userInput = input.nextLine();
					
				} while (userInput.replaceAll("\\\\.", "").length() + 1 < userInput.length()); // if more than 1 decimal
				
				frac.decimalToFrac(userInput);
				
				displaySimplifyFraction(frac);
				
			} else if (userChoice == 8) {
				System.out.println("\\nSORT ARRAY OF FRACTIONS");
				
				
			}
			
			else if (userChoice != 0) { // input was either not an integer or not an option
				System.out.println("Invalid input. Please try again.");
			}
			
			System.out.println();
			
		} while (userChoice != 0);
		
		// farewell message
		System.out.println("\\nGoodbye!");
	}
	
	/*
	 * Calculates and shows the steps to getting the harmonic sum and displays it to the user. Also returns the harmonic sum as a Fraction object
	 * Pre: int representing the user's input, user must have chosen option 1 in the main menu
	 * Post: displays the terms of the sequence and the final harmonic sum, returns the harmonic sum as a Fraction object
	 */
	public static Fraction calcHarmonicSum(int n) {
		// declarations and initializations
		Fraction harmonicSum = new Fraction();
		Fraction currentFrac;
		String[][] fractionArr = new String[n + 1][3];
		
		// adding the fractions
		for (int i = 1; i <= n; i++) {
			currentFrac = new Fraction(1, i); // construct currentFrac
			harmonicSum.add(currentFrac); // add the current fraction to the sum
			fractionArr[i - 1] = currentFrac.formatImproper(); // format it for output later
		}
		
		// format the harmonic sum
		fractionArr[n] = harmonicSum.formatImproper();
		
		// output; go through every numerator first, then fraction bars, then denominators
		for (int j = 0; j < fractionArr[0].length; j++) {
			for (int i = 0; i < fractionArr.length; i++) {
				
				if (i == fractionArr.length - 1) { // if we are trying to print a part of the harmonic sum, then nothing comes after
					System.out.println(fractionArr[i][j]);
					
				} else if (i == fractionArr.length - 2 && j == 1) { // if we are printing the fraction bar of the last term, it must be followed by =
					System.out.print(fractionArr[i][j] + " = ");
					
				} else if (j != 1) {
					System.out.print(fractionArr[i][j] + "   "); // if we are printing the numerator or denominator of a term
					
				} else {
					System.out.print(fractionArr[i][j] + " + "); // if we are printing the fraction bar of a term, then we must also add a + to show that we are adding
				}
			}
		}
		
		// decimal output
		System.out.println("\\nAs a decimal, the harmonic sum of " + n + " is " + harmonicSum.calcDecimal());
		
		return(harmonicSum);
	}
	
	/*
	 * Calculates the approximation for e^x given an x value, displays it to the user, and returns the fraction object.
	 * Pre: int for x, user must have chosen option 2 from the main menu
	 * Post: displays the approximation for e^x, returns a Fraction object
	 */
	public static Fraction calcMaclaurinSeries(int x) {
		// declarations & initializations
		String[] fractionArr = new String[3];
		Fraction sum = new Fraction();
		Fraction currentFrac;
		
		// calculating
		for (int i = 0; i < 13; i++) { // the more iterations of the loop, the lower the number the user can input for x because a long is only 64 bits (-2^61 to 2^61-1) so after that, the fraction won't be accurate. But, this also means the approximation will not be as close given larger x values
			currentFrac = new Fraction((int) Math.pow(x, i), calcFactorial(i)); // maclaurin series for e^x
			
			currentFrac.simplify(); // reduce the fraction to lowest terms to try and prevent long overflow

			sum.add(currentFrac);
			
			sum.simplify(); // reduce the fraction to lowest terms to try and prevent long overflow

		}
		
		fractionArr = sum.formatImproper();
		
		// output
		System.out.println(" " + x + getSpaces(" is approximately equal to: ".length()) + fractionArr[0]); // x is to act as the superscript of e^x
		System.out.println("e" + getSpaces((x + "").length()) + " is approximately equal to: " + fractionArr[1]);
		System.out.println(getSpaces(1 + (x + "").length() + " is approximately equal to: ".length()) + fractionArr[2]);
		
		// decimal output
		System.out.println("\\nAs a decimal, it is approximately " + sum.calcDecimal());
		
		return(sum);
	}
	
	/*
	 * Displays the equivalent mixed number given an improper fraction
	 * Pre: Fraction object, user must have chosen option 3 from the main menu
	 * Post: returns nothing but displays the mixed number to the user
	 */
	public static void displayConvertToMixed(Fraction frac) {
		// declaration & initialization
		String[][] fractionArr = new String[2][3]; // fractionArr[0] will be the improper fraction and fractionArr[1] will have part of the fraction part of the mixed number
		
		fractionArr[0] = frac.formatImproper(); // format improper fraction
		
		// get the mixed number values of the fraction
		frac.improperToMixed();
		
		fractionArr[1] = frac.formatMixed(); // format mixed number
		
		System.out.println();
				
		// output
		if (frac.getWholeNumber() == 0) { // normal fraction
			System.out.println(fractionArr[0][0] + getSpaces(" as a mixed number is equal to: ".length())+ fractionArr[1][0]); // numerators
			System.out.println(fractionArr[0][1] + " as a mixed number is equal to: " + fractionArr[1][1]); // fraction bars & words
			System.out.println(fractionArr[0][2] + getSpaces(" as a mixed number is equal to: ".length()) + fractionArr[1][2]); // denominators
			
		} else if (frac.getDenominator() == 1) { // whole number
			System.out.println(fractionArr[0][0]); // numerators
			System.out.println(fractionArr[0][1] + " as a mixed number is equal to: " + frac.getWholeNumber()); // words and whole number of mixed number
			System.out.println(fractionArr[0][2]); // denominators
			
		} else {
			System.out.println(fractionArr[0][0] + getSpaces(" as a mixed number is equal to: ".length() + (frac.getWholeNumber() + "").length() + 1) + fractionArr[1][0]); // numerators
			System.out.println(fractionArr[0][1] + " as a mixed number is equal to: " + frac.getWholeNumber() + " " + fractionArr[1][1]); // fraction bars & words, and whole number of mixed number
			System.out.println(fractionArr[0][2] + getSpaces(" as a mixed number is equal to: ".length() + (frac.getWholeNumber() + "").length() + 1) + fractionArr[1][2]); // denominators
		}

	}
	
	/*
	 * Displays the equivalent improper fraction given a mixed number
	 * Pre: Fraction object, user must have chosen option 4 from the main menu
	 * Post: returns nothing but displays the improper fraction to the user
	 */
	public static void displayConvertToImproper(Fraction frac) {
		// declaration
		String[][] fractionArr = new String[2][3];
		boolean alreadyImproper = true;
		
		// initializing/assigning values
		fractionArr[0] = frac.formatMixed();
				
		if (frac.getWholeNumber() != 0) {
			alreadyImproper = false; // the fraction given is mixed
			fractionArr[0][0] = getSpaces((frac.getWholeNumber() + "").length() + 1) + fractionArr[0][0];
			fractionArr[0][1] = frac.getWholeNumber() + " " + fractionArr[0][1];
			fractionArr[0][2] = getSpaces((frac.getWholeNumber() + "").length() + 1) + fractionArr[0][2];
			
		} else if (frac.getDenominator() == 1) { // no need to show denominator
			fractionArr[0][0] = getSpaces((frac.getMixedNumerator() + "").length());
			fractionArr[0][1] = frac.getMixedNumerator() + "";
			fractionArr[0][2] = getSpaces((frac.getMixedNumerator() + "").length());
		}
		
		frac.mixedToImproper();
		
		fractionArr[1] = frac.formatImproper();
		
		System.out.println();
		
		// output
		if (alreadyImproper) { // the whole number was always 0 (even before converting)
			System.out.println(fractionArr[1][0]);
			System.out.println(fractionArr[1][1] + " is already an improper fraction!");
			System.out.println(fractionArr[1][2]);
		} else { // regular case
			System.out.println(fractionArr[0][0] + getSpaces(" as an improper fraction is equal to: ".length()) + fractionArr[1][0]);
			System.out.println(fractionArr[0][1] + " as an improper fraction is equal to: " + fractionArr[1][1]);
			System.out.println(fractionArr[0][2] + getSpaces(" as an improper fraction is equal to: ".length()) + fractionArr[1][2]);
		}
	}
	
	/*
	 * Displays the simplified fraction
	 * Pre: Fraction object, user must have chosen option 5 in the main menu
	 * Post: doesn't return anything but displays the formatted, simplified fraction
	 */
	public static void displaySimplifyFraction(Fraction frac) {
		// declaration & initialization
		String[] fractionArr = new String[3];
		
		frac.simplify(); // simplify fraction if possible
		
		fractionArr = frac.formatImproper();
		
		// output
		if (frac.getDenominator() == 1) {
			System.out.println(fractionArr[0]); // only display the numerator if the denominator is 1
		} else {
			for (int i = 0; i < fractionArr.length; i++) { // display full fraction
				System.out.println(fractionArr[i]);
			}
		}

	}
	
	/*
	 * Calculates the expression and displays the final fraction
	 * Pre: String
	 * Post: returns nothing but displays the final fraction
	 */
	public static void calculate(String expression) {
		// declarations
		int previousOperator, nextOperator;
		boolean hasDM = false; // BEDMAS --> DM = division/multiplication
		Fraction frac;
		long[] fractionArr;
		
		// calculate the expression
		for (int i = 0; i < expression.length(); i++) {
			if (expression.charAt(i) == '*') {
				expression = evaluate(expression, i, '*');
				i = -1; // restart from the beginning of the expression
				
			} else if (expression.charAt(i) == '|') {
				expression = evaluate(expression, i, '|');
				i = -1;
				
			} else if (i == expression.length() - 1 && !hasDM) {
				hasDM = true; // lets the program know that addition/subtraction can occur now
				i = -1;
				
			} else if (hasDM && expression.charAt(i) == '+') {
				expression = evaluate(expression, i, '+');
				i = -1;
				
			} else if (hasDM && expression.charAt(i) == '_') {
				expression = evaluate(expression, i, '_');
				i = -1;
				
			} 

		}
		
		// format and output
		fractionArr = checkValidFraction(expression);
		frac = new Fraction(fractionArr[0], fractionArr[1]);
		displaySimplifyFraction(frac);
	}
	
	/*
	 * Evaluates the binomial according to the operator given
	 * Pre: String expression, int (index), char operator
	 * Post: returns a string
	 */
	public static String evaluate(String expression, int i, char operator) {
		Fraction frac1, frac2;
		int previousOperator, nextOperator;
		long[] fraction;
		
		// find the start and end of the binomial
		previousOperator = findPreviousOperator(expression, i);
		nextOperator = findNextOperator(expression, i);
		
		
		// make sure the terms before and after the operator are valid
		fraction = checkValidFraction(expression.substring(previousOperator + 1, i)); // get numerator & denominator
		frac1 = new Fraction(fraction[0], fraction[1]); // create fraction
		
		fraction = checkValidFraction(expression.substring(i + 1, nextOperator));
		frac2 = new Fraction(fraction[0], fraction[1]); // create fraction

		// evaluate accordingly
		if (operator == '*') {
			frac1.multiply(frac2);
		} else if (operator == '|') {
			frac1.divide(frac2);
		} else if (operator == '+') {
			frac1.add(frac2);
		} else {
			frac1.subtract(frac2);
		}
		
		// replace the two terms with the answer and return
		return(expression.substring(0, previousOperator + 1) + frac1 + expression.substring(nextOperator));
	}
	
	/*
	 * Finds the nearest operator (+, -, *, |) BEFORE the index given
	 * Pre: String, int
	 * Post: returns an int
	 */
	public static int findPreviousOperator(String expression, int index) {
		// find & return the index of the operator that comes before the index given
		
		for (int i = index - 1; i > 0; i--) {
			if (expression.charAt(i) == '+' || expression.charAt(i) == '_' || expression.charAt(i) == '*' || expression.charAt(i) == '|') {
				return(i);
			}
		}
		
		return(-1);
	}
	
	/*
	 * Finds the nearest operator AFTER the index given
	 * Pre: String, int
	 * Post: returns an int
	 */
	public static int findNextOperator(String expression, int index) {
		// find and return the index of the operator that comes after the index given
		for (int i = index + 1; i < expression.length(); i++) {
			
			if (expression.charAt(i) == '+' || expression.charAt(i) == '_' || expression.charAt(i) == '*' || expression.charAt(i) == '|') {
				return(i);
			}
		}
		
		return(expression.length());
	}
	
	/*
	 * Returns a string containing a valid expression or "invalid" 
	 * Pre: String
	 * Post: returns a formatted String
	 */
	public static String checkValidExpression(String potential) {
		// declarations & initializations
		potential = potential.replaceAll(" ", ""); // remove spaces
		int nextOperator = -1;
		
		for (int i = 0; i < potential.length(); i++) {
			// look for the next operator 
			nextOperator = findNextOperator(potential, i);
			
			// see if the term is valid
			if (checkValidFraction(potential.substring(i, nextOperator))[1] == 0) { // denominator = 0, meaning invalid fraction
				return("invalid");
			}
			
			i = nextOperator; // check next term in the next iteration
		} 
		
		// return formatted (spaces removed) valid expression
		return(potential);
	}
	
	/*
	 * Checks if the given String contains a valid fraction (numerator/denominator)
	 * Pre: String
	 * Post: returns a long array of length 2 with the first element containing the numerator and the second containing the denominator (denominator = 0 if the fraction given is invalid)
	 */
	public static long[] checkValidFraction(String potentialFrac) {
		// declarations & initialization
		String potentialN, potentialD;
		int slashIndex = -1;
		long[] frac = new long[2];
		
		potentialFrac = potentialFrac.replaceAll(" ", ""); // remove all spaces
		
		// see how many /s and where
		for (int i = 0; i < potentialFrac.length(); i++) {
			if (potentialFrac.charAt(i) == '/' && slashIndex == -1 && i + 1 != potentialFrac.length()) {
				slashIndex = i;
				frac[1] = 1;
			} else if (potentialFrac.charAt(i) == '/') { // string has more than 1 slash or the slash is at the end, making it an invalid fraction for our purposes
				frac[1] = 0;
				return(frac);
			}
		}
		
		// if there was no slash (potential integer)
		if (slashIndex == -1) {
			frac[1] = 1;
			if (makeInt(potentialFrac) != -1) {
				frac[0] = makeInt(potentialFrac);
			} else if (potentialFrac.length() > 0 && potentialFrac.charAt(0) == '-' && makeInt(potentialFrac.substring(1)) != -1) {
				frac[0] = -makeInt(potentialFrac.substring(1));
			} else {
				frac[1] = 0;
			}
			return(frac);
		}
		
		// initialize the potential numerator & denominator
		potentialN = potentialFrac.substring(0, slashIndex);
		potentialD = potentialFrac.substring(slashIndex + 1);
		
		if (makeInt(potentialN) != -1) {
			frac[0] = makeInt(potentialN);
		} else if (potentialN.length() > 0 && potentialN.charAt(0) == '-' && makeInt(potentialN.substring(1)) != -1) {
			frac[0] = -makeInt(potentialN.substring(1));
		} else {
			frac[1] = 0;
			return(frac); // means that the fraction is invalid since the denominator is 0
		}
		
		if (makeInt(potentialD) != -1 && makeInt(potentialD) != 0) { // make sure that not only is denominator an integer but it also isn't 0
			frac[1] = makeInt(potentialD);
		} else if (potentialD.length() > 0 && potentialD.charAt(0) == '-' && makeInt(potentialD.substring(1)) != -1 && makeInt(potentialD.substring(1)) != 0) {
			frac[1] = -makeInt(potentialD.substring(1));
		} else {
			frac[1] = 0;
			return(frac); // means that the fraction is invalid
		}

		return(frac);
		
		
	}
	
	/*
	 * Checks if the given String contains a valid mixed number (wholeNum numerator/denominator)
	 * Pre: String
	 * Post: returns a long array of length 3 with the whole number, numerator, and denominator, respectively (if denominator is 0, then the number is invalid)
	 */
	public static long[] checkValidMixed(String potentialMixed) {
		// declarations & initializations
		long[] mixed = new long[3], frac = new long[2];
		int spaceStart, spaceEnd = -1, slashIndex;
		String potentialWhole, potentialFrac;
		
		// Check that there is a space section (separates whole vs. fraction) & it comes before /, if there are any
		spaceStart = potentialMixed.indexOf(" ");
		slashIndex = potentialMixed.indexOf("/");
		
		if (slashIndex != -1 && slashIndex < spaceStart) {
			return(mixed); // denominator will be 0 to signal invalid mixed
		} else if (spaceStart == -1) {
			potentialFrac = potentialMixed; // no space
		} else {
			// find spaceEnd
			for (int i = spaceStart; i < potentialMixed.length(); i++) {
				if (potentialMixed.charAt(i) != ' ') {
					spaceEnd = i - 1;
					i = potentialMixed.length(); // exit the loop once space section ends
				}
			}
			
			// initialize strings
			potentialWhole = potentialMixed.substring(0, spaceStart);
			potentialFrac = potentialMixed.substring(spaceEnd + 1);
			
			// convert to int
			// whole part
			if (makeInt(potentialWhole) != -1) {
				mixed[0] = makeInt(potentialWhole);
			} else if (potentialWhole.length() > 0 && potentialWhole.charAt(0) == '-' && makeInt(potentialWhole.substring(1)) != -1) {
				mixed[0] = -makeInt(potentialWhole.substring(1));
			} else {
				return(mixed); // not an int
			}
		}

		// fraction part
		frac = checkValidFraction(potentialFrac);
		mixed[1] = frac[0];
		mixed[2] = frac[1]; // will indicate whether or not the fraction & mixed number were valid
		
		return(mixed);

	}
	
	/*
	 * Calculates the factorial of a given integer
	 * Pre: int
	 * Post: returns an int
	 */
	public static int calcFactorial(int num) {
		int factorial = 1;
		
		for (int i = num; i > 1; i--) {
			factorial *= i;
		}
		
		return(factorial);
	}
	
	/*
	 * Returns a string with the specified number of spaces
	 * Pre: int representing the number of spaces
	 * Post: returns a string containing spaces
	 */
	public static String getSpaces(int numSpaces) {
		// declare & initialize an empty string to be returned at the end
		String returnStr = "";
		
		// add spaces to the return string
		for (int i = 0; i < numSpaces; i++) {
			returnStr += " ";
		}
		
		return(returnStr);
	}
	
	/*
	 * Converts the given string into an integer
	 * Pre: string parameter (number must be positive)
	 * Post: returns the integer, and -1 if the string is not a number
	 */
	public static int makeInt(String str) {
		// declaration and initialization
		int returnInt = -1;
		int multiplier = (int) Math.pow(10,  str.length() - 1);
		boolean isAllInt = true;
		
		// check for int digits
		for (int i = 0; i < str.length() && isAllInt; i++) {
			if (str.charAt(i) < 48 || str.charAt(i) > 57) { // if the char is not in this ascii range, it is not a digit
				isAllInt = false;
			}
		}
		
		// calculate the int value
		if (isAllInt && str.length() > 0) {
			returnInt = 0;
			for (int i = 0; i < str.length(); i++, multiplier /= 10) {
				returnInt += (str.charAt(i) - 48) * multiplier;
			}
		}
		
		return(returnInt);
	}

	public static Fraction[] sortFractions(Fraction[]arr){ //sorts a fraction array from least to greatest
		Fraction temp;
        for (int i = 0; i<arr.length-1; i++) {
            for (int j = 0; j<arr.length-1-i; j++) {
                if(arr[j].calcDecimal() > arr[j+1].calcDecimal()) {
                temp = arr[j];
                arr[j] = arr[j+1];
				arr[j+1] = temp;
                }
            }
        }
            return arr;
	}
	

}