okay so we have group program and my job was to make it only fool proof which I've done most of it and now my group wants me modify/change the program that we talked about and they chose me to do it even though I Said no and I can't..a little help would very much appreciated.
I want to change the program and output should be like this:
How many Jail Room will be created? 2How many Inmates per room? 2
JailRoom 1:
Inmate 1:
DetaineeNo:
Name:
Case:
sentence:
Inmate 2:
DetaineeNo:
Name:
Case:
sentence
JailRoom 2:
Inmate 1:
DetaineeNo:
Name
Case
sentence
status
Inmate 2:
DetaineeNo:
Name:
Case:
sentence:
import java.io.*;
import java.util.*;
public class JailInternFiles {
static Scanner kbd = new Scanner(System.in);
private Scanner reader,scan;
private JailIntern[] intns;
public JailInternFiles() throws IOException{
// To read the input file
reader = new Scanner (new File("JailInternRecords.txt"));
// this scanner is here to open again the text file
// because the "reader" cannot be opened once it is closed
scan = new Scanner(new File("JailInternRecords.txt"));
}
// Executes the chosen number
public void run() throws Exception {
intns = new JailIntern[checkEmpty()];
readFile();
int choice;
do {
menu();// displays the menu
choice = readChoice();// reads the choice
switch (choice) {
case 1:
storeJailIntern();
System.out.println("Press enter to continue. . .");
kbd.nextLine();
break;
case 2:
showJailInterns(intns);
System.out.println("\nPress enter to continue. . .");
kbd.nextLine();
break;
case 3:
showJailInterns(sortJailInternsAccordingToNames(intns));
System.out.println("\nPress enter to continue. . .");
kbd.nextLine();
break;
case 4:
showJailInterns(sortJailInternsAccordingToRoomNumbers(intns));
System.out.println("\nPress enter to continue. . .");
kbd.nextLine();
break;
case 5:
System.out.print("Enter the name to be searched: ");
String searchKey = kbd.nextLine();
if (isFound(intns, searchKey)) {
System.out.println(searchKey + " is in the database.");
} else if (scan.hasNextLine() == false) {
System.out.println("\nThe text file is empty.");
System.out.println("You should store jail intern records first.");
} else {
System.out.println(searchKey + " is not in the database.");
}
System.out.println("\nPress enter to continue. . .");
kbd.nextLine();
break;
case 6:
if ( scan.hasNextLine() == false ) {
System.out.println("\nThe text file is empty.");
System.out.println("You should store jail intern records first.");
} else {
printDataToFile(intns);
System.out.println("\nThe output file has been created successfully!");
}
System.out.println("\nPress enter to continue. . .");
kbd.nextLine();
}// end of switch
}while(choice != 7);
System.out.println("\nThank you for using this program.");
scan.close();
System.exit(0);
}// end of run method
/*
* Read each of the records contained in the text file
* and store them as elements of the array called intns
*/
public void readFile(){
int index = 0;
while (reader.hasNextLine()){
String[] temp = reader.nextLine().split(", ");
// Store the values to the following variables
String detaineeNo = temp[0];
String name = temp[1];
int roomNumber = Integer.parseInt(temp[2]);
String Case = temp[3];
String sentence = temp[4];
String status = temp[5];
// Use the values in instantiating JailIntern object
intns[index++] = new JailIntern(detaineeNo, name, roomNumber, Case, sentence, status);
}
reader.close();
}
/*
* this method prints the record
* into an ouput file
*/
public void printDataToFile(JailIntern[] s) throws IOException{
// To output records in an output file: JailInternOutput.txt
PrintWriter outFile = new PrintWriter(new FileWriter("JailInternRecordsOutput.txt"));
for (int i = 0; i < s.length; i++){
outFile.println(s[i]);
}
outFile.close();
}
// The main method
public static void main(String[] args) throws Exception{
JailInternFiles test = new JailInternFiles();
test.run();
}// end of main method
// This is the menu
private void menu() throws Exception{
System.out.println("This program can store and organize data of Jail Intern records.");
System.out.println("What do you want to do?");
System.out.println("1.) Store new jail intern record");
System.out.println("2.) Display records in original order");
System.out.println("3.) Display sorted records based on names");
System.out.println("4.) Display sorted records based on room numbers");
System.out.println("5.) Check if a jail intern record is in the database");
System.out.println("6.) Print data into a file.");
System.out.println("7.) Quit");
}
/*
* readChoice method asks the user to input what
* will be his choice in the categories
*/
public int readChoice() throws Exception{
boolean inputFlag = false;
int choice=0;
do {
try {
System.out.print("Input the number of your choice: ");
choice = Integer.parseInt(kbd.nextLine());
inputFlag = true;
} catch(Exception e) {
inputFlag = false;
}
if ( choice < 1 || choice > 7 ) {
System.out.println("\nInvalid input! You can only choose from 1 to 7 only.");
System.out.println("\nPress enter to continue. . .");
kbd.nextLine();
inputFlag = false;
}
}while( inputFlag = false );
return choice;
}// end of readChoice method
/*
* storeJailIntern method asks the user
* how many Jail Interns will be save and
* stores it in the text file
*/
public void storeJailIntern() throws Exception{
JailIntern[] list;
int count;
do {
System.out.print("How many rooms will be created? ");
count = Integer.parseInt(kbd.nextLine());
list = new JailIntern[count];
checkInside(count);
String enter = enter();
for ( int x = 0 ; x < list.length ; x++ ) {
System.out.println("\nFor room: " + (x+1) + ": ");
if ( enter.compareToIgnoreCase("") == 0 && x >= 1 )
enter = "\r\n";
FileWriter out = new FileWriter("JailInternRecords.txt", true);
BufferedWriter writer = new BufferedWriter(out);
writer.write(enter + (list[x] = readInterns()));// stores/appends data into the file
writer.close();
}// end of for
if ( count > 0 ) {
System.out.println();
System.out.println((count == 1 ? count + " new jail intern record " : count + " new jail intern records ")+"has been successfully saved!");
}
System.out.println(count <= 0 ? "You should enter one or more!" : "");// prints out if count is less than or equal to zero
}while(count == 0);
}// end of storeJailIntern method
/*
* readInterns method will ask the information
* of a certain jail intern
*/
public static JailIntern readInterns() throws IOException{
boolean inputFlag = false;
JailIntern[] list;
int number;
int roomNo;
String j;
System.out.print("How many interns will be in this room? ");
number = Integer.parseInt(kbd.nextLine());
list = new JailIntern[number];
for(int x = 0; x < list.length; x++) {
System.out.print("Intern " + (x+1) + ":");
System.out.print("Enter the detainee number: ");
String detaineeNo = kbd.nextLine();
System.out.print("Enter the name: ");
String name = kbd.nextLine();
int roomNo = 0;
while( inputFlag == false ) {
try {
System.out.print("Enter the room number: ");
roomNo = Integer.parseInt(kbd.nextLine());
inputFlag = true;
} catch (NumberFormatException e) {
System.out.println("Invalid integer! Please try again.");
inputFlag = false;
}
}// end of while
System.out.print("Enter the case: ");
String Case = kbd.nextLine();
System.out.print("Enter the sentence: ");
String sentence = kbd.nextLine();
String valid = "RD";
String status = "";
do {
System.out.print("Enter the status([R]eleased/[D]etained): ");
status = kbd.nextLine();
if(valid.indexOf(status) == -1)
System.out.println("Invalid choice! please input UpperCase R or D only");
}while(valid.indexOf(status) == -1);
JailIntern j = new JailIntern(list[x].detaineeNo, list[x].name, list[x].roomNo, list[x].Case, list[x].sentence, list[x].status);
list[x] = j;
}
return j;
}// end of readInterns method
/*
* checkInside method checks if the text file
* is empty and if it is empty, it will put
* the number 1 which is the starting number.
* And if there is already a record, it will just
* replace the current count of jail intern records
*/
public void checkInside(int number) throws Exception {
String count;
if ( scan.hasNextLine() == false ) {
count = Integer.toString(number);
FileWriter out = new FileWriter("JailInternRecords.txt", true);
BufferedWriter writer = new BufferedWriter(out);
writer.write(count);// stores/appends data into the file
writer.close();
} else {
String currentCount = scan.nextLine();
int newCount = number+(Integer.parseInt(currentCount));
String newCountString = Integer.toString(newCount);
File file = new File("JailInternRecords.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while((line = reader.readLine()) != null) {
oldtext += line + "\r\n";
}
reader.close();
String newtext = oldtext.replaceAll(currentCount, newCountString);// to replace the current number of the records
FileWriter writer = new FileWriter("JailInternRecords.txt");// will be written on the text file
writer.write(newtext);
writer.close();// to close the text file
}
}// end of checkInside method
/*
* enter method fixes the placing
* of the records to avoid space and
* enter disorders
*/
public String enter() {
String en;
if ( scan.hasNextLine() == false ) {
en = "\r\n";
} else {
en = "";
}
return en;
}// end of enter method
/*
* shows the jail intern records in original order
* or any sorted methods
*/
private void showJailInterns(JailIntern[] s){
if ( scan.hasNextLine() == false ) {
System.out.println("\nThe text file is empty.");
System.out.println("You should store jail intern records first.");
} else {
for (int x=0; x<s.length; x++) {
System.out.println(s[x]);// toString() method is called automatically
}
}
return;
}// end of showJailInterns method
private JailIntern[] copyArray(JailIntern[] source){
JailIntern[] copiedArray = new JailIntern[source.length];
for (int x=0; x<source.length; x++){
JailIntern sCopy = new JailIntern(source[x].getDetaineeNo(), source[x].getName(),source[x].getRoomNumber(),source[x].getCase(), source[x].getSentence(), source[x].getStatus());
copiedArray[x] = sCopy;
}
return copiedArray;
}// end of copyArray method
/*
* this method sorts the JailIntern records
* based on their names
*/
private JailIntern[] sortJailInternsAccordingToNames(JailIntern[] array){
JailIntern[] s = copyArray(array);
int minIndex=0;
for (int x=0; x < s.length-1; x++){
minIndex = x;
for (int y=x+1; y<s.length; y++)
if (s[minIndex].getName().compareToIgnoreCase(s[y].getName())>0)
minIndex = y;
if (minIndex != x){
JailIntern temp = s[x];
s[x] = s[minIndex];
s[minIndex] = temp;
}//end of if
}// end of first for loop
return s;
}// end of sortJailInternsAccordingToNames method
/*
* sortJailInternsAccordingToRooms method will sort
* jail intern records based on room numbers
*/
private JailIntern[] sortJailInternsAccordingToRoomNumbers(JailIntern[] array){
JailIntern[] s = copyArray(array);
int minIndex=0;
for (int x=0; x < s.length-1; x++){
minIndex = x;
for (int y=x+1; y<s.length; y++)
if (s[minIndex].getRoomNumber()>(s[y].getRoomNumber()))
minIndex = y;
if (minIndex != x){
JailIntern temp = s[x];
s[x] = s[minIndex];
s[minIndex] = temp;
}
}// end of first for
return s;
}// end of sortJailInternsAccordingToRooms method
/*
* checkEmpty method checks if the text file is empty.
* if empty, it will set n to 0
*/
public int checkEmpty() {
int n;
if ( scan.hasNextLine() == false ) {
n = 0;
} else {
n = Integer.parseInt(reader.nextLine());
}
return n;
}// end checkEmpty method
/*
* this method finds if the name of the jail intern
* is in the database and returns it to true or false
*/
private boolean isFound(JailIntern[] s, String nameToSearch){
boolean found = false;
for (int x=0; x<s.length && !found; x++){
if (s[x].getName().equalsIgnoreCase(nameToSearch))
found = true;
}
return found;
}// end of isFound method
}// end of JailInternArithmetic Class
and here's the other class
public class JailIntern {
//These are the data members
public String detaineeNo;
public String name;
public int roomNumber;
public String Case;
public String sentence;
public String status;
// Constructor
public JailIntern(String detaineeNo, String name, int roomNumber, String Case, String sentence, String status) {
this.detaineeNo = detaineeNo;
this.name = name;
this.roomNumber = roomNumber;
this.Case = Case;
this.sentence = sentence;
this.status = status;
}
// Getter/Accessor methods
public String getDetaineeNo(){
return detaineeNo;
}
public String getName(){
return name;
}
public int getRoomNumber(){
return roomNumber;
}
public String getCase(){
return Case;
}
public String getSentence(){
return sentence;
}
public String getStatus() {
return status;
}
// Setter/Mutator methods
public void setDetaineeNo(String dN){
detaineeNo = dN;
}
public void setName(String n){
name = n;
}
public void setRoomNumber(int rN){
roomNumber = rN;
}
public void setCase(String c){
Case = c;
}
public void setSentence(String s){
sentence = s;
}
public void setStatus(String st) {
status = st;
}
// Prints the result
public String toString(){
return (detaineeNo + ", " + name + ", " + roomNumber + ", "+ Case + ", " + sentence + ", " + status);
}
}// end of JailIntern class
and there's an empty text file that is together with the 2 programs.