1. 程式人生 > >java學生資訊管理Arraylist的運用

java學生資訊管理Arraylist的運用

  1 import java.io.File;
  2 import java.io.FileNotFoundException;
  3 import java.io.PrintStream;
  4 import java.util.ArrayList;
  5 import java.util.Iterator;
  6 import java.util.Scanner;
  7  
  8 public class Windzly {
  9     
 10     static ArrayList<Student> list = new ArrayList<Student>();
11 12 public static void main(String[] arg) throws FileNotFoundException { 13 Scanner sc = new Scanner(System.in); 14 15 readfile();//讀取文字檔案 16 17 18 int n; 19 while(true) 20 { 21 System.out.println("1、新增學生 2、刪除指定學號的學生 3、修改指定學號的成績 " 22
+ " 4、查詢指定學號的資訊 5、列印所有的學生資訊 6、退出"); 23 n = sc.nextInt(); 24 if(n == 1) 25 { 26 addstudent();//新增學生; 27 } 28 else if(n == 2) 29 { 30 erasestudent();//刪除指定學號的學生 31 32
} 33 else if(n == 3) 34 { 35 editstudent();//修改指定學號的成績 36 } 37 else if(n == 4) 38 { 39 findstudent();//查詢學生資訊 40 } 41 else if(n == 5) 42 { 43 printstudent();//列印所有學生的資訊 44 } 45 else if(n == 6) 46 { 47 savestudent(); 48 System.exit(0); 49 break; 50 } 51 } 52 } 53 54 public static void savestudent() throws FileNotFoundException { 55 PrintStream put = System.out; 56 System.setOut(new PrintStream(new File("y.txt"))); 57 Iterator it = list.iterator(); 58 while(it.hasNext()) 59 { 60 Object object = it.next(); 61 System.out.println(object); 62 } 63 System.setOut(put); 64 } 65 66 public static void readfile() throws FileNotFoundException { 67 Scanner sc = new Scanner(new File("s.txt")); 68 while(sc.hasNext()) 69 { 70 String s = sc.nextLine(); 71 String []ss = s.split(" "); 72 Student student = new Student(ss[0],ss[1],ss[2]); 73 list.add(student); 74 } 75 } 76 77 public static void printstudent() { 78 for(int i = 0; i < list.size(); i ++) 79 { 80 System.out.println(list.get(i).toString()); 81 } 82 } 83 84 public static void findstudent() { 85 String s; 86 Scanner sc = new Scanner(System.in); 87 System.out.println("請輸入學號"); 88 s = sc.nextLine(); 89 for(int i = 0; i < list.size(); i ++) 90 { 91 if(list.get(i).getNo().equals(s)) 92 { 93 System.out.println(list.get(i).toString()); 94 } 95 } 96 } 97 98 public static void editstudent() { 99 String s,sco; 100 Scanner sc = new Scanner(System.in);; 101 System.out.println("請輸入學號"); 102 s = sc.nextLine(); 103 for(int i = 0; i < list.size(); i ++) 104 { 105 if(list.get(i).getNo().equals(s)) 106 { 107 System.out.println("請輸入修改後的成績"); 108 sco = sc.nextLine(); 109 list.get(i).setScore(sco); 110 } 111 } 112 } 113 114 public static void erasestudent() { 115 Scanner sc = new Scanner(System.in); 116 String s; 117 System.out.println("請輸入指定的學號"); 118 s = sc.nextLine(); 119 for(int i = 0; i < list.size(); i ++) 120 { 121 if(list.get(i).getNo().equals(s)) 122 { 123 list.remove(i); 124 } 125 } 126 } 127 128 public static void addstudent() { 129 String s; 130 Scanner sc = new Scanner(System.in); 131 System.out.println("輸出新同學的學號,姓名,成績"); 132 s = sc.nextLine(); 133 String [] ss = s.split(" "); 134 Student student = new Student(ss[0],ss[1],ss[2]); 135 list.add(student); 136 } 137 } 138 139 class Student 140 { 141 String no; 142 String name; 143 String score; 144 public Student() { 145 super(); 146 } 147 public Student(String no, String name, String score) { 148 super(); 149 this.no = no; 150 this.name = name; 151 this.score = score; 152 } 153 @Override 154 public String toString() { 155 return "Student [學號:" + no + ", 姓名:" + name + ", 成績:" + score 156 + "]"; 157 } 158 @Override 159 public int hashCode() { 160 final int prime = 31; 161 int result = 1; 162 result = prime * result + ((name == null) ? 0 : name.hashCode()); 163 result = prime * result + ((no == null) ? 0 : no.hashCode()); 164 result = prime * result + ((score == null) ? 0 : score.hashCode()); 165 return result; 166 } 167 public String getNo() { 168 return no; 169 } 170 public void setNo(String no) { 171 this.no = no; 172 } 173 public String getName() { 174 return name; 175 } 176 public void setName(String name) { 177 this.name = name; 178 } 179 public String getScore() { 180 return score; 181 } 182 public void setScore(String score) { 183 this.score = score; 184 } 185 186 }
View Code