I just want to ask if this is correct? I believe I wrote back to the data
Question:
I just want to ask if this is correct? I believe I wrote back to the data file correctly, but I have never done a project like this and I want to be sure I didn't mess it up. Also, would I write the registration data back the same way as I did with the member data? Where would I include the class types that are listed? The code I have currently and the data files are below, thank you so much!
Add code to the exit_program method. In this method you will: Add the code to write the member data back to the data file member.txt. Add the code to write the registration data back to the data file register.txt. You will have to change the call to the exit_program method, passing the arrays and counts
package ymca; import javax.swing.JOptionPane; import java.io.*; import java.util.*; import java.text.*; public class ymca { public static void main(String[] args) { int mcount=-1,i; int [] code = new int[100]; String [] type = new String [100]; String [] name = new String [100]; int [] age = new int [100]; String [] gender = new String[100]; double [] payment = new double [100];
int rcount=-1; int [] regCodes = new int [100]; String [] regClasses = new String [100];
int selection; String snumber; mcount=read_member(mcount,code,name,type,age,gender,payment); rcount=read_registration(rcount, regCodes, regClasses); selection = menu(); while(selection !=4) { if(selection==1) Modify_member(); else if(selection==2) Modify_registration(); else if(selection==3) Report(mcount,code,name,type,age,gender,payment); selection = menu(); }//while loop exit_program(); System.exit(0); }//main method //******************************************************************* public static int read_member(int mcount,int[]code,String[]name,String[]type,int[]age,String[]gender,double[]payment) { int i; String newLine; try { //define a file variable for Buffered read BufferedReader member_file = new BufferedReader(new FileReader("member.txt")); //read lines in file until there are no more lines in the file to read while ((newLine = member_file.readLine()) != null) { //there is a "#" between each data item in each line StringTokenizer delimiter = new StringTokenizer(newLine,"#"); mcount=mcount+1; code[mcount] = Integer.parseInt(delimiter.nextToken()); name[mcount] =delimiter.nextToken(); type[mcount] =delimiter.nextToken(); age[mcount] = Integer.parseInt(delimiter.nextToken()); gender[mcount] =delimiter.nextToken(); payment[mcount] = Double.parseDouble(delimiter.nextToken()); }//while loop member_file.close(); }//end try catch (IOException error) { //there was an error on the file writing System.out.println("Error on file read " + error); }//error on read return mcount; } //******************************************************************* public static int read_registration(int rcount, int[]regCodes, String[]regClasses) { String newLine; try { BufferedReader register_file = new BufferedReader(new FileReader("register.txt")); while ((newLine = register_file.readLine()) != null) { StringTokenizer delimiter = new StringTokenizer(newLine,"#"); rcount=rcount+1; regCodes[rcount] = Integer.parseInt(delimiter.nextToken()); regClasses[rcount] = delimiter.nextToken(); } register_file.close(); } catch (IOException error) { System.out.println("Error on file read " +error); } return rcount; } //******************************************************************* public static int menu() { String snum; int selection; String Output = "Pittsburgh Area YMCA" + " " + "1. Add/Modify Member Information" + " " + "2. Add/Modify Class Registrations " + " " + "3. Report Section" + " " + "4. Exit the System"+ " " +" " + "Please make your seleciton"; snum = JOptionPane.showInputDialog(null, Output, "",JOptionPane.QUESTION_MESSAGE); selection = Integer.parseInt(snum); return selection; } //*******************************************************************
public static void Modify_member() { System.out.println("executing Modify_member"); } //*******************************************************************
public static void Modify_registration() { System.out.println("executing Modify_registration"); } //*******************************************************************
public static void Report(int mcount,int[]code,String[]name,String[]type,int[]age,String[]gender,double[]payment) { int selection,i; String value; String words= "Pittsburgh Area YMCA"+" " + "1. All Member Info"+" " + "2. Report 2"+" " + "3. Report 3"+" " + "4. Report 4"+" " + "5. Report 5"+" " + "6. Report 6"+" " + "7. Report 7"+" " + "8. Report 8"+" " + "9. Report 9"+" " + "10. Report 10"+" " + "11. Exit Report Menu"+" " + " Please make your selection "; value=JOptionPane.showInputDialog(null, words, "Input Data", JOptionPane.QUESTION_MESSAGE); selection=Integer.parseInt(value); while (selection != 11) { if (selection == 1) { System.out.println("All Member Information"); for(i=0;i<=mcount;++i) { System.out.println(code[i]+" "+name[i]+" "+type[i]+" "+age[i]+" "+gender[i]+" "+payment[i]); } }//end of report 1 else if (selection == 2) { System.out.println("Add Code here for Report #2"); }//end of report 2 else if (selection == 3) { System.out.println("Add Code here for Report #3"); }//end of report 3 else if (selection == 4) { System.out.println("Add Code here for Report #4"); }//end of report 4 else if (selection == 5) { System.out.println("Add Code here for Report #5"); }//end of report 5 else if (selection == 6) { System.out.println("Add Code here for Report #6"); }//end of report 6 else if (selection == 7) { System.out.println("Add Code here for Report #7"); }//end of report 7 else if (selection == 8) { System.out.println("Add Code here for Report #8"); }//end of report 8 else if (selection == 9) { System.out.println("Add Code here for Report #9"); }//end of report 9 else if (selection == 10) { System.out.println("Add Code here for Report #10"); }//end of report 10 value=JOptionPane.showInputDialog(null, words, "Input Data", JOptionPane.QUESTION_MESSAGE); selection=Integer.parseInt(value); }//end of while loop } //*******************************************************************
public static void exit_program(int mcount,int[]code,String[]name,String[]type,int[]age,String[]gender,double[]payment) { int i; try { BufferedWriter member_file = new BufferedWriter(new FileWriter("member.txt")); for (i=0; i<=mcount; ++i) { member_file.write(code[i]+"#"+name[i]+"#"+type[i]+"#"+age[i]+"#"+gender[i]+"#"+payment[i]+"#"); member_file.newLine(); } member_file.close(); } catch (IOException error) { System.out.println("Error on file write " +error); } } //*******************************************************************
}
member.txt file:
1111#Thomas Johnson#Single#23#Male#209.99# 2222#Jane Williams#Single#42#Female#199.50# 3333#John Atkins#Couple#25#Male#300# 4444#Gregory Thomas#Family#45#Male#450# 5555#Henry Green#Single#23#Male#199# 6666#William Hilling#Family#35#Male#325.00# 7777#Karen Power#Couple#42#Female#199.80# 8888#Marie Smith#Single#28#Female#209.00#
register.txt:
1111#Yoga# 1111#Karate# 1111#SwimmingI# 1111#AerobicsII# 2222#SwimmingII# 2222#Karate# 3333#SwimmingI# 3333#Yoga# 3333#AerobicsII# 4444#SwimmingI# 4444#Yoga# 4444#Karate# 5555#Karate# 5555#SwimmingII# 5555#Yoga# 6666#Yoga# 6666#Karate# 6666#SwimmingI# 7777#SwimmingII# 7777#Karate# 7777#AerobicsII# 8888#Yoga# 8888#AerobicsII# 8888#SwimmingII#
Managerial Accounting
ISBN: 978-1259024900
9th canadian edition
Authors: Ray Garrison, Theresa Libby, Alan Webb