1. 程式人生 > >java-IO 檔案作為資料庫的學生管理系統

java-IO 檔案作為資料庫的學生管理系統

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.html.HTMLDocument.Iterator;

public class Student {

	public static List num = new ArrayList<String>(); // 學生編號
	public static List name = new ArrayList<String>(); // 學生姓名
	public static List age = new ArrayList<Integer>(); // 學生姓名

	public static File file = new File(
			"..//exercise//src//com//chinasoft//exercise12//Student.txt");

	static {

		List temp = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));

			String read = br.readLine();
			while (read != null) {
				temp.add(read);
				read = br.readLine();
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < temp.size(); i++) {
			String[] sTemp = ((String) temp.get(i)).split(",");
			num.add(sTemp[0]);
			name.add(sTemp[1]);
			age.add(Integer.parseInt(sTemp[2]));
		}

	}

}
</pre><pre name="code" class="java"><pre name="code" class="java">
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * 操作學生資料
 * 
 * @author Administrator
 *
 */
public class StudentOperator {

	static String action = "";

	static Scanner sc = new Scanner(System.in);

	/**
	 * 主選單
	 */
	public static void menu() {
		System.out.println("請選擇相應的操作:");
		System.out.println("--------------------");
		System.out.println("【1】新增學生資訊");
		System.out.println("【2】查詢學生資訊");
		System.out.println("【3】刪除學生資訊");
		System.out.println("【4】退出");
		String select = sc.next();
		if ("1234".indexOf(select) > -1) {
			switch (select) {
			case "1":
				add();
				break;
			case "2":
				search();
				break;
			case "3":
				delete();
				break;
			case "4":
				if (save()) {
					System.out.println("\n謝謝使用!");
				} else {
					System.out.println("儲存失敗!");
				}
				break;
			}
		}
	}

	/**
	 * 判斷是否繼續
	 */
	public static void returnMenu() {
		System.out.println("是否繼續(輸入y繼續操作)?");
		String input = sc.next();
		if (input.equals("y") || input.equals("Y")) {
			switch (action) {
			case "add":
				add();
				break;
			case "search":
				search();
				break;
			case "delete":
				delete();
				break;
			}
		} else {
			menu();
		}
	}

	/**
	 * 查詢所有學生資訊 如果傳入-1,則列印所有學生 如果傳入下標,則列印指定下標的學生
	 */
	public static void show(int index) {
		if (index == -1) {
			for (int i = 0; i < Student.num.size(); i++) {
				System.out.println("*******************");
				System.out.println("編號:" + Student.num.get(i));
				System.out.println("姓名:" + Student.name.get(i));
				System.out.println("年齡:" + Student.age.get(i));
				System.out.println("*******************");
			}
		} else {
			System.out.println("*******************");
			System.out.println("編號:" + Student.num.get(index));
			System.out.println("姓名:" + Student.name.get(index));
			System.out.println("年齡:" + Student.age.get(index));
			System.out.println("*******************");
		}

	}

	/**
	 * 新增學生資訊
	 */
	public static void add() {
		action = "add";
		System.out.println("新增學生資訊: ");
		System.out.println("--------------------");
		// 輸入學號
		String inputNum = null;
		while (true) {
			System.out.println("請輸入學生編號:");
			inputNum = sc.next();
			if (isExist(inputNum)) {
				System.out.println("學號已存在,請重新輸入!");
				continue;
			} else {
				break;
			}
		}
		// 輸入年齡,如果輸入的格式不對,則重新輸入!
		int inputAge = 0;
		while (true) {
			System.out.println("請輸入學生年齡:");
			String age = sc.next();
			if (isNumber(age)) {
				inputAge = Integer.parseInt(age);
				break;
			} else {
				System.out.println("輸入的格式不對,請重新輸入!");
			}
		}
		// 輸入學生姓名
		System.out.println("請輸入學生姓名:");
		String inputName = sc.next();
		// 插入到資料庫中
		Student.num.add(inputNum);
		Student.name.add(inputName);
		Student.age.add(inputAge);
		// 列印所有學生
		show(-1);

		// 是否繼續
		returnMenu();
	}

	/**
	 * 查詢學院資訊
	 */
	public static void search() {
		action = "search";
		System.out.println("輸入學生編號,查詢相關資訊:");
		String num = sc.next();
		int i = 0;
		for (; i < Student.num.size(); i++) {
			if (Student.num.get(i).equals(num)) {
				show(i);
				break;
			}
		}

		if (i >= Student.num.size()) {
			System.out.println("找不到!");
		}

		// 是否繼續
		returnMenu();
	}

	/**
	 * 刪除學生資訊
	 */
	private static void delete() {
		System.out.println("請輸入要刪除的學生編號:");
		String num = sc.next();
		for (int i = 0; i < Student.num.size(); i++) {
			if (Student.num.get(i).equals(num)) {
				Student.num.remove(i);
				Student.name.remove(i);
				Student.age.remove(i);
				break;
			}
		}

		// 是否繼續
		returnMenu();
	}

	/**
	 * 儲存資訊
	 */
	public static boolean save() {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(Student.file));
			for (int i = 0; i < Student.num.size(); i++) {
				String temp = Student.num.get(i) + "," + Student.name.get(i)
						+ "," + Student.age.get(i);
				bw.write(temp);
				bw.newLine();
			}

			bw.flush();
			bw.close();
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 判斷輸入的是否是數字,如果是則返回true,如果不是,則返回false
	 */
	public static boolean isNumber(String inputNum) {
		try {
			int change = Integer.parseInt(inputNum);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 判斷學號是否存在
	 */
	public static boolean isExist(String num) {
		for (int i = 0; i < Student.num.size(); i++) {
			if (Student.num.get(i).equals(num)) {
				return true;
			}
		}
		return false;
	}
}
<pre name="code" class="java">public class testStudent {
	public static void main(String[] args) {
		
		StudentOperator.menu();
		
	}
}