Some time ago to write the educational management system uses a set of back-end, but was not too much to understand the role of the collection, but according to some information to use
What to know again the next through a simple set of local educational administration system in the end is
For convenience, I put all of them written in a class, we create a class of students directly by way of internal class is created
1 // by way of the class is created inside student class 2 public static class Student { . 3 // temporarily create three properties, to be understood that the focus is set List . 4 public int ID; . 5 public String name; . 6 public String pwd; . 7 . 8 / / create a constructor . 9 public Student ( int ID, String name, String pwd) { 10 the this .id = ID; . 11 the this .name = name; 12 is the this .pwd = pwd; 13 is } 14 15 //重写toString方法 16 @Override 17 public String toString() { 18 return "Student{" + 19 "id=" + id + 20 ", name='" + name + '\'' + 21 ", pwd='" + pwd + '\'' + 22 '}'; 23 } 24 }
Once you have created inner class, we begin to create a set of global variables List, to say here, is definitely guide package util package, not awt package, otherwise there will be problems
// Create a List collection of global variables static List <Student> List = new new ArrayList <Student> ();
Angle brackets Student representation is generic, this also can not give, do not give, then the default is the Object type, if given, they are all types Student
ArrayList is linear memory, we do educational management system with this is enough
Because the List collection has a complete CRUD methods, so we can put all the operations are written in the switch inside, of course, the work is not allowed to do so
Adding and queries are best done, add the add method can be used directly
// call the Add method to add data list.add ( new new Student (getInt ( "Please enter the Student ID:"), getString ( "Enter the student's name:"), getString ( "Please enter the password for the students:")));
This line of code can be resolved, the next query, use the foreach loop on the line
// use foreach loop through the collection for (Student info: List) { PL (info.toString ()); }
But then, modify, and delete when using this two methods is to work through the index, so writing this two methods, we need to write a method to get at the underlying
1 // set the ID mark obtained according to 2 public static int getIndexById ( int ID) { . 3 // used to access a variable subscript . 4 int index = 0 ; . 5 // traversing . 6 for (Student info: List) { 7 // determines whether, the found returns . 8 IF (info.id == ID) . 9 return index; 10 // not found on the increment, to find the next . 11 index ++ ; 12 is } 13 is // If all none are found, return -1 14 return -1; 15 }
Then the next, delete, and modify write is not a difficult thing, and call the method on the line, paste all the code
. 1 Package org.lanqiao.jwglx; 2 . 3 Import of java.util.ArrayList; . 4 Import java.util.List; . 5 Import java.util.Scanner; . 6 . 7 public class Demo { . 8 . 9 // by way student class is created inside class 10 public static class Student { . 11 // temporarily create three properties, to be understood that the focus is set List 12 is public int ID; 13 is public String name; 14 public String pwd; 15 16 //Create a constructor . 17 public Student ( int ID, String name, String pwd) { 18 is the this .id = ID; . 19 the this .name = name; 20 is the this .pwd = pwd; 21 is } 22 is 23 is // override toString Method 24 @override 25 public String toString () { 26 is return "Student {" + 27 "ID =" + ID + 28 ", name = '" + name +' \ '' + 29 ", pwd = '" + Pwd + '\' +30 '}' ; 31 } 32 } 33 is 34 is // Create a set of global variables List 35 static List <Student> List = new new the ArrayList <Student> (); 36 // create input objects 37 [ static Scanner INPUT = new new Scanner (the System .in); 38 is 39 public static void main (String [] args) { 40 // infinite loop so that the menu has been output 41 is the while ( to true ) { 42 is // print menu 43 PL ( "------------------------" ); 44 PL ( "1, add student" ); 45 PL ( "2, delete student" ); 46 is PL ( ". 3, modify student" ); 47 PL ( ". 4, query the student" ); 48 PL ( "0, exit the system" ); 49 PL ( "----------- ------------- " ); 50 51 is // function selection 52 is Switch (the getInt (" Please enter the number function <0-4>: " )) { 53 is Case . 1 : 54 // call the Add method to add data 55 list.add ( new new64 break ; 65 Case . 4 : 66 // use foreach loop through a set of 67 for (Student info: List) { 68 PL (info.toString ()); 69 } 70 BREAK ; 71 is Case 0 : 72 System.exit (0 ); 73 is default : 74 PL ( "enter the number of bad!" ); 75 } 76 } 77 } 78 79 // acquired according to the ID set subscript 80 public static int getIndexById ( int ID) { 81 // used to access a variable subscript 82 int index = 0 ; 83 // traversing 84 for (Student info: List) { 85 // determines whether or not found, returns to find 86 IF (info.id == ID) 87 return index; 88 // not found on the increment, to find the next 89 index ++ ; 90 } 91 is // If all none are found, it returns -1 92 return -1 ; 93 } 94 95 // The following are created as a tool for convenient method, 96 public static void P (String MSG) { 97 of System.out.print (MSG); 98 } 99 100 public static void PL (String MSG) { 101 the System.out. the println (MSG); 102 } 103 104 public static int the getInt (String MSG) { 105 P (MSG); 106 return input.nextInt (); 107 } 108 109 public static String the getString (String MSG) { 110 p(msg); 111 return input.next(); 112 } 113 }