// Arup Guha
// 2/22/2026
// mixedfraction class to inherit from fraction
// Edited on 10/5/2026 for Zoom lecture, added subtraction and stock test.

import java.util.*;

public class mixedfraction extends fraction {

	private int whole;
	
	// Creates a mixed fraction with whole part w and fractional part n/d.
	public mixedfraction(int w, int n, int d) {
		super(n%d, d);
		whole = n/d + w;
	}
	
	// Creates a mixed fraction with value n divided by d.
	public mixedfraction(int n, int d) {
		super(n%d, d);
		whole = n/d;
	}
	
	// Default constructor set to 0.
	public mixedfraction() {
		super(0,1);
		whole = 0;
	}
	
	// Adds this to other and returns the result.
	public mixedfraction add(fraction other) {
		fraction tmp = super.add(other);
		return new mixedfraction(whole, tmp.numerator, tmp.denominator);
	}
	
	// Adds this to other and returns the result.
	public mixedfraction add(mixedfraction other) {
		int wholeTotal = whole + other.whole;
		fraction tmp = super.add(other);
		return new mixedfraction(wholeTotal, tmp.numerator, tmp.denominator);
	}
	
	public mixedfraction sub(mixedfraction other) {
		int wholeTotal = whole - other.whole;
		fraction tmp = super.sub(other);
		if (tmp.isNeg()) {
			tmp = tmp.add(1); /*** tmp.add(1) was a bug!!! ***/
			wholeTotal -= 1;
		}
		
		return new mixedfraction(wholeTotal, tmp.numerator, tmp.denominator);
	}
	
	// String representation of this object.
	public String toString() {
		return whole == 0 ? super.toString() : whole + " " + super.toString();
	}
	
	public static void main(String[] args) {
		
		Random rndObj = new Random();
		
		mixedfraction stockPrice = new mixedfraction(95, 3, 10);
		
		for (int i=1; i<=20; i++) {
			
			// Den is 2, 4, 6, 8 or 10.
			int den = 2 + 2*rndObj.nextInt(5);
			int num = rndObj.nextInt(2*den+1);
			mixedfraction today = new mixedfraction(num, den);
			
			// 60% gain
			int perc = rndObj.nextInt(100);
			if (perc < 60) {
				stockPrice = stockPrice.add(today);
				System.out.println("Day "+i+" stock added "+today+" new value is "+stockPrice);
			}

			else {
				stockPrice = stockPrice.sub(today);
				System.out.println("Day "+i+" stock lost "+today+" new value is "+stockPrice);
			}
		}
	}
}