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(); // 学生编号
	public static List name = new ArrayList(); // 学生姓名
	public static List age = new ArrayList(); // 学生姓名

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

	static {

		List temp = new ArrayList();
		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]));
		}

	}

}
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;
	}
}
public class testStudent {
	public static void main(String[] args) {
		
		StudentOperator.menu();
		
	}
}


 
  

你可能感兴趣的:(JAVA)