Java Code Example - Prison Management System
Class assignment in which we have to develop a management system using Java, to manage a 100 room prison.
/*Student name: Joe Caulfield
* Student number: 20050493
* Date: 15-02-2012
* Purpose:Write a program to manage a 100-room prison.*/
import java.util.Scanner;
public class Prison {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String fName[] = new String[3]; // Forename
String lName[] = new String[3]; // Surname
int prisonerId[] = new int[3]; // Prisoner Id
int maxSentence[] = new int[3]; // Maximum Sentence
int prisonerAge[] = new int[3]; // Prisoner's Age
int remission[] = new int[3]; // Remission Sentence
// handle user commands
int menuItem; // Menu System
boolean quit = false;
// print menu
System.out.println("1.Enter Prisoner Information");
System.out.println("2.Maximum Sentence");
System.out.println("3.Oldest Prisoner");
System.out.println("4.Youngest Prisoner's Id");
System.out.println("5.Average Age of prisoner's");
System.out.println("6.Remission");
System.out.println("7.Quit");
do
{
System.out.print("\n ");
System.out.print("Choose menu item: ");
menuItem = keyboard.nextInt();
switch (menuItem) {
case 1:
System.out.println("You've chosen: ");
{
prisonerInfo(keyboard, fName, lName, prisonerId,
maxSentence, prisonerAge); // Calls the prisoner information mathod
}
break;
case 2:
System.out.println("You've chosen: ");
{
maxprisonSentence(fName, maxSentence, prisonerAge); // Calls the maximum prison sentence method
}
break;
case 3:
System.out.println("You've chosen: ");
{
oldestPrisoner(fName, prisonerAge); // Calls the oldest prisoner method
}
break;
case 4:
System.out.println("You've chosen: ");
{
youngestPrisonerId(fName, prisonerId, prisonerAge); // Calls the youngest prisoners Id method
}
break;
case 5:
System.out.println("You've chosen: ");
{
averageAge(prisonerAge); // Calss the prisoner's average age method
}
break;
case 6:
System.out.println("You've chosen: ");
{
prisonerRemission(maxSentence, prisonerAge, remission); // Calls the sentence remission method
}
break;
case 7:
quit = true; // Exit the program
break;
default:
System.out.println("Invalid choice."); // If the user enters in invalid number like 0, 8, 9, 10...
}
} while (!quit); // End of do while
System.out.println("Have a nice day!"); // Message that is outputed when the user exits the program
} // End of main
//Asks the user to enter in the prisoner's details and stores the details within the arrays
static int prisonerInfo(Scanner keyboard, String[] fName,
String[] lName, int[] prisonerId, int[] maxSentence, int[] prisonerAge)
{
int cellNumber; // cellNumber is the index
for (cellNumber = 0; cellNumber < 3; cellNumber++)
{
System.out.println("___________Enter in prisoner information___________ \n");
System.out.println("Enter in prisoner's Id for prisoner in room: "+ cellNumber);
prisonerId[cellNumber] = keyboard.nextInt();
System.out.println("Enter in prisoner's First Name: ");
fName[cellNumber] = keyboard.next();
System.out.println("Enter in prisoner's Last name: ");
lName[cellNumber] = keyboard.next();
System.out.println("Enter in prisoner's age");
prisonerAge[cellNumber] = keyboard.nextInt();
System.out.println("Enter in prisoner's orginal sentence");
maxSentence[cellNumber] = keyboard.nextInt();
} //End of while
return cellNumber;
}//End of while
// Outputs the prisoner with the longest sentence
static void maxprisonSentence(String[] fName, int[] maxSentence,int[] prisonerAge)
{
int maximumSentence;
int cellMaxSentence;
int cellNumber; // cellNumber is the index
maximumSentence = 0;
cellMaxSentence = 0;
cellNumber = 0;
while (cellNumber < 3)
{
if (maxSentence[cellNumber] > maximumSentence) // if maximum sentence is greater than the maximum sentence
{
maximumSentence = maxSentence[cellNumber]; // fills the maxSentence array
cellMaxSentence = cellNumber; // cell max sentence is equal to the cell number
}
cellNumber++;
} // End of while
System.out.println("\n"); // skips a line
System.out.println("___________Prisoner with longest Sentence___________");
System.out.println("\n This prisoner is: " + fName[cellMaxSentence]); //outputs the prisoner's forename
System.out.println("\n Cell Number for the prisoner with the longest sentence is: "+ cellMaxSentence); // outputs the cell number
System.out.println("\n The length of the sentence is: "+ maximumSentence); //outputs the prison sentence
} //End of while
// Outputs the oldest prisoner
static void oldestPrisoner(String[] fName, int[] prisonerAge)
{
int prisonersAge;
int cellMaxSentence;
int cellNumber; // cellNumber is the index
prisonersAge = 0; // start at the left most age of the prisoner
cellMaxSentence = 0; // start at the left most sentence of the prisoner
cellNumber = 0;
while (cellNumber < 3) {
if (prisonerAge[cellNumber] > prisonersAge)
{
prisonersAge = prisonerAge[cellNumber]; //fills the prisonerAge array
cellMaxSentence = cellNumber;
}
cellNumber = cellNumber + 1; // go to the next cell room
}//Endo of while
System.out.println("\n"); // skips a line
System.out.println("___________Oldest prisoner___________"); // out puts a title
System.out.println("\n This prisoner is: " + fName[cellMaxSentence]);
System.out.println("\n The age for the oldest prisoner is: "+ prisonersAge); // out puts the age of the oldest prisoner
}//End of method
// Outputs the name, Id number and cell room number of the youngest prisoner
static void youngestPrisonerId(String[] fName, int[] prisonerId,
int[] prisonerAge)
{
int prisonersAge;
int cellMaxSentence;
int cellNumber; // cellNumber is the index
prisonersAge = 0;
cellMaxSentence = 0;
cellNumber = 0;
while (cellNumber < 3)
{
if (prisonerAge[cellNumber] < prisonersAge)
{
prisonerAge[cellNumber] = prisonersAge; //fills the prisonerAge array
cellMaxSentence = cellNumber;
}
cellNumber = cellNumber + 1;
} // End of while
System.out.println("\n"); // skips a line
System.out.println("___________Youngest prisoner___________"); // outputs a title
System.out.println("\n This prisoner is: " + fName[cellMaxSentence]); //outputs the prisoner's forename
System.out.println("\n The Id number for the youngest prisoner is: "+ prisonerId[cellMaxSentence]); /* outputs the Id of the
youngest prisoner*/
} // End of method
// Outputs all prisoners average age by dividing the ages by the cell rooms that are being used
static void averageAge(int[] prisonerAge)
{
int averageAge;
int cellNumber; // cellNumber is the index
averageAge = 0;
cellNumber = 0;
while (cellNumber < 3)
{
averageAge = averageAge + prisonerAge[cellNumber]; // average age calculation
cellNumber = cellNumber + 1; // moves onto the next cell
}
System.out.println("___________Prisoner's Average Age___________ \n");
System.out.println("The average age of all prisoners is: " + averageAge/cellNumber); // divides the average age by the number of cells used
}//End of method
//Outputs the prisoner's remission sentence
static void prisonerRemission(int[] maxSent, int[] prisonerAge,
int[] remission)
{
int cellNumber; // cellNumber is the index
cellNumber=0;
while (cellNumber < 3)
{
remission[cellNumber] = maxSent[cellNumber]/2; /* fills the remission array and halfs the maximum sentences and outputing
the halfed sentence as the remission*/
if (prisonerAge[cellNumber] >= 70) // prisoner's age is greater than or equal to 70
{
System.out.println("Prisoner in cell " + cellNumber +" on good Behaviour will get out soon");
System.out.println( "\n");
System.out.println( "Prisoner in cell " + cellNumber + " on bad Behaviour will serve full sentence and a extra "
+ remission[cellNumber] + " years");
System.out.println( "\n");
}
else if (prisonerAge[cellNumber] <= 70) // prisoner's age is less than or equal to 69
{
System.out.println(" Prisoner in cell " + cellNumber + " on good Behaviour will get out in " + remission[cellNumber] );
System.out.println( "\n");
System.out.println("Prisoner in cell " + cellNumber +" on bad Behaviour will serve full sentence and a extra "
+ remission[cellNumber] + " years");
System.out.println( "\n");
}
cellNumber++;
} // End of while
} // End of method
} // End of class
/*Student name: Joe Caulfield
* Student number: 20050493
* Date: 15-02-2012
* Purpose:Write a program to manage a 100-room prison.*/
import java.util.Scanner;
public class Prison {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String fName[] = new String[3]; // Forename
String lName[] = new String[3]; // Surname
int prisonerId[] = new int[3]; // Prisoner Id
int maxSentence[] = new int[3]; // Maximum Sentence
int prisonerAge[] = new int[3]; // Prisoner's Age
int remission[] = new int[3]; // Remission Sentence
// handle user commands
int menuItem; // Menu System
boolean quit = false;
// print menu
System.out.println("1.Enter Prisoner Information");
System.out.println("2.Maximum Sentence");
System.out.println("3.Oldest Prisoner");
System.out.println("4.Youngest Prisoner's Id");
System.out.println("5.Average Age of prisoner's");
System.out.println("6.Remission");
System.out.println("7.Quit");
do
{
System.out.print("\n ");
System.out.print("Choose menu item: ");
menuItem = keyboard.nextInt();
switch (menuItem) {
case 1:
System.out.println("You've chosen: ");
{
prisonerInfo(keyboard, fName, lName, prisonerId,
maxSentence, prisonerAge); // Calls the prisoner information mathod
}
break;
case 2:
System.out.println("You've chosen: ");
{
maxprisonSentence(fName, maxSentence, prisonerAge); // Calls the maximum prison sentence method
}
break;
case 3:
System.out.println("You've chosen: ");
{
oldestPrisoner(fName, prisonerAge); // Calls the oldest prisoner method
}
break;
case 4:
System.out.println("You've chosen: ");
{
youngestPrisonerId(fName, prisonerId, prisonerAge); // Calls the youngest prisoners Id method
}
break;
case 5:
System.out.println("You've chosen: ");
{
averageAge(prisonerAge); // Calss the prisoner's average age method
}
break;
case 6:
System.out.println("You've chosen: ");
{
prisonerRemission(maxSentence, prisonerAge, remission); // Calls the sentence remission method
}
break;
case 7:
quit = true; // Exit the program
break;
default:
System.out.println("Invalid choice."); // If the user enters in invalid number like 0, 8, 9, 10...
}
} while (!quit); // End of do while
System.out.println("Have a nice day!"); // Message that is outputed when the user exits the program
} // End of main
//Asks the user to enter in the prisoner's details and stores the details within the arrays
static int prisonerInfo(Scanner keyboard, String[] fName,
String[] lName, int[] prisonerId, int[] maxSentence, int[] prisonerAge)
{
int cellNumber; // cellNumber is the index
for (cellNumber = 0; cellNumber < 3; cellNumber++)
{
System.out.println("___________Enter in prisoner information___________ \n");
System.out.println("Enter in prisoner's Id for prisoner in room: "+ cellNumber);
prisonerId[cellNumber] = keyboard.nextInt();
System.out.println("Enter in prisoner's First Name: ");
fName[cellNumber] = keyboard.next();
System.out.println("Enter in prisoner's Last name: ");
lName[cellNumber] = keyboard.next();
System.out.println("Enter in prisoner's age");
prisonerAge[cellNumber] = keyboard.nextInt();
System.out.println("Enter in prisoner's orginal sentence");
maxSentence[cellNumber] = keyboard.nextInt();
} //End of while
return cellNumber;
}//End of while
// Outputs the prisoner with the longest sentence
static void maxprisonSentence(String[] fName, int[] maxSentence,int[] prisonerAge)
{
int maximumSentence;
int cellMaxSentence;
int cellNumber; // cellNumber is the index
maximumSentence = 0;
cellMaxSentence = 0;
cellNumber = 0;
while (cellNumber < 3)
{
if (maxSentence[cellNumber] > maximumSentence) // if maximum sentence is greater than the maximum sentence
{
maximumSentence = maxSentence[cellNumber]; // fills the maxSentence array
cellMaxSentence = cellNumber; // cell max sentence is equal to the cell number
}
cellNumber++;
} // End of while
System.out.println("\n"); // skips a line
System.out.println("___________Prisoner with longest Sentence___________");
System.out.println("\n This prisoner is: " + fName[cellMaxSentence]); //outputs the prisoner's forename
System.out.println("\n Cell Number for the prisoner with the longest sentence is: "+ cellMaxSentence); // outputs the cell number
System.out.println("\n The length of the sentence is: "+ maximumSentence); //outputs the prison sentence
} //End of while
// Outputs the oldest prisoner
static void oldestPrisoner(String[] fName, int[] prisonerAge)
{
int prisonersAge;
int cellMaxSentence;
int cellNumber; // cellNumber is the index
prisonersAge = 0; // start at the left most age of the prisoner
cellMaxSentence = 0; // start at the left most sentence of the prisoner
cellNumber = 0;
while (cellNumber < 3) {
if (prisonerAge[cellNumber] > prisonersAge)
{
prisonersAge = prisonerAge[cellNumber]; //fills the prisonerAge array
cellMaxSentence = cellNumber;
}
cellNumber = cellNumber + 1; // go to the next cell room
}//Endo of while
System.out.println("\n"); // skips a line
System.out.println("___________Oldest prisoner___________"); // out puts a title
System.out.println("\n This prisoner is: " + fName[cellMaxSentence]);
System.out.println("\n The age for the oldest prisoner is: "+ prisonersAge); // out puts the age of the oldest prisoner
}//End of method
// Outputs the name, Id number and cell room number of the youngest prisoner
static void youngestPrisonerId(String[] fName, int[] prisonerId,
int[] prisonerAge)
{
int prisonersAge;
int cellMaxSentence;
int cellNumber; // cellNumber is the index
prisonersAge = 0;
cellMaxSentence = 0;
cellNumber = 0;
while (cellNumber < 3)
{
if (prisonerAge[cellNumber] < prisonersAge)
{
prisonerAge[cellNumber] = prisonersAge; //fills the prisonerAge array
cellMaxSentence = cellNumber;
}
cellNumber = cellNumber + 1;
} // End of while
System.out.println("\n"); // skips a line
System.out.println("___________Youngest prisoner___________"); // outputs a title
System.out.println("\n This prisoner is: " + fName[cellMaxSentence]); //outputs the prisoner's forename
System.out.println("\n The Id number for the youngest prisoner is: "+ prisonerId[cellMaxSentence]); /* outputs the Id of the
youngest prisoner*/
} // End of method
// Outputs all prisoners average age by dividing the ages by the cell rooms that are being used
static void averageAge(int[] prisonerAge)
{
int averageAge;
int cellNumber; // cellNumber is the index
averageAge = 0;
cellNumber = 0;
while (cellNumber < 3)
{
averageAge = averageAge + prisonerAge[cellNumber]; // average age calculation
cellNumber = cellNumber + 1; // moves onto the next cell
}
System.out.println("___________Prisoner's Average Age___________ \n");
System.out.println("The average age of all prisoners is: " + averageAge/cellNumber); // divides the average age by the number of cells used
}//End of method
//Outputs the prisoner's remission sentence
static void prisonerRemission(int[] maxSent, int[] prisonerAge,
int[] remission)
{
int cellNumber; // cellNumber is the index
cellNumber=0;
while (cellNumber < 3)
{
remission[cellNumber] = maxSent[cellNumber]/2; /* fills the remission array and halfs the maximum sentences and outputing
the halfed sentence as the remission*/
if (prisonerAge[cellNumber] >= 70) // prisoner's age is greater than or equal to 70
{
System.out.println("Prisoner in cell " + cellNumber +" on good Behaviour will get out soon");
System.out.println( "\n");
System.out.println( "Prisoner in cell " + cellNumber + " on bad Behaviour will serve full sentence and a extra "
+ remission[cellNumber] + " years");
System.out.println( "\n");
}
else if (prisonerAge[cellNumber] <= 70) // prisoner's age is less than or equal to 69
{
System.out.println(" Prisoner in cell " + cellNumber + " on good Behaviour will get out in " + remission[cellNumber] );
System.out.println( "\n");
System.out.println("Prisoner in cell " + cellNumber +" on bad Behaviour will serve full sentence and a extra "
+ remission[cellNumber] + " years");
System.out.println( "\n");
}
cellNumber++;
} // End of while
} // End of method
} // End of class
Comments
Post a Comment