All pastes #185493 Raw Edit

Something

public java v1 · immutable
#185493 ·published 2006-09-29 00:20 UTC
rendered paste body
// x  g-x o-2  k-inf1000/h06import easyIO.*;public class ruritania {	public static void main(String[] args) {		/*		 * Start with declaring variables and constants		 * that are needed throughout the application		 */		boolean inUse = true;			// Use this to loop program until termination is requested		boolean oilExtracted = false;	// Use this to determine if any oil has been extracted		In input = new In();			// Handle for data input		Out output = new Out();			// Handle for data output		int menu = 0;					// Set a default menu value that does not exist, to force the default case of the switch		int submenu = 0;				// Same as above, for submenu		int rowCarrier = 0;				// Used to carry local row variable out to other submenu case		int colCarrier = 0;				// Same as above		final int width = 3;			// The constant width of each field in printed formatted table throughout the app		final int rows = 15;			// The constant size of the oilfield		final int columns  = 25;		// Same as above		final String ruler  = "=================================================";		// Just a horisontal ruler for visual formatting		final String ruler2 = "-------------------------------------------------";		// Same as above		/*		 * "pause" is just an empty variable to allow pausing of program.		 * This renders a warning which is silenced with @SupressWarnings("unused")		 */		@SuppressWarnings("unused") String pause = "0";		/*		 * Finally, declare the main arrays for data storage.		 * This is the data which in a production environment would be saved to a flatfile or database,		 * so it would be saved also after application termination and restart.		 */		String[][] company = new String[rows][columns];		double[][] extracted = new double[rows][columns];		while (inUse == true) {		// Loop until menuoption "8) Avslutt" is chosen			switch (menu) {				// *** Buy field ***				case 1 :					// Check for available fields to buy					boolean allSold = true;					for (int r=0; r<rows; r++){						for (int c=0; c<columns; c++) {							if (company[r][c] == null) {								allSold = false;							}						}					}					if (allSold == false) {		// There are fields available for purchase, now prompt user for which is wanted						output.outln("\n\n> Kj0p et felt \n" + ruler2);						output.out("Oppgi 0nsket radnr (0-" + (rows-1) + "): ");						int row = input.inInt();						output.out("Oppgi 0nsket kolnr (0-" + (columns-1) + "): ");						int col = input.inInt();						if (row<0 || row>=rows || col<0 || col>=columns) {		// Entered integers are outside field!							output.out(ruler2 + "\nFEIL: Radnr maa vaere fra 0-" + (rows-1) + " og kolnr fra 0-" + (columns-1) + "!\n" + ruler2);						} else {							if (company[row][col] == null) {		// Requested field is available, prompt user for company name								String compName = "1";								boolean nameLengthOk = false;								while (!nameLengthOk) {									output.out("Oppgi firmanavn: ");									compName = input.inLine();									if (compName.length() > 1) {		// Name of company needs to be at least two characters, i.e. Q8										nameLengthOk = true;									} else {										output.outln(ruler2 + "\nFEIL: Firmanavn mA vaere minimum to bokstaver\n" + ruler2);										nameLengthOk = false;									}								}								company[row][col] = compName;		// Purchase is ok, sell field, store values and inform user								output.outln(ruler2 + "\nOK: Felt F(" + row + "," + col + ") solgt til " + company[row][col] + "\n" + ruler2);							} else {		// Requested field is already sold, inform user								output.outln(ruler2 + "\nFEIL: Felt F(" + row + "," + col + ") er allerede solgt til " + company[row][col] + "!\n" + ruler2);							}						}						// Send user to submenu 1						menu = 20;						submenu = 1;					} else {		// All fields are sold, inform user						output.outln(ruler2 + "\nFEIL: Beklager, alle felt er solgt.\n" + ruler2);						menu = 30;					}					break;				// *** Cancel purchase ***				case 2 :					// Check for available fields					boolean noneSold = true;					for (int r=0; r<rows; r++){				// Loop through all rows						for (int c=0; c<columns; c++) {		// Loop through all columns							if (company[r][c] != null) {	// See if field owner exists								noneSold = false;							}						}					}					if (noneSold == false) {		// There are sold fields, cancellation is possible						output.outln("\n\n> Annnuler kj0p av felt \n" + ruler2);						output.out("Oppgi 0nsket radnr (0-" + (rows-1) + "): ");						int row = input.inInt();						output.out("Oppgi 0nsket kolnr (0-" + (columns-1) + "): ");						int col = input.inInt();						if (row<0 || row>=rows || col<0 || col>=columns) {		// Entered integers are outside field!							output.out(ruler2 + "\nFEIL: Radnr maa vaere fra 0-" + (rows-1) + " og kolnr fra 0-" + (columns-1) + "!\n" + ruler2);						} else {							if (company[row][col] == null) {		// Entered field is currently not sold								output.outln(ruler2 + "\nFEIL: Feltet er ikke solgt, det er ledig for salg!\n" + ruler2);							} else {		// Entered field is sold, prompt for correct company name								output.out("Oppgi firmanavn: ");								String inCompany = (String) input.inLine();								String storedCompany = (String) company[row][col];								if (inCompany.equals(storedCompany)) {		// Company name entered correctly, cancel purchase and inform user									company[row][col] = null;									output.outln(ruler2 + "\nOK: Salg av felt F(" + row + "," + col + ") annulert.\n" + ruler2);								} else {		// Entered company mismatch with stored company									output.outln(ruler2 + "\nFEIL: Feltet tilh0rer ikke firmaet \"" + inCompany +"\".\n" + ruler2);								}							}						}						// Send user to submenu 2						menu = 20;						submenu = 2;					} else {		// No fields sold, so no cancellations possible						output.outln(ruler2 + "\nFEIL: Ingen felt er blitt solgt!\n" + ruler2);						menu = 30;					}					break;				// *** Create overview ***				case 3 :					output.outln("\n\n" + ruler);					output.outln("#                Oversiktskart                  #");					output.outln(ruler);					output.out(" ");					for(int i=0;i<25;i++) {						output.out(i,width);					// Print column headers					}					for(int j=0;j<15;j++) {						output.out("\n" + j + " ",width+1);		// Print row headers						for(int i=0;i<25;i++) {					// Print rest of columns in row							if(company[j][i] == null) {			// Field not sold, print .								output.out(".",width);							} else {							// Field is sold, print X								output.out("X",width);							}						}					}					menu = 30;					break;				// *** Show list of companies ***				case 4 :					output.outln("\n\n" + ruler);					output.outln("#                Firmaregister                  #");					output.outln(ruler);					String[] list = new String[375];		// New array of size 15*25 = 375, the max possible entries					int numbers = 0;						// Integer to keep track of cycles					for(int i=0;i<rows;i++) {						for(int j=0;j<columns;j++) {							if (company[i][j] != null) {								boolean exists = false;								for(int e=0;e<numbers;e++) {									if (list[e]!= null && list[e].equals(company[i][j])){		// Check if field is owned										exists = true;									}								}								if(!exists) {									list[numbers] = company[i][j];		// Store company name in the new array									numbers++;							// Increase cycles by one								}							}						}					}					for(int k=0;k<numbers;k++) {						output.out(list[k] + ":\n");		// Print the array with unique companies						for(int i=0;i<rows;i++) {							for(int j=0;j<columns;j++) {								if(company[i][j] != null && company[i][j].equals(list[k]) ) {									output.out("\tFelt F(" + i + "," + j + ")\n");		// Print every field of that company								}							}						}					}					menu = 30;					break;				// *** Update extractions *** -- see case 10 for more				case 5 :					output.outln("\n\n" + ruler);					output.outln("#            Oppdatering utvinning               #");					output.outln(ruler);					output.outln("\n\n> Oppdater utvinning \n" + ruler2);					output.out("Oppgi 0nsket radnr (0-" + (rows-1) + "): ");					int row = input.inInt();					output.out("Oppgi 0nsket kolnr (0-" + (columns-1) + "): ");					int col = input.inInt();					if (row<0 || row>rows || col<0 || col>columns) {		// Entered integers are outside field!						output.out(ruler2 + "\nFEIL: Radnr maa vaere fra 0-" + (rows-1) + " og kolnr fra 0-" + (columns-1) + "!\n" + ruler2);					} else {						if (company[row][col] == null) {							output.outln(ruler2 + "\nINFO: Felt F(" + row + "," + col + ") har ingen eier!\nUtvunnet hittil: " + extracted[row][col] + " fat\n" + ruler2);						} else {							output.outln(ruler2 + "\nINFO: Felt F(" + row + "," + col + ") eies av " + company[row][col] + "!\nUtvunnet hittil: " + extracted[row][col] + " fat\n" + ruler2);						}						/*						 * Regardless of result of last if-block, give user possibility of registering extracted oil.						 * This is split into the own submenu at menu (case 10) for simplicity and reuse of code.						 */						rowCarrier = row;						colCarrier = col;						menu = 10;					}					break;				// *** Find max ***				case 6 :					double maxExtracted = 0;					int maxExRow = 0;					int maxExCol = 0;					oilExtracted = false;					String maxComp = "Udefinert";					for(int i=0;i<rows;i++) {						for(int j=0;j<columns;j++) {							if(company[i][j] != null) {								if(extracted[i][j] > 0) {									oilExtracted = true;									if(extracted[i][j] > maxExtracted) {										maxExtracted = extracted[i][j];										maxExRow = i;										maxExCol = j;										maxComp = company[i][j];									}								}							}						}					}					if (!oilExtracted){						output.out(ruler2 + "\nFEIL: Ingen olje utvunnet");					} else {						output.outln("\n\n" + ruler);						output.outln("#               Maks utvinning                  #");						output.outln(ruler);						output.out(ruler2 + "\nINFO: Maks utvinning er " + maxExtracted + " fat,\n\tutvunnet fra felt F(" + maxExRow + "," + maxExCol + ")\n\tFeltet eies nA av: " + maxComp + ".\n" + ruler2);					}					menu = 30;					break;				// *** Find average oil ***				case 7 :					double totExtracted = 0;					int nrFields = 0;					oilExtracted = false;					for(int i=0;i<rows;i++) {						for(int j=0;j<columns;j++) {							if(company[i][j] != null) {			// Field currently owned by company?								if(extracted[i][j] > 0) {		// Any oil extracted?									oilExtracted = true;									totExtracted = totExtracted + extracted[i][j];		// Add this fields value to total									nrFields++;		// Keep track of number of fields for a valid divider for average								} else {									oilExtracted = false;								}							}						}					}					if (!oilExtracted){						output.out(ruler2 + "\nFEIL: Ingen olje utvunnet");					} else {		// Oil has been extracted, output results						double avgExtracted = totExtracted / nrFields;						output.outln("\n\n" + ruler);						output.outln("#           Gjennomsnittlig utvunnet            #");						output.outln(ruler);						output.out(ruler2 + "\nINFO: Gjennomsnittlig utvinning per felt er ");						output.out(avgExtracted, 2);						output.out(".\n\tTotalt ");						output.out(totExtracted, 0);						output.out(" fat utvunnet over " + nrFields + " felt.\n" + ruler2);					}					menu = 30;					break;				// *** End program ***				case 8 :					output.outln("Avslutter program");					inUse = false;					break;				// *** Edit extractions ***				case 10 :					row = rowCarrier;					col = colCarrier;					output.outln("\n\n> Oppdater utvinning av felt F(" + row + "," + col + ").\nUtvunnet hittil: " + extracted[row][col] + " fat\n" + ruler2);					output.out("Oppgi antall fat utvunnet siste 3 mnd: ");					double lastmonths = input.inDouble();					// Add new value to already existing					extracted[row][col] = extracted[row][col] + lastmonths;					output.outln(ruler2 + "\nOK: Utvinning pA F(" + row + "," + col + ") oppdatert til " + extracted[row][col] + "\n" + ruler2);					menu = 20;					submenu = 5;					break;				// *** Show submenu ***				case 20 :					output.outln("\n\n" + ruler);					output.outln("#                  Undermeny                    #");					output.outln(ruler);					switch (submenu) {						case 1 : output.out("1) Kj0p flere felt"); break;						case 2 : output.out("1) Annuler flere kj0p"); break;						case 5 : output.out("1) Oppdater utvinning av flere felt"); break;					}					output.out("\n2) Tilbake til hovedmeny\n"+ruler2);					output.out("\nVelg kommando: ");					int selected = input.inInt();					if (selected==1) {						menu = submenu;					} else {						menu = 0;					}					break;				// *** Output pausemenu ***				case 30 :					output.out("\n\n" + ruler2 + "\n> Trykk enter for A gA tilbake til startmeny");					pause = input.inLine();				// *** Output mainmenu ***				default:					output.outln("\n\n\n" + ruler);					output.outln("#  Velkommen til Ruritanias  oljefeltoversikt   #");					output.outln(ruler);					output.outln("1) Kj0p et felt");					output.outln("2) Annuler kj0p av felt");					output.outln("3) Lag oversiktskart");					output.outln("4) Vis firmaregister");					output.outln("5) Oppdater oljeutvinning");					output.outln("6) Finn maksimal oljeutvinning");					output.outln("7) Finn gjennomsnittlig oljeutvinning");					output.outln("8) Avslutt\n" + ruler2);					output.out("> Velg kommando: ");					// Prompt for wanted menuoption					menu = input.inInt();			}		}	}}