import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java .util.iterator; import java.util.StringTokenizer;
/ * * CREATED ON 2005-1-11 * /
/ ** * @Author rangwei * @since 1.0 * * Todo student score management system * Search, modify, delete data * * / public class littleprogram {static boolean isdelete = true; static boolean isfind = true; public static void Main (String [] args) // main method, the program starts to run throws ioException, number = -1; do {LittleProgram LP = new littleProgram (); system.out.println (); system.out. Println ("/ t #################################); System.out.Println ); System.out.Println ("/ T / T Java Student Achievement Management System 1.1"); System.out.Println ("/ T / T Review, Modify, Delete Data"); System.out. Println (); system.out.println ("/ t ###################################### N "); system.out.print (" 1. Add data: / n " " 2. Find data: / n " " 3. Delete data: / n " " 4. Clear all data: / n " "5. Print all the data to the screen / N" "6. Sort the score to the school number number / N" "7. Modify the data / N" "8. Statistics have recorded the recorded score student number / n" "9. With regard to the author / n" "0. Exit the program ./N" "Enter:"); bufferedreader in = // From the final new buffrereader (// Terminal New InputStreamReader (system.in)); //// Number string infutline = in.readline (); // selection choice = integer.valueof (inputline) .intValue (); // item; switch (choice) {case 1: {// 1. Add data string str = lp.inputdata ();
lp.adddata (STR); System.out.Println ("Added Data Success."); Timeout (1);} Break; Case 2: {// 2. Find data long find = 0; system.out.print "Please enter the student number you want to find:"); bufferedreader INN = New BufferedReader (NEW INPUTSTREADER (SYSTEMREADER (STRING INPUTLI = Inn.Readline (); Find = Integer.Valueof (Inputli) .longValue () Lp.FindData (Find); Timeout (2);} Break; Case 3: {// 3. Delete Data Long DeleTenumber = 0; System.out.Print ("Please enter the student number of classmates you want to delete:" ); BufferedReader bf = new BufferedReader (new InputStreamReader (System.in)); String inputL = bf.readLine (); deleteNumber = Integer.valueOf (inputL) .longValue (); lp.deleteData (deleteNumber); if (isDelete)System.out.println ("Delete Data Success!"); Timeout (1);} Break; Case 4: {lp.cleardata (); // 4. Clear all data Timeout (1);} Break; Case 5: {Print (); // 5. Print the data to the screen Timeout (2);} Break; Case 6: {lp.numsort (); // 6. Sort the score to SYSTEM.OUT.PRINTLN (" According to the student number from small to large sort! / N " " Sort: / n "); print (); timeout (2);} Break; Case 7: {lp.rewrite (); // 7. Modify data TIMEOUT (2);
Case 8: {int count = lp.count (); system.out.println ("A total of" count "a student has been logged."); timeout (2);} Break; case 9: {system.out.print ("/ T / TRANGWEI / N" "/ T / TSHIEP / N" "/ t / tqq: ********* / n"); timeout (4);} Break;}} while (Choice! = 0); System.out.Println ("BYE! ^ - ^"); system.exit (0);} public string inputData () // Receive data from the terminal, return string throws oException, Numberformatexception {system.out.print ("Please enter: Learn Name Gender Score / N" "Each data is separated by space:"); string all = ""; try {bufferedreader in = // New BufferedReader (// Terminate New InputStreamReader (System.IN)); // Number String InputLine = in.readline (); // According to StringTokenizer Str = NEW STRINGTOKENIZER (InputLine, ""); // The received data is spaced apart from the space, this class is used to extract each string long Num = integer.Valueof () "longValue (); // student number String name = (string) str.nextToken (); // Name String sex = (string) str.nextToken (); // Gender Double Mark = Integer.Valueof (Str.NextToken ()). DoubleValue (); // Score All = String.Valueof (NUM) "," Name "," SEX ","
String.Valueof (Mark); // Use ",", "," and then put it in the string all} catches (ooException e) {} catch (numberformatexception e) {} return all; // Return String all} public void addddata (string str) // How to increase data THROWS IEXCEPTION {String S1 = "", s2 = ", s3 ="; File File = New File ("DATA.TXT"); if (file.exists ()) // If the file data.txt exists {Try {BufferedReader IN = New BufferedReader (New FileReader ("Data.txt")); while ((S1 = in.readline ())! = null) S2 = S1 "/ N"; // put all the rows of data in the file into a string S2 S2 = STR "/ N"; //, connect S2 in the shape of the parameter STR into S2 BUFFEREDReader IN2 = // Put the character new bufferedreader (// string S2 also new stringreader (s2)); // is the original printwriter out = // file new printwriter (//-Conversion STR (newly input line data) new bufferedwriter (// Reconfaction into Data.txt New FileWriter ("DATA.TXT"))); // Overwrite the original data while ((S3 = in2.readline) ())! = null) {OUT.PRINTLN (S3);} out.close (); //system.out.println ("IOException E) {}}} else {system .err.println ("File /" Data / "Missing!");
}}} Public void cleardata () // Method for clearing all data for Data.txt THROWS IOEXCEPTION {file file = new file ("data.txt"); if (file.exists ()) // If the file is {Try { PrintWriter Out = New PrintWriter (New FileWriter (New FileWriter (")); Out.print (" "); // Write an empty character in the file data.txt, so clear the original content out.close () ; // Close the file system.out.println ("Clear Data True!");} Catch (ioException e) {}} else {system.err.println ("file /" data / "missing!");}} Public void deletedata // Delete a data for IOException, filenotfoundexception {isdelete = true; try {datamap mp = new dataMap (); // Generate a container LONG J = 0; String S1 = "" , S2 = "", s3 = ""; bufferedreader in = new bufferedreader (New FileReader); While ((S1 = IN.Readline ())! = null) {j = numberTokenizer (S1); mp.put (j, s1);} try {if (mp.containskey (String.Valueof (deleTenumber) .tostring ))) {Mp.remove;} else throw new number (); collection c = mp.values (); iterator iter = c.iterator (); while (ore (ore) {s1 = (String ); s3 = s1 "/ n";
} BufferedReader in2 = new BufferedReader (new StringReader (s3)); PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter ( "data.txt"))); //System.out.println("delete No " deleteNumber) WHILE ((S1 = In2.Readline ())! = Null) {Out.Println (S1);} out.close ();} catch (numbernotfoundexception e) {isdelete = false; system.out.println (deletenumber " no found :( ");}} catch (IOException e) {}} public long numberTokenizer (String s) throws IOException {StringTokenizer st = new StringTokenizer (s," "); return Integer.valueOf ((st.nextToken () )))). LongValue (); public void findData (long find) // Find data throws ioException, NumberNotFoundException {isfind = true; String s = "" "" ""; long i; datamap dm = new dataMap (); bufferedreader in = new bufferedreader ("Data.txt"); while ((s = in.readline ()) ! = NULL) {i = Numbertokenizer (s); DM.PUT (I, s);} //in.close (); try {i (DM.ContainsKey (String.Valueof (Find) .tostring ())) {FINDSTRING = DM.Get (Find); System.out.Println ("Student" Find "student information is:"); system.out.println (FindString);
} else throw new number ();} catch (NumberNotFoundException e) {system.out.println (Find "no found: ("); isfind = false;}} public static void print () // read text file Put data Method for printing to the terminal throws ioException {try {buffredreader in = new bufferedreader ("DATA.TXT"); string read = ""; while ((Read = in.readline ())! = Null) System. Out.println (read);} catch (ooException e) {}} public static void timeout (double sec) // Pleandering short time can not this function {double seconds = sec; long t = system.currenttimemillis ) (int) (SECONDS * 1000); while ((System.currentTimeMillis ()) NEW PrintWriter (New FileWriter ("Data.txt"))); item it = Dal.iterator (); long temp = 0; string tempstr = ""; while (it.hasnext ()) {TEMP = INTEGER .Valueof () IT.Next ()). LongValue (); tempstr = DM.Get (TEMP); out.println (Tempstr);} out.close ();} catch (ooException e) {}} PUBLIC void rewrite () throws IOException, NumberNotFoundException {try {System.out.print ( "Please enter the student number you want to modify:"); BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String inputLine = in .readline (); long Num = INTEGER.VALUEOF (InputLine) .longvalue (); Finddata (NUM); if (isfind) {Deletedata (NUM); System.out.Print ("Please re-enter the student's information:" Stri Ng str = inputdata (); adddata (str); System.out.Println ("Rewrite True!");}}}} catch (numberNotfoundExcection e) {}} public int count () throws ooException { DataArrayList Dal = New DataARrayList (); try {string s = ""; long i = 0; bufferedreader in = New BufferedReader ("Data.txt"); while ((s = in.readline ())! = NULL) {i = Numbertokenizer (s); Dal.Add (i);