Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this existing code of Java. Make these changes and add some options as shown bellow: Summary: We will add additional functionality

I need help with this existing code of Java. Make these changes and add some options as shown bellow:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Summary: We will add additional functionality to our Week 2 assignment by using serialization with csv. You will modify your program to write your variables out as a CSV. This will create a studentdatabase.csv file that will store your student information. Rather than overwrite the file, we will now append to the file so that we can add multiple students to this database. Week 1 and 2 assignment guidelines are assumed to be followed. If you have any questions or had any issues with week 1 or 2, reach out for help. When our user selects to Write Data Ie File, it will append the students information like so: Namexeat.Era. John, Senior, 3.8 Joe, Sophomore, 4.0 Phil, Freshman 4.0 Sarah, Junior 3.5 Additional functionality will be added to our application to allow the administrator to "Search by name". Search by Name will be Option 7. When the user enters option, they should be asked "Enter the students name". Your program should search the names of the students in the database and print out the results based on their name. *IMPORTANT* data duplication is a real world problem. What do we do if there are 3 John Smiths? At this time we won't need to solve this problem. You can take 1 of 2 approaches here to get full credit. Assume there are 2 John Smith's in the studentdatabase.csv file. 1) Print out the data for both. 2) Print out the data for the first one found. Optional Extra Credit: An gih option will be added to "delete" a student from the database. Much like option 7 the user will enter a name, and the corresponding student will be deleted. Note, just as with option 7, you can handle duplicates the same way. Delete both, or delete the first one found. Requirements Interface: Same as week 1 with the following changes. Menu: 1. Enter The Students Name: 2. Enter the Students Academic Year: 3. Enter the Students GPA: 4. Print Current Working Student 5. Write Data to File 6. Read Data From File 7. Search by Student Name 8. OPTION EXTRA CREDI: Delete Student 9. Exit As with week 2 user cannot write to database until they've answered all the questions. Page 1 of 4 import java.io.File; import java.io.FileNotFoundException; import java.io.Filelriter; import java.io.IOException; import java.io. Printwriter import java.util.Scanner; public class Main public static void main(String[] args) throws IOException, InterruptedException { // Required variables int choice; String studentName" String student Year double studentGPA-1; // while loop repeats until user wants to exit do // Displays menu and asks for user choice printMenu(); Scanner s = new Scanner(System.in); choice - Integer.parseInt(s.nextLine().trim()); switch (choice) case 1: // Reads student name System.out.print("Enter Students Name: "); student Nane - S.nextLine().trim(); break; case 2: // Reads student year and validates it System.out.print("Enter Students Acadenic Year: "); student Year - S.nextLine().trim(); validateInput (studentane, studentYear, 0); break; case 3: // Reads student spa and validates it System.out.print("Enter the Students GPA: "); studentGPA - Double.parseDouble(s.nextLine().trim(); validateInput (studentame, "Freshman", studentGPA); break; case 4: // If all data entered by user is valid if (validateInput (studentName, student Year, studentGPA)) { // prints student data System.out.println("The students name is: studentName); System.out.println("The students academic year is: " + studentYear); System.out.println("The students GPA is: " + studentGPA); System.out.print("Type clear to clear the terminal screen to continue: "); String clear - S.nextLine().trim(); // if user types clear, then it clears the screen and continues if (clear.equals("clear")) { if (System.getProperty (cs.name").contains ("Windows")) new ProcessBuilder("end", "/", "cls").inherit100.start().waitfor(); else Runtime.getRuntime().exec("clear"); studentName" studentYear - studentGPA = -1; } break; case 5: // writes data entered by user to an output file // asks user for complete file path System.out.print("Enter the full path for the output file: "); String filename - S.nextLine().trim(); writeToFile(filename, studentNane, studentyear, studentGPA); break; case 6: // reads student data from an user entered file 11 asks user for complete file path System.out.print("Enter the full path for the input file: "); filename - 5.nextLine().trim(); String line - readFronFile(filename); // we need to split the line by comma String[] data - line.split(""); studentName = data[@]; student Year - data[1]; student GPA - Double.parseDouble(data[2]); break; case 7: // Exit System.exit(); default: System.out.println("invalid option. Try again!"); > > while (choice - 7); } * Method that displays menu for the user public static void printMenu() { // Displays menu System.out.print("1. Enter Students Name " + "2. Enter Students Academic Year " + "3. Enter Students GPA " + "4. Print Current Student " + "5. Write Data to File " + "6. Read Data from File " + "7. Exit " + "Please enter which number you want to answer: "); > * Method that validates whether the user entered data for a student is correct or not. * @param name @param year @param pa return either true or false */ public static boolean validateInput (String nane, String year, double spa) { // if either student year is empty or GPA is not entered // Displays appropriate message and return false if (year.equals("")) { System.out.println("Please enter academic year 1"); return false; ) else 14 cepe 1) System.out.println("Please enter the students GPA"); return false } else // Otherwise try validates and throws exception + (p 4.0) throw new exceptional students goa.) // validates student year to notch any of the values 'Freshman', 'Sophonore', 'Senior', 'Junior 1 if given year doesn't match any of those values throws Exception 1+ year.equals IgnoreCase("Freshman") && tyear.equalsignoreCase("Sophomore") & tyear.equalsignorecasensor) 46 year.equalsignorecase("Sensor >> throw new Exception("Invalid academic year."); > catch (Exception ex) System.out.printan(ex.toString()); 11 return true if data is valid return true; Method that writes the student name, year and goa, passed in as parameters, to the output file 'leane .paran Filename complete file path para nane para year Para spa throws Save. Sotoexception any error occurs during writing data to file public static void writeToFile(String filenane, String nane, String year, double ga) throws IOException / Fleriter and Printeriter objects to write data to file Fleister Printiriter on the file in append node(true) new leurster(new File(filename), true); 11 create a printwriter coject using the devriter object created above 1 write the data to the faste line Separator() appends new line to the file pw.write(none year oa Systen. 1.neSeparator(); // close all the resources pw.flush(); fw.close(); pw.close(); Method that reads data from file 'Filename into variables 'name', 'year' and 'p paran Filename complete file path return a line read from the input file public static String readFrom 11e(String filenane) { // Scanner object to read from File Scanner fleReader; // String to store the data line from the file String line." try 1/ open a strean to read from the file fileReader new Scanner(new File(fllenane); // If file is empty 17 (fleReader.haslextLine()) { System.out.println("Flle is empty!"); return": else 11ne fileReader.nextLine().trim(); // finally we close the Scanner resource fileReader.close() > catch (FileNotFoundException inte) // if file not found, throws an exception System.out.println("File" filename + " cannot be found. Exiting.."); System.exit(); return line Page 4 of 4 Summary: We will add additional functionality to our Week 2 assignment by using serialization with csv. You will modify your program to write your variables out as a CSV. This will create a studentdatabase.csv file that will store your student information. Rather than overwrite the file, we will now append to the file so that we can add multiple students to this database. Week 1 and 2 assignment guidelines are assumed to be followed. If you have any questions or had any issues with week 1 or 2, reach out for help. When our user selects to Write Data Ie File, it will append the students information like so: Namexeat.Era. John, Senior, 3.8 Joe, Sophomore, 4.0 Phil, Freshman 4.0 Sarah, Junior 3.5 Additional functionality will be added to our application to allow the administrator to "Search by name". Search by Name will be Option 7. When the user enters option, they should be asked "Enter the students name". Your program should search the names of the students in the database and print out the results based on their name. *IMPORTANT* data duplication is a real world problem. What do we do if there are 3 John Smiths? At this time we won't need to solve this problem. You can take 1 of 2 approaches here to get full credit. Assume there are 2 John Smith's in the studentdatabase.csv file. 1) Print out the data for both. 2) Print out the data for the first one found. Optional Extra Credit: An gih option will be added to "delete" a student from the database. Much like option 7 the user will enter a name, and the corresponding student will be deleted. Note, just as with option 7, you can handle duplicates the same way. Delete both, or delete the first one found. Requirements Interface: Same as week 1 with the following changes. Menu: 1. Enter The Students Name: 2. Enter the Students Academic Year: 3. Enter the Students GPA: 4. Print Current Working Student 5. Write Data to File 6. Read Data From File 7. Search by Student Name 8. OPTION EXTRA CREDI: Delete Student 9. Exit As with week 2 user cannot write to database until they've answered all the questions. Page 1 of 4 import java.io.File; import java.io.FileNotFoundException; import java.io.Filelriter; import java.io.IOException; import java.io. Printwriter import java.util.Scanner; public class Main public static void main(String[] args) throws IOException, InterruptedException { // Required variables int choice; String studentName" String student Year double studentGPA-1; // while loop repeats until user wants to exit do // Displays menu and asks for user choice printMenu(); Scanner s = new Scanner(System.in); choice - Integer.parseInt(s.nextLine().trim()); switch (choice) case 1: // Reads student name System.out.print("Enter Students Name: "); student Nane - S.nextLine().trim(); break; case 2: // Reads student year and validates it System.out.print("Enter Students Acadenic Year: "); student Year - S.nextLine().trim(); validateInput (studentane, studentYear, 0); break; case 3: // Reads student spa and validates it System.out.print("Enter the Students GPA: "); studentGPA - Double.parseDouble(s.nextLine().trim(); validateInput (studentame, "Freshman", studentGPA); break; case 4: // If all data entered by user is valid if (validateInput (studentName, student Year, studentGPA)) { // prints student data System.out.println("The students name is: studentName); System.out.println("The students academic year is: " + studentYear); System.out.println("The students GPA is: " + studentGPA); System.out.print("Type clear to clear the terminal screen to continue: "); String clear - S.nextLine().trim(); // if user types clear, then it clears the screen and continues if (clear.equals("clear")) { if (System.getProperty (cs.name").contains ("Windows")) new ProcessBuilder("end", "/", "cls").inherit100.start().waitfor(); else Runtime.getRuntime().exec("clear"); studentName" studentYear - studentGPA = -1; } break; case 5: // writes data entered by user to an output file // asks user for complete file path System.out.print("Enter the full path for the output file: "); String filename - S.nextLine().trim(); writeToFile(filename, studentNane, studentyear, studentGPA); break; case 6: // reads student data from an user entered file 11 asks user for complete file path System.out.print("Enter the full path for the input file: "); filename - 5.nextLine().trim(); String line - readFronFile(filename); // we need to split the line by comma String[] data - line.split(""); studentName = data[@]; student Year - data[1]; student GPA - Double.parseDouble(data[2]); break; case 7: // Exit System.exit(); default: System.out.println("invalid option. Try again!"); > > while (choice - 7); } * Method that displays menu for the user public static void printMenu() { // Displays menu System.out.print("1. Enter Students Name " + "2. Enter Students Academic Year " + "3. Enter Students GPA " + "4. Print Current Student " + "5. Write Data to File " + "6. Read Data from File " + "7. Exit " + "Please enter which number you want to answer: "); > * Method that validates whether the user entered data for a student is correct or not. * @param name @param year @param pa return either true or false */ public static boolean validateInput (String nane, String year, double spa) { // if either student year is empty or GPA is not entered // Displays appropriate message and return false if (year.equals("")) { System.out.println("Please enter academic year 1"); return false; ) else 14 cepe 1) System.out.println("Please enter the students GPA"); return false } else // Otherwise try validates and throws exception + (p 4.0) throw new exceptional students goa.) // validates student year to notch any of the values 'Freshman', 'Sophonore', 'Senior', 'Junior 1 if given year doesn't match any of those values throws Exception 1+ year.equals IgnoreCase("Freshman") && tyear.equalsignoreCase("Sophomore") & tyear.equalsignorecasensor) 46 year.equalsignorecase("Sensor >> throw new Exception("Invalid academic year."); > catch (Exception ex) System.out.printan(ex.toString()); 11 return true if data is valid return true; Method that writes the student name, year and goa, passed in as parameters, to the output file 'leane .paran Filename complete file path para nane para year Para spa throws Save. Sotoexception any error occurs during writing data to file public static void writeToFile(String filenane, String nane, String year, double ga) throws IOException / Fleriter and Printeriter objects to write data to file Fleister Printiriter on the file in append node(true) new leurster(new File(filename), true); 11 create a printwriter coject using the devriter object created above 1 write the data to the faste line Separator() appends new line to the file pw.write(none year oa Systen. 1.neSeparator(); // close all the resources pw.flush(); fw.close(); pw.close(); Method that reads data from file 'Filename into variables 'name', 'year' and 'p paran Filename complete file path return a line read from the input file public static String readFrom 11e(String filenane) { // Scanner object to read from File Scanner fleReader; // String to store the data line from the file String line." try 1/ open a strean to read from the file fileReader new Scanner(new File(fllenane); // If file is empty 17 (fleReader.haslextLine()) { System.out.println("Flle is empty!"); return": else 11ne fileReader.nextLine().trim(); // finally we close the Scanner resource fileReader.close() > catch (FileNotFoundException inte) // if file not found, throws an exception System.out.println("File" filename + " cannot be found. Exiting.."); System.exit(); return line Page 4 of 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Understanding Group Leadership Culture and Group Leadership

Answered: 1 week ago