package se.ericthelin.fractions;

import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;

public class Fraction {

	private static final Pattern FRACTION_PATTERN = Pattern.compile("\\d+/\\d+");
	private static final Pattern INTEGER_PATTERN = Pattern.compile("\\d+");

	public static Fraction of(String text) {
		if (!FRACTION_PATTERN.matcher(text).matches()
				&& !INTEGER_PATTERN.matcher(text).matches()) {
			throw new InvalidFractionFormatException(text);
		}

		if (text.endsWith("/0")) {
			throw new ZeroDenominatorException(text);
		}

		return new Fraction(text);
	}

	private final String text;

	private Fraction(String text) {
		this.text = requireNonNull(text);
	}

	@Override
	public String toString() {
		return text;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}

		if (!(obj instanceof Fraction)) {
			return false;
		}

		Fraction other = (Fraction) obj;

		return text.equals(other.text);
	}

	public Fraction plus(Fraction term) {
		throw new UnsupportedOperationException("Not yet implemented");
	}
}

package se.ericthelin.fractions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Integer.parseInt;

public class Fraction {

	private static final Pattern FRACTION_PATTERN = Pattern.compile("(\\d+)/(\\d+)");
	private static final Pattern INTEGER_PATTERN = Pattern.compile("\\d+");

	public static Fraction of(String text) {
		Matcher fractionMatcher = FRACTION_PATTERN.matcher(text);

		if (fractionMatcher.matches()) {
			return Fraction.of(
					parseInt(fractionMatcher.group(1)),
					parseInt(fractionMatcher.group(2)));
		}

		Matcher integerMatcher = INTEGER_PATTERN.matcher(text);

		if (integerMatcher.matches()) {
			return Fraction.of(parseInt(integerMatcher.group(0)), 1);
		}

		throw new InvalidFractionFormatException(text);
	}

	private static Fraction of(int numerator, int denominator) {
		return new Fraction(numerator, denominator);
	}

	private int numerator;
	private int denominator;

	public Fraction(int numerator, int denominator) {
		this.numerator = numerator;
		this.denominator = denominator;

		if (denominator == 0) {
			throw new ZeroDenominatorException(toString());
		}
	}

	@Override
	public String toString() {
		return numerator + "/" + denominator;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}

		if (!(obj instanceof Fraction)) {
			return false;
		}

		Fraction other = (Fraction) obj;

		return numerator == other.numerator
				&& denominator == other.denominator;
	}

	public Fraction plus(Fraction term) {
		throw new UnsupportedOperationException("Not yet implemented");
	}
}

Change Fraction implementation from text to integers