All pastes #745563 Raw Edit

Someone

public java v1 · immutable
·published 2007-10-22 14:36 UTC
rendered paste body
//johnaby g-6 o-3 k-inf1000/h07./* John Alexander Bye Obligatorisk Innlevering #3 INF1000		   21. Oktober 07MNM5-INFPS 2007-HØST DSNGruppe 6 johnaby@ifi.uio.no Notes:Det står at vi kun skal la monthlyRun() til å bli kjørt èn gang i måneden, men det står ingenting om hvordan dette skal gjøres.Etter å ha samtalt med en annen oblig-retter, kom vi frem til at det mest sannsynlig bare var snakk om at administratorneselv skulle passe på dette.Dersom dette ikke stemmer, kan jeg fikse dette ved blant annet å kjøre en sjekk mot systemdato. Oppgavener skrevet på engelsk, da jeg gjør dette av vane, noe som sikkert ikke blir dumt i jobbsammenheng senere.*/import easyIO.*;class Student{	int amount;	String name;		Student(String sName, int iAmount)	{		amount = 0;		name = sName;		amount += iAmount;	}		String getName()	{		return name;	}		void setName(String sName)	{		name = sName;	}		int getAmount()	{		return amount;	}		void setAmount(int iAmount)	{		amount += iAmount;	}		void purgeFlat()	{		name = "EMPTY FLAT";		amount = 0;	}}class Flat{	Student tenant;	int remaining;		Flat(String sName, int iAmount)	{		if(sName.equals("EMPTY FLAT"))		{			tenant = new Student(sName, iAmount);			remaining = 0;		}		else		{			tenant = new Student(sName, iAmount);			remaining = iAmount;		}	}		Student getTenant()	{		return tenant;	}		int getRemaining()	{		return remaining;	}}class Flatcomplex{	final int NUM_HOUSES = 4, NUM_FLATS = 8;	Flat[][] flats = new Flat[NUM_HOUSES][NUM_FLATS];	In cInput = new In();	public enum ErrorType { Normal, Critical, Fatal };	String sFilename;		int totalMonths;	int totalVacancyMonths;	int totalEarned;		void printEconomyReport()	{		double medianVacancyMonths;				medianVacancyMonths = totalVacancyMonths / totalMonths;			System.out.println("\n-- Economy report :: System has been running for " + totalMonths + " months");				System.out.println("Total balance:\t" + totalEarned);		System.out.println("Vacancy months:\t" + totalVacancyMonths);		System.out.println("Median vacancy for each month:\t" + medianVacancyMonths);		System.out.println("Lost revenue due to vacancy:\t" + (totalVacancyMonths*4500));		}		void callTorpedo(String flatName, String sStudent, int iDemand)	{			Out fOutput = new Out("Torpedo.txt", true); //Write with append				fOutput.outln(flatName + ":" + sStudent + ":" + iDemand + ":");				fOutput.close();		}		void checkEviction()	{		System.out.println("\n-- Check for evictions");				int iDemand = 0;		String flatName;		String sName;				for(int house = 0; house < NUM_HOUSES; house++)		{			for(int flat = 1; flat < NUM_FLATS; flat++)			{				if(!checkVacancy(house+1, flat))				{ //Flat is occupied, check the students balance										if(flats[house][flat].getTenant().getAmount() < 0)					{	//Balance is less than zero, evictiontime!						iDemand = (flats[house][flat].getTenant().getAmount() * -1) + 1000;						sName = flats[house][flat].getTenant().getName();						flatName = "" + (house+1) + (char)(flat+'A');												callTorpedo(flatName, sName, iDemand);												flats[house][flat].getTenant().purgeFlat();												System.out.println("Flat " + flatName + " is now evicted. Tenant " + sName + " owes " + iDemand + ".");					}				}			}		}	}		void registerPayment()	{		System.out.println("\n-- Register payment");				int iHouse;		char cFlat;		String sName;				System.out.print("Name:\t");		sName = cInput.inLine();		sName = sName.toUpperCase();		System.out.print("House:\t");		iHouse = cInput.inInt();				cInput.readLine(); //Empty buffer				System.out.print("Flat:\t");		cFlat = cInput.inChar();			cFlat = Character.toUpperCase(cFlat);				int iAmount;				//Check if the tenant is who he says he is ;)		if(flats[iHouse-1][(int)cFlat-'A'].getTenant().getName().equals(sName))		{	//Tenant matches -- Register payment			System.out.print("Amount:\t");			iAmount = cInput.inInt();			flats[iHouse-1][(int)cFlat-'A'].getTenant().setAmount(iAmount);						System.out.println("-> " + iAmount + " was added to the balance of " + sName);		}		else		{			printError("Flat is not registered to the tenants name.", ErrorType.Normal);		}		}		void monthlyRun()	{		System.out.println("\n-- Monthly run");			//All non-vacant flats have their tenants charged with 4500		totalMonths++;				In fInput = new In("Lysregning.data");				int iNum;		int iHouse;		char cFlat;		int iKwt;		int amountKwt;		int vacant = 0;				while(!fInput.endOfFile())		{			iNum = fInput.inInt(":");			iHouse = fInput.inInt(":");			cFlat = fInput.inChar(":");			iKwt = fInput.inInt(":");						fInput.readLine(); //Empty						cFlat = Character.toUpperCase(cFlat);						//Electricity			if(cFlat != 'A' /*&& (!checkVacancy(iHouse, (int)cFlat-'A'))*/)			{	//Deduct whatever kwt that was registered				amountKwt = iKwt*2;				flats[iHouse-1][(int)cFlat-'A'].getTenant().setAmount(-amountKwt);			}			else if(cFlat == 'A')			{	//Guldbrand has to pay 0.8 per kwt registered to all of the A's -- This is for the public areas				amountKwt = (int)(iKwt*0.8); //Here I assume that we only deal with whole numbers								totalEarned -= amountKwt;				System.out.println(amountKwt);			}		}				//To deduct the the monthly fee		for(int house = 0; house < NUM_HOUSES; house++)		{			for(int flat = 1; flat < NUM_FLATS; flat++)			{				if(checkVacancy(house+1, flat)) //+1 because checkvacancy deducts 1				{					totalEarned -= 1000;					totalVacancyMonths++;				}				else				{	//These are occupied, deduct 4500 from the tenants					flats[house][flat].getTenant().setAmount(-4500);					totalEarned += 4500;					totalEarned -= 1000;				}			}		}				System.out.println("-> Completed the monthly run. These are this months statistics:");		System.out.println("Total Vacancy Months:\t" + totalVacancyMonths);		System.out.println("Total Earned:\t" + totalEarned);	}		void moveTenant()	{		System.out.println("\n-- Move tenant");				int iHouse;		char cFlat;		String sName;				System.out.print("Name:\t");		sName = cInput.inLine();		sName = sName.toUpperCase();				System.out.print("House:\t");		iHouse = cInput.inInt();				cInput.readLine(); //Empty buffer				System.out.print("Flat:\t");		cFlat = cInput.inChar();				cFlat = Character.toUpperCase(cFlat);				//Check if the tenant is who he says he is ;)		if(flats[iHouse-1][(int)cFlat-'A'].getTenant().getName().equals(sName))		{	//Tenant matches			totalEarned += 800; //Expeditionfee			System.out.print("-> Students balance with the fee deducted:\t");			int postDeduction = flats[iHouse-1][(int)cFlat-'A'].getTenant().getAmount();			postDeduction -= 800;						System.out.print(postDeduction + "\n");						flats[iHouse-1][(int)cFlat-'A'].getTenant().purgeFlat(); //Purging the flat, setting name = EMPTY FLAT and amount = 0				}		else		{			printError("Flat is not registered to the tenants name.", ErrorType.Normal);		}	}		void registerTenant()	{		System.out.println("\n-- Register new tenant");		int iHouse;		char cFlat;		String sName;				System.out.print("House:\t");		iHouse = cInput.inInt();		cInput.readLine(); //Clear buffer		System.out.print("Flat:\t");		cFlat = cInput.inChar();		cFlat = Character.toUpperCase(cFlat);				if(checkVacancy(iHouse, ((int)cFlat-'A')))		{ //Vacant -- Register a new tenant			System.out.print("Name:\t");			sName = cInput.inLine();			sName = sName.toUpperCase(); //Store the name in uppercase for an easier-to-read datafile & comparisson later						//iHouse-1 to get the correct ArrayIndex for our 1-4 values			flats[iHouse-1][(int)cFlat-'A'].getTenant().setName(sName);			flats[iHouse-1][(int)cFlat-'A'].getTenant().setAmount(9000); //9000 deposit						totalEarned += 4500; //Student pays 4500 upon becoming a tenant						System.out.println("-> Room " + iHouse + cFlat + " has now been registered to " + sName);				}		else		{			printError("Flat is not vacant.", ErrorType.Normal);		}	}		void listTenants()	{		System.out.println("\n-- Vacant flats");		int vacant = 0;				for(int house = 0; house < NUM_HOUSES; house++)		{			for(int flat = 1; flat < NUM_FLATS; flat++)			{								if (checkVacancy(house+1, flat)) {					System.out.println("Flat:\t" + (house+1) + ((char)(flat+'A')));					vacant++;				}			}		}		System.out.println("Total Vacancies:\t" + vacant);	}		Flatcomplex(String filename)	{		//Populate from FILE		sFilename = filename;		In fInput = new In(filename);				int iHouse;		char cFlat;		String sName;		int iAmount;				boolean first = true;		int ant = 0;				for(int i = 0; i < NUM_HOUSES*(NUM_FLATS-1); i++)		{					iHouse = fInput.inInt(";");			cFlat = fInput.inChar(";");			sName = fInput.inWord(";");			iAmount = fInput.inInt(";");						fInput.readLine(); //Finish reading						flats[iHouse - 1][(int)(cFlat-'A')] = new Flat(sName, iAmount);					}					totalMonths = fInput.inInt(";");		totalVacancyMonths = fInput.inInt(";");		totalEarned = fInput.inInt(";");			}		void Menu()	{		//Meny		int iChoice = 1;		boolean finished = false;				while(!finished)		{				System.out.println("\nMENY -- HYBELHUS SYSTEM");		System.out.println("0.\tExit and save");		System.out.println("1.\tList vacant flats");		System.out.println("2.\tRegister new tenant");		System.out.println("3.\tRegister move of student");		System.out.println("4.\tMonthly run with electricity");		System.out.println("5.\tRegister payment from student");		System.out.println("6.\tCheck for evictions");		System.out.println("7.\tPrint economy report");		System.out.print("\nChoice:\t");				iChoice = cInput.inInt();				switch(iChoice)		{			case 0:			finished = true;			break;			case 1:			listTenants();			break;			case 2:			registerTenant();			break;			case 3:			moveTenant();				break;			case 4:			monthlyRun();			break;			case 5:			registerPayment();			break;			case 6:			checkEviction();			break;			case 7:			printEconomyReport();			break;			default:			printError("Invalid choice.", ErrorType.Normal);		}		}				Exit();	}			public static void printError(String msg, ErrorType TYPE)	{		switch(TYPE)		{			case Normal:			System.out.println("*** ERROR: " + msg);			break;			case Critical:			System.out.println("*** CRITICAL ERROR: " + msg);			break;		}	}		boolean checkVacancy(int iHouse, int iFlat)	{		if(flats[iHouse-1][iFlat].getTenant().getName().equals("EMPTY FLAT"))			return true;					return false;	}		void Exit()	{		//Rewrite our data-file		System.out.println("-- Exiting, please wait whilst the application saves your information");				Out fOutput = new Out(sFilename);				int iHouse;		char cFlat;		String sName;		int iAmount;				for(int house = 0; house < NUM_HOUSES; house++)		{			for(int flat = 1; flat < NUM_FLATS; flat++)			{				iHouse = (house+1);				cFlat = (char)(flat+'A');				sName = flats[house][flat].getTenant().getName();				iAmount = flats[house][flat].getTenant().getAmount();								fOutput.outln(iHouse + ";" + cFlat + ";" + sName + ";" + iAmount + ";");			}		}				fOutput.outln(totalMonths + ";" + totalVacancyMonths + ";" + totalEarned + ";");				fOutput.close(); //Save to file			System.out.println("-> Finished saving your information. The application will now exit.");		System.exit(0);	}}public class Oblig3{	public static void main(String[] args)	{		Flatcomplex utsyn = new Flatcomplex("HaiHus.data");				utsyn.Menu();	}}