//johnaby g-6 o-2 k-inf1000/h07. /* John Alexander Bye Obligatorisk Innlevering #2 INF1000 12. September 07 MNM5-INFPS 2007-HØST DSN Gruppe 6 johnaby@ifi.uio.no */ class Oilfield { //Coders conduct (private) private String owner = ""; private int refined; public Oilfield(String sOwner, int iRefined) { //Initialize owner = sOwner; refined = iRefined; //Initialize to zero } //Return private members String getOwner() { return owner; } int getRefined() { return refined; } void setOwner(String sOwner) { owner = sOwner; } void setRefined(int iRefined) { refined = iRefined; } } //-- OILSYS.JAVA --// //johnaby g-6 o-2 k-inf1000/h07. /* John Alexander Bye Obligatorisk Innlevering #2 INF1000 12. September 07 MNM5-INFPS 2007-HØST DSN Gruppe 6 johnaby@ifi.uio.no TODO: List over oilcompanies .. Find out how many fields each company owns */ import easyIO.*; //In import java.util.*; //HashSet class Oilsys { //For printError static final int ERROR_NORMAL = 0, ERROR_CRITICAL = 1; //Global declaration of the Oilfield-classarray & In static Oilfield database[][] = new Oilfield[16][22]; static In input = new In(); static public void totalRefined() { System.out.printf("\n-- Total Refined From All Oilfields --\n"); int totalRefined = 0, numFields = 0; for(int row = 0; row < 16; row++) { for(int col = 0; col < 22; col++) { if(database[row][col] != null) { //Occupied so they're refining... No money-back guarantee! totalRefined += database[row][col].getRefined(); numFields++; } } } System.out.printf("\nTotal Refined:\t" + totalRefined + " (Total Oilfields: " + numFields + ")"); } static public void largestRefined() { int fieldX = 0, fieldY = 0, maxRefined = 0; String sStatus; System.out.printf("\n-- Largest Refined Value --\n"); //Loop through the array to find the largest refined and it's position for extra data for(int row = 0; row < 16; row++) { for(int col = 0; col < 22; col++) { if(database[row][col] != null) { if(database[row][col].getRefined() > maxRefined) { maxRefined = database[row][col].getRefined(); fieldX = row; fieldY = col; } } } } if(maxRefined != 0) { if(database[fieldX][fieldY].getOwner() == null) sStatus = "(Available)"; else sStatus = "(Occupied by " + database[fieldX][fieldY].getOwner() + ")"; System.out.printf("Largest Refined Value:\t" + maxRefined + " at " + fieldX + "," + fieldY + ". STATUS: " + sStatus); } else { printError("The oilfields have not refined any oil yet!", ERROR_NORMAL); } } static public void updateRefined() { int fieldX, fieldY; System.out.printf("\n-- Updated Refined Values --\nField F(row,column):\t"); fieldX = input.inInt(" \n\rF(,)"); //Filter out chars fieldY = input.inInt(" \n\rF(,)"); input.inChar(); //Read it all if(!checkBounds(fieldX, fieldY)) printMenu(); //Jump to menu if(!checkField(fieldX, fieldY)) { //Occupied, so we can update its values System.out.printf("\nCurrent Refined Value:\t" + database[fieldX][fieldY].getRefined()); System.out.printf("\nUpdate Refined with:\t"); int newRefined; //Get user input newRefined = input.inInt(); database[fieldX][fieldY].setRefined( database[fieldX][fieldY].getRefined() + newRefined ); System.out.printf("\nNew Refined Value:\t" + database[fieldX][fieldY].getRefined()); } else { printError("Field is vacant. Unable to update!", ERROR_NORMAL); } } static public void printRegistrants() { System.out.printf("\n-- List of Registrants --\n"); HashSet hashSet = new HashSet(); final int iTotalFields = 352; //16*22 int iOccupiedFields = 0; for(int row = 0; row < 16; row++) { for(int col = 0; col < 22; col++) { if(!checkField(row, col)) //Field is occupied { hashSet.add( database[row][col].getOwner() ); //Add owner to our HashSet iOccupiedFields++; } } } System.out.printf("\nRegistrants:\t"); String sFormattedRList = "", sTempRegistrant; int iOccurences = 0; Iterator hashIterator = hashSet.iterator(); while(hashIterator.hasNext()) { sTempRegistrant = (String)hashIterator.next(); //Check occurences (Cheap Code-Hack) This could have been done differently -- Though this works so it's good enough for this cause ;) for(int row = 0; row < 16; row++) { for(int col = 0; col < 22; col ++) { if(!checkField(row, col)) //Field is occupied, check for equals { if(sTempRegistrant.toLowerCase().equals(database[row][col].getOwner().toLowerCase())) { iOccurences++; } } } } sFormattedRList += sTempRegistrant + " (" + iOccurences + "), "; iOccurences = 0; } System.out.printf(sFormattedRList + "(Occupied\\Available: " + iOccupiedFields + "\\" + iTotalFields + ").\n"); } static public void printMap() { System.out.printf("\n-- Map over Ruritania --\n"); //Print the header System.out.printf(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21"); //For-loop to check for fields and generate row numbers for(int row = 0; row < 16; row++) { if(row < 10) //Cosmetic hack ;) It's all about looks System.out.printf("\n" + row + " "); else System.out.printf("\n" + row + " "); for(int col = 0; col < 22; col++) //Check for vacancies { if(checkField(row, col)) System.out.printf(". "); else System.out.printf("x "); } } } static public void purgeField() { int fieldX, fieldY; String sRegistrant; System.out.printf("\n-- Purge Oilfield --\nField F(row,column):\t"); //Read user input fieldX = input.inInt(" \n\rF(,)"); //Filter out chars fieldY = input.inInt(" \n\rF(,)"); input.inChar(); //Read it all if(!checkBounds(fieldX, fieldY)) printMenu(); //Jump to menu if(checkField(fieldX, fieldY)) { //Available -- Can't purge an empty field printError("Field is vacant. Unable to purge!", ERROR_NORMAL); printMenu(); } System.out.printf("\nRegistrant:\t"); //Check that it it's the actual owner of the field who wants to have it purged sRegistrant = input.inLine(); if(sRegistrant.toLowerCase().equals(database[fieldX][fieldY].getOwner().toLowerCase())) { System.out.printf("\nRegistrant verified! Processing request..."); } else { printError("Registrant could not be verified! Check spelling!", ERROR_NORMAL); printMenu(); } //We know its occupied. Lets set the owner-string to NULL System.out.printf("\nPurging field " + fieldX + "," + fieldY + " registered to " + database[fieldX][fieldY].getOwner() + "."); database[fieldX][fieldY].setOwner(null); } static public void buyField() { int fieldX, fieldY; boolean fieldsAvailable = false; System.out.printf("\n-- Buy Oilfield --\nField F(row,column):\t"); //Check if there are any fields available for(int row = 0; row < 16; row++) { for(int col = 0; col < 22; col++) { if(checkField(row, col)) { fieldsAvailable = true; break; } } } if(!fieldsAvailable) { printError("No fields available. All are currently occupied!", ERROR_NORMAL); printMenu(); } //Read user input fieldX = input.inInt(" \n\rF(,)"); //Filter out chars fieldY = input.inInt(" \n\rF(,)"); input.inChar(); //Read it all if(!checkBounds(fieldX, fieldY)) printMenu(); //Jump to menu if(!checkField(fieldX, fieldY)) { //Occupied printError("Field is already occupied!", ERROR_NORMAL); } else { //Available -- CaChing! System.out.printf("\nRegistrant:\t"); String sRegistrant; int iOldRefined = 0; //Initialize to zero, passing to constructor later on //Check if it has been constructed, then see if they have refined any oil if(database[fieldX][fieldY] != null) { if(database[fieldX][fieldY].getRefined() > 0) iOldRefined = database[fieldX][fieldY].getRefined(); } sRegistrant = input.inLine(); //Allow whitespaces database[fieldX][fieldY] = new Oilfield(sRegistrant, iOldRefined); System.out.printf("\nField " + fieldX + "," + fieldY + " sold to " + sRegistrant + "."); } } static public boolean checkBounds(int fieldX, int fieldY) { if((fieldX > 15) || (fieldY > 21)) { printError("Field was out of bounds. Check that the field you specified does not exceed F(15,21)!\n",ERROR_CRITICAL); return false; //Out of bounds -- Let the user pick again } return true; //In bounds } static public boolean checkField(int fieldX, int fieldY) { if((database[fieldX][fieldY] == null) || (database[fieldX][fieldY].getOwner() == null)) return true; //Available return false; //Field is Occupied } static public void printError(String sErrorMessage, int type) { switch(type) { case ERROR_NORMAL: System.out.printf("\n** ERROR:\t" + sErrorMessage); break; case ERROR_CRITICAL: System.out.printf("\n** CRITICAL ERROR:\t" + sErrorMessage); } } static public void printMenu() { System.out.printf("\n\n\n-- OilSystem by John A Bye --\n" + "1. Buy Oilfield\n" + "2. Purge Oilfield\n" + "3. Map Oilfields\n" + "4. List Owners\n" + "5. Update Refined Values\n" + "6. Largest Refined Value\n" + "7. Total Refined (All Oilfields)\n" + "8. Exit\n" + "Choice:\t"); //Get user input int iChoice; iChoice = input.inInt(); //Execute input switch(iChoice) { case 1: buyField(); break; case 2: purgeField(); break; case 3: printMap(); break; case 4: printRegistrants(); break; case 5: updateRefined(); break; case 6: largestRefined(); break; case 7: totalRefined(); break; case 8: System.exit(1); //Exit default: printError("Invalid choice!", ERROR_NORMAL); } printMenu(); } static public void main(String[] args) { printMenu(); } }