
import myInputs;
import Matrix;
import java.io.*;

class MatrixMenu{

	public MatrixMenu(){			//boring little constructor
	}

	/* Main program start point */
	public static void main(String args[]){
		Matrix A = new Matrix(0,0);
		Matrix B = new Matrix(0,0);
		myInputs inp = new myInputs();
		RandomAccessFile f;
		long len;
		String filename;
		
		int iH, iW;
		int iChoice = -1;
		while(iChoice!=0){
			MatrixMenu m = new MatrixMenu();
			iChoice = m.doMenu();				//Show the menu to the user and get input
			switch(iChoice){
			case 1:								//Modify matrix A
				System.out.print("Enter width for Matrix A: ");
				try{
					iW = inp.inputIntegerQuiet();
					System.out.print("Enter height for Matrix A: ");
					iH = inp.inputIntegerQuiet();
				}//try
				catch(Exception e){
					System.out.println("Error...aborting function.");
					break;
				}//catch
				A = new Matrix(iW, iH);			//Change to new size...
				A.getUserInput();
				break;
			case 2:
				System.out.print("Enter width for Matrix B: ");

				try{
					iW = inp.inputIntegerQuiet();
					System.out.print("Enter height for Matrix B: ");
					iH = inp.inputIntegerQuiet();
				}//try
				catch(Exception e){
					System.out.println("Error...aborting function.");
					break;
				}//catch
				B = new Matrix(iW, iH);			//Change to new size...
				B.getUserInput();
				break;
			case 3:
				A.printMatrix();
				break;
			case 4:
				B.printMatrix();
				break;
			case 5:
				Matrix tmp = new Matrix( A.getWidth(), A.getHeight());
				tmp.copyMatrix(A);
				tmp.transpose();
				System.out.println("\r\nThe transpose of A is : ");
				tmp.printMatrix();
				break;
			case 6:
				tmp = new Matrix( B.getWidth(), B.getHeight());
				tmp.copyMatrix(B);
				tmp.transpose();
				System.out.println("\r\nThe transpose of B is : ");
				tmp.printMatrix();
				break;
			case 7:
				tmp = A.multiply(B);
				if(tmp==null){
					System.out.println("\r\nThis matrix multiplication is not possible!\r\n");
				}
				else{
					System.out.println("\r\nThe matrix resulting from A * B is");
					tmp.printMatrix();
				}
				break;
			case 8:
				tmp = B.multiply(A);
				if(tmp==null){
					System.out.println("\r\nThis matrix multiplication is not possible!\r\n");
				}
				else{
					System.out.println("\r\nThe matrix resulting from B * A is");
					tmp.printMatrix();
				}
				break;
			case 9:
				tmp = A.multiply(A);
				if(tmp==null){
					System.out.println("\r\nThis matrix cannot be squared!\r\n");
				}
				else{
					System.out.println("\r\nThe matrix resulting from A^2 is");
					tmp.printMatrix();
				}
				break;
			case 10:
				tmp = B.multiply(B);
				if(tmp==null){
					System.out.println("\r\nThis matrix cannot be squared!\r\n");
				}
				else{
					System.out.println("\r\nThe matrix resulting from B^2 is");
					tmp.printMatrix();
				}
				break;
			case 11:
				MatrixMenu t = new MatrixMenu();
				t.doWriteToFile(A);
				break;
			case 12:
				t = new MatrixMenu();
				t.doWriteToFile(B);
				break;
			default:
				if((iChoice!=0)&&(iChoice!=999))
					System.out.println("\r\nUnrecognized command...\r\n");
				break;
			}//switch
		}//while

	}//main method

	//This will prompt for a file and then get the specified matrix to write
	//it's display to the file
	private void doWriteToFile(Matrix theMat){
		System.out.print("\r\nPlease enter filename: ");
		byte b[] = new byte[100];
		RandomAccessFile f;
		String filename;
		long len;
		try{
			System.in.read(b);
			filename = new String(b, 0);
			filename = filename.trim();
			f = new RandomAccessFile(filename, "rw");
			len = f.length();				//Get existing file length
			f.seek(len);					//Go to the end
			theMat.printToFile(f);
		}//try
		catch(IOException e){
			System.out.print("There was an error writing to the file (");
			System.out.print( e.getMessage().trim());
			System.out.print(")....aborting\r\n");
		}//catch
		System.out.print("Output written successfully\r\n\r\n");
	}//doWriteToFile method

	/* This will display a menu to the user and get the menu choice. */
	public int doMenu(){

		System.out.println("  1) Modify matrix A");
		System.out.println("  2) Modify matrix B");
		System.out.println("  3) Display matrix A");
		System.out.println("  4) Display matrix B");
		System.out.println("  5) Compute transpose(A)");
		System.out.println("  6) Compute transpose(B)");
		System.out.println("  7) Compute A * B");
		System.out.println("  8) Compute B * A");
		System.out.println("  9) Compute A^2");
		System.out.println(" 10) Compute B^2");
		System.out.println(" 11) Write matrix A to file");
		System.out.println(" 12) Write matrix B to file");
		System.out.println("  0) Quit\r\n");
		System.out.print("Choice --> ");
		
		myInputs m = new myInputs();
		int iVal = -1;
		try{
			iVal = m.inputIntegerQuiet();
			if(iVal==999){
				System.out.println("\r\nIsn't java so much fun...\r\n");		//Easter eggs come easily when you're tired...
			}//if
		}
		catch(Exception e){
			iVal = -1;
		}

		return iVal;
	}//displayMenu method


}