package Excel;


	import javax.swing.JOptionPane;
	import java.io.File;
	import java.io.IOException;
	import jxl.Cell;
	import jxl.Sheet;
	import jxl.Workbook;
import jxl.read.biff.BiffException;


			
		public class Input {
			
			/**
		 * @param args
		 */
		
		private String inputFile;
		
		public void setInputFile(String inputFile) {
			this.inputFile = inputFile;
		}

		protected static int row ;
		protected static int column;
		protected static int[][] arr;
		//private String Machines_numbers;
		//private String Jobs_numbers;
		
		public int[][] getArr() {
	        return arr;
	        }

		public void read() throws IOException  {
		
			
			File inputWorkbook = new File(inputFile);
			Workbook w;
			try {
				w = Workbook.getWorkbook(inputWorkbook);
				// Get the first sheet
				Sheet sheet = w.getSheet(0);
				
				//---------------------------read from XML and store in array----------------------------------//
				
				row = sheet.getRows()-1;       // Machines_numbers
				column = sheet.getColumns()-1;    // Jobs_numbers
				// we subtracted 1 from rows and coulumns because the first row and column is test
				
				//--------------------------determine the number of machines & jobs---------------------------
				/*
				LOOPA:
					 for(int i = 0; i < 1000; i++) {

					Machines_numbers = JOptionPane.showInputDialog("Please Enter the number of machines") ;

					if (Character.isDigit(Machines_numbers.charAt( i = 0  ) )) // verify if the entered value is only digit value
					{
						row = Integer.parseInt( Machines_numbers );

				     break LOOPA;
					}

				    else {
						 JOptionPane.showMessageDialog(null,"Please enter just numbers"
							,null, JOptionPane.ERROR_MESSAGE);
					}
					 }  			 
				
				LOOPB:
					 for(int i = 0; i < 1000; i++) {

						 Jobs_numbers = JOptionPane.showInputDialog("Please Enter the number of jobs") ;

					if (Character.isDigit(Jobs_numbers.charAt( i = 0  ) )) // verify if the entered value is only digit value
					{
			        column = Integer.parseInt( Jobs_numbers );
			        break LOOPB;
					}

				    else {
						 JOptionPane.showMessageDialog(null,"Please enter just numbers"
							,null, JOptionPane.ERROR_MESSAGE);
					}
					 }*/
			//--------------------------------------------------------------------------------------------------------------------		 
					 
					 arr = new int [row][column]; //processing time for every product on every machine
				
			        String output = "";   // Accumulate text here (should be StringBuilder).
			        //... Print array in rectangular form using nested for loops.
			        
					for (int row = 1; row < sheet.getRows(); row++) {
						for (int column = 1; column < sheet.getColumns(); column++) {
					    	 
							Cell cell = sheet.getCell(column, row);
							if(cell.getContents() != null && (!cell.getContents().equals("")) ) { 
		                          arr[row-1][column-1] = Integer.parseInt(cell.getContents()); }
							}
					}    
		                          
		                          for (int i = 0; i < row; i++) {
		          		            for (int j = 0; j < column; j++) {
			                output += " " + arr[i][j];
			            }
			                output += "\n";
			        }
					//System.out.println(output);
			      JOptionPane.showMessageDialog(null, "The input is " + "\n" + output, 
			          "F" + (sheet.getRows()-1) + "|Sijk, prmu|Cmax",  JOptionPane.INFORMATION_MESSAGE);
				    
			     	 
			        } 
			catch (BiffException e) {
				e.printStackTrace();
			}
		
	    }
		
	}