1. 程式人生 > >java:學生管理系統

java:學生管理系統

要求:1、新增學生    2、刪除指定學號的學生    3、修改指定學號的成績   

           4、查詢指定學號的資訊        5、列印所有學生的資訊       6、退出

步驟:1、定義一個學生類,類中包含學號、姓名、成績

package studentManage;

public class Student {
	private String no;
	private String name;
	private String score;

	public Student() {
		super();
	}

	public Student(String no, String name, String score) {
		super();
		this.no = no;
		this.name = name;
		this.score = score;
	}

	@Override
	public String toString() {
		return "學號:" + no + ", 姓名:" + name + ", 成績:" + score;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}
}

           2、生成一個檔案,用來存放學生資訊(假入現有三個同學,方便後續操作)

學號:001, 姓名:張, 成績:90
學號:002, 姓名:李, 成績:89
學號:003, 姓名:王, 成績:88

           3、設計主程式

package studentManage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class StudentManage {
	//定義列表,列表元素為Student類成員
	static ArrayList<Student> list = new ArrayList<Student>();

	public static void main(String[] arg) throws FileNotFoundException {
		readFile();//程式開始時讀取系統檔案
		//選擇功能的實現
		Scanner scanner = new Scanner(System.in);
		System.out.println("歡迎使用學生管理系統!");
		do {
			System.out
					.println("1、新增學生    2、刪除指定學號的學生    3、修改指定學號的成績    4、查詢指定學號的資訊    5、列印所有的學生資訊    6、退出");
			System.out.println("請選擇您所需的功能:");
			int i = scanner.nextInt();

			switch (i) {
			case 1:
				addStudent();
				break;
			case 2:
				deleteStudent();
				break;
			case 3:
				editStudent();
				break;
			case 4:
				seekStudent();
				break;
			case 5:
				printStudent();
				break;
			case 6:
				saveStudent();
				System.exit(0);
				break;
			default:
				System.out.println("輸入錯誤,請重新輸入!");
				break;
			}
		} while (true);
	}
	//讀取檔案
	private static void readFile() throws FileNotFoundException {
		Scanner scanner =new Scanner(new File("student.txt"));
		while(scanner.hasNextLine()){
			String s=scanner.nextLine();
			String[] ss=s.split("[, ]");//檔案中的每一行內容由空格分割為字串陣列	
			Student student=new Student(ss[0], ss[1],ss[2] );//將字串陣列賦值給student
			list.add(student);//把student新增到列表中
		}
		
	}
	//存取學生資訊到檔案
	private static void saveStudent() throws FileNotFoundException {
		PrintStream psOld =System.out;//存取輸出路徑
		System.setOut(new PrintStream(new File("student.txt")));//設定新的輸出路徑到檔案
		Iterator it=list.iterator();//迭代列表
		while(it.hasNext()){
			Object object=it.next();
			System.out.println(object);
		}	
		System.setOut(psOld);//恢復原來的輸出路徑
		
	}
	//查詢指定學號的學生
	private static void seekStudent() {
		String n = null;
		System.out.println("請輸入指定的學號:");
		n = new Scanner(System.in).nextLine();

		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			Student student = it.next();
			if (n.equals(student.getNo())) {
				System.out.println(student.toString());
			}
		}

	}
	//輸出所有的學生資訊(使用foreach迴圈)
	private static void printStudent() {
		for (Student student : list) {
			System.out.println(student.toString());
		}

	}
	//修改指定學號學生的資訊
	private static void editStudent() {
		String n = null;
		String s = null;
		System.out.println("請輸入指定的學號:");
		n = new Scanner(System.in).nextLine();

		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			Student student = it.next();
			if (n.equals(student.getNo())) {
				System.out.println("請輸入修改後的成績:");
				s = new Scanner(System.in).nextLine();
				student.setScore(s);
			}
		}
	}
	//刪除指定學號的學生
	private static void deleteStudent() {
		String n = null;
		System.out.println("請輸入指定的學號:");
		n = new Scanner(System.in).nextLine();

		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getNo().equals(n)) {
				list.remove(i);
				break;
			}
		}

	}
	//新增學生
	public static void addStudent() {
		System.out.println("請依次輸入新同學的學號、姓名、成績:");
		String s = new Scanner(System.in).nextLine();
		String[] ss = s.split("[, ]");
		Student student = new Student(ss[0], ss[1], ss[2]);
		list.add(student);
	}
}