C++ Assignment: (Could you give me the whole solution? Thanks a lot.) Create an
ID: 666379 • Letter: C
Question
C++ Assignment: (Could you give me the whole solution? Thanks a lot.)
Create an application to manipulate an array of student record objects. A student record will consist of a name (first, middle, and last), an ID number (9 numeric digits, cannot be more or less), an address (street, city, state, and 5 digit Zip code), and a phone number (3 digit area code and 7 digit number). The application will support an array of students. The user will be allowed to enter records from the keyboard, sort records by either name (last, first, middle) or by ID, save the records to a disk file (name supplied by user), and read the records from a disk file (name again supplied by user).
Create a fixed length string that must check that the length of the string is the required length. The fixed length class should be done as a template with the number of characters as the template argument. From this fixed length string, derive a class to hold digits of a fixed length.
Create component classes as necessary to use together to implement the student record class.
Use either the array template created in an earlier lab to handle the array or you may use the vector class from the STL to handle the array of student record objects.
The maximum number of students will be 25 (it may be less).
Explanation / Answer
import java.util.*; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.text.SimpleDateFormat; public class StudentList { public static ArrayList studentList = new ArrayList(); /** * loadStudenList() Method will load all students from the defined constant * STUDENT_FILE_PATH with the location of the Student.txt file. * * @author William Crews * @param None With no params it will load Student.txt in local directory. * @throws IOException Will throw IOException if file cannot be found or read. * @returns void */ public static void loadStudentList() { String lineRead = null; // String to hold each line read from the file. try { BufferedReader studentFile = null; studentFile = new BufferedReader(new FileReader(Constants.STUDENT_FILE_PATH)); while ((lineRead = studentFile.readLine()) != null) { System.out.println(lineRead); String [] textArray = lineRead.split(","); Student obj = new Student(Integer.parseInt(textArray[0]), textArray[1].trim(),textArray[2].trim(), textArray[3].trim(),textArray[4].trim(), textArray[5].trim(),textArray[6].trim(), textArray[7].trim()); studentList.add(obj); } studentFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * loadStudenList(String fName) Method will load all students from the file name passed. * An overriding method to loadStudentList() with no parms. * @author William Crews * @param fName String type, allowing to specify the path and file to use. * @throws IOException Will throw IOException if file cannot be found or read. * @returns void */ public static void loadStudentList(String fName) { String lineRead = null; // String to hold each line read from the file. try { BufferedReader studentFile = null; studentFile = new BufferedReader(new FileReader(fName)); while ((lineRead = studentFile.readLine()) != null) { System.out.println(lineRead); String [] textArray = lineRead.split(","); Student studentObj = new Student(Integer.parseInt(textArray[0]), textArray[1].trim(),textArray[2].trim(), textArray[3].trim(),textArray[4].trim(), textArray[5].trim(),textArray[6].trim(), textArray[7].trim()); studentList.add(studentObj); } studentFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * saveStudentList() Method will save all students info to file Students.txt. * Note: If data already exists this will overwrite after * making a backup copy with appended date-time. * * @author William Crews * @param None With no parameters it will save to Student.txt file in * local directory. * @throws IOException * @returns void */ public static void saveStudentList() { File testFile, reNamedFile; File studentsFile; PrintWriter outFile; String fileName = Constants.STUDENT_FILE_PATH; try { testFile = new File(Constants.STUDENT_FILE_PATH); // We need to test if file already exists, we want to save a backup // so we rename it with a the date & time appended. if (testFile.exists()) { reNamedFile = new File(AppendDate(fileName)); testFile.renameTo(reNamedFile); // Rename to new file name with // date_time appended. } // Create a new Student.txt file (empty at this point) studentsFile = new File(fileName); outFile = new PrintWriter(studentsFile); if(studentList.size() != 0) { // Sort by LastName, FirstName Collections.sort(studentList); // Construct output record and write to file for(Student a: studentList){ String studentRecord = Integer.toString(a.getStudentID()) + "," + a.getFirstName().trim() + "," + a.getLastName().trim() + "," + a.getAddress().trim() + "," + a.getCity().trim() + "," + a.getState().trim() + "," + a.getZip().trim() + "," + a.getPassword().trim(); outFile.println(studentRecord); } outFile.close(); } } catch (IOException e) { e.printStackTrace(); } } public static String AppendDate(String fName) { SimpleDateFormat simpleFormatter = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss"); Date now = new Date(); fName = fName + "_" + simpleFormatter.format(now); System.out.println(fName); return fName; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.