⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.java

📁 javaSE以控制台操作
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Driver {
	/**
	 * 构造方法,输出开头提示信息
	 */
	public Driver() {
		System.out.println("___________________________\n");
		System.out.println("| ********Main Menu********\n");
		System.out
				.println("| 1.Read a student data file\n| 2.Show all students\n| 3.Set unit name\n"
						+ "| 4.Arrange an instructer to unit\n| 5.Enrol a student to unit\n"
						+ "| 6.Delete a student from unit\n| 7.Print unit report on screen\n"
						+ "| 8.Exprot unit report\n| 9.Exit\n");
		System.out.println("___________________________\n");
		System.out.println("Please enter your selection:");

	}

	/**
	 * 执行方法,可按照用户输入的选项执行响应的功能
	 */

	public void go() {
		while (true) {
			i = Integer.parseInt(scanner.nextLine());
			Student s;
			// unit = new Unit("java", studentDB);
			switch (i) {
			case 1:
				try {
					readData();
					System.out.println("\t\t***success to read!***\n");
				} catch (Exception e) {
					System.out.println("\t\t***fall to read!***\n");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 2:
				try {
					showDBData();
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 3:
				try {
					System.out.println("please input the Unit's Name:\n");
					unitName();
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 4:
				try {
					setStaff();
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 5:
				try {
					enrolStudent();
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 6:
				try {
					eliminateStudent();
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 7:
				try {
					System.out.println(unit.toString());
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 8:
				try {
					exportReport();
				} catch (Exception e) {
					System.out.println("\t\t***wrong input!\n***");
				}
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			case 9:
				System.exit(0);
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			default:
				System.out.println("\t\t***wrong common!***\n");
				System.out.println("\n");
				System.out.println("Please enter your selection:");
				break;
			}
		}
	}

	/**
	 * 从student.dat读取信息
	 */

	public void readData() {
		StringTokenizer t;
		try {
			BufferedReader in = new BufferedReader(
					new FileReader("student.dat"));
			while ((read = in.readLine()) != null) {
				t = new StringTokenizer(read, ",");
				String[] s = new String[t.countTokens()];
				int i = 0;
				while (t.hasMoreTokens()) {
					s[i++] = t.nextToken();
				}
				int age = Integer.parseInt(s[3]);
				int score = Integer.parseInt(s[4]);
				Student stu = new Student(s[0], s[1], s[2], age, score);
				int j;
				for (j = 0; j < studentDB.size(); j++) {
					if (s[0].compareTo(studentDB.get(j).getID()) == 0) {
						break;
					}
					if (s[0].compareTo(studentDB.get(j).getID()) < 0) {
						studentDB.add(j, stu);
						break;
					}
				}
				if (j == studentDB.size()) {
					studentDB.add(j, stu);
				}
			}

		} catch (FileNotFoundException e) {
			System.out.println(e.toString() + "\n");

		} catch (IOException e2) {
			System.out.println(e2.toString() + "\n");

		}

	}

	/**
	 * 显示从student.dat中读取的学生信息
	 */

	public void showDBData() {
		System.out.println("ID\tName\tGender\tAge\tScore\tGrade\n");
		for (int i = 0; i < studentDB.size(); i++) {
			System.out.println(studentDB.get(i).toString() + "\n");
		}
	}

	/**
	 * 设置unit的名称
	 */

	public void unitName() {
		unit.setUnitName(scanner.nextLine());
	}

	/**
	 * 设置unit的instructor的信息
	 */

	public void setStaff() {

		System.out.println("please input the Name:\n");
		String name = scanner.nextLine();
		System.out.println("please input the Gender:\n");
		String gender = scanner.nextLine();
		System.out.println("please input the Department:\n");
		String department = scanner.nextLine();

		unit.setlnstructor(name, gender, department);

	}

	/**
	 * 向unit中添加一个学生的信息
	 */

	public void enrolStudent() {
		System.out.println("please input the ID:\n");
		String ID = scanner.nextLine();
		int a = -1;
		for (int i = 0; i < studentDB.size(); i++) {
			if (ID.compareToIgnoreCase(studentDB.get(i).getID()) == 0) {
				a = i;
			}
		}
		if (a != -1) {
			unit.addStudent(studentDB.get(a));
			System.out.println("\t\t***success to add!***\n");
		} else {
			System.out.println("\t\t***fail to add!***\n");
		}
	}

	/**
	 * 从unit中删除一个学生的信息
	 */

	public void eliminateStudent() {
		System.out.println("please input the ID:\n");
		String ID = scanner.nextLine();
		int a = -1;
		for (int i = 0; i < studentDB.size(); i++) {
			if (ID.compareToIgnoreCase(studentDB.get(i).getID()) == 0) {
				a = i;
			}
		}
		if (a != -1) {
			unit.deleteStudent(studentDB.get(a).getID());
			System.out.println("\t\t***success to delete!***\n");
		} else {
			System.out.println("\t\t***fail to delete!***\n");
		}

	}

	/**
	 * 发布unit信息生成abc.txt
	 */

	public void exportReport() {
		try {
			PrintWriter print = new PrintWriter(new FileWriter("abc.txt"));
			print.println(unit.toString());
			print.close();
			System.out.println("\t\t***success to export***\n");
		} catch (IOException e) {
			System.out.println("\t\t***fail to export***\n");
			System.out.println(e.toString() + "\n");
		}
		System.out.println("\n");

	}

	private Unit unit = new Unit();;
	private ArrayList<Student> studentDB = new ArrayList<Student>();
	private int i;
	private Scanner scanner = new Scanner(System.in);
	private String read = "";

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -