java 自寫Student管理系統並,將數據 存儲本地、讀取於本地文件

studentManager.java

package StudentsManager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class StudentsManager {
	
	public static void main(String[] args) throws IOException {
		ArrayList<Student> studentArray = new ArrayList<Student>();
		
		Date time = new Date();
		System.out.println("time : " + time.toString());

		studentArray = getStudetsInfoFromfile();
		for (int i=0; i<studentArray.size(); i++) {
			System.out.println(studentArray.get(i).getName());
		}
		
		while(true) {
			System.out.println("歡迎來到學生系統");
			System.out.println("1 產看所有學生");
			System.out.println("2 添加學生");
			System.out.println("3 刪除學生");
			System.out.println("4 修改學生");
			System.out.println("其他輸入即退出");
			System.out.println("請輸入您的選擇:");
			Scanner sc = new Scanner(System.in);
			String choice = sc.nextLine();
			switch(choice) {
				case "1":
					findAllStudents(studentArray);
					break;
				case "2":
					addStudent(studentArray);
					break;
				case "3":
					deleteStudent(studentArray);
					break;
				case "4":
					updateStudent(studentArray);
					break;
				default:
					System.out.println("謝謝你的使用!choice = " + choice);
					System.out.println("謝謝你的使用!");
					System.exit(0);
					break;
			}
		}
	}
	
	public static void findAllStudents(ArrayList<Student> arr) {
		if (arr.size() == 0) {
			System.out.println("不好意思,暫時沒有數據,請先添加數據後再查詢");
			return;
		}
		System.out.println("學號\t姓名\t年齡\t地址");
		for (int i=0; i<arr.size(); i++) {
			Student s = arr.get(i);
			System.out.println("" + s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
		}
	}
	
	public static void addStudent(ArrayList<Student> arr) throws IOException {
		boolean falg = false;
		String id = "0";
		while(true) {
			falg = false;
			System.out.println("請輸入學生的id");
			Scanner sc = new Scanner(System.in);
			id = sc.nextLine();
			for (int i=0; i<arr.size(); i++) {
				Student s = arr.get(i);
				if (s.getId().equals(id)) {
					System.out.println();
					falg = true;
				}
			}
			if (falg) {
				System.out.println("您輸入的ID已存在請重新輸入");
			} else {
				break;
			}
		}
		System.out.println("請輸入學生的姓名");
		Scanner sc1 = new Scanner(System.in);
		String name = sc1.nextLine();
		System.out.println("請輸入學生的年齡");
		Scanner sc2 = new Scanner(System.in);
		String age = sc2.nextLine();
		System.out.println("請輸入學生的地址");
		Scanner sc3 = new Scanner(System.in);
		String address = sc3.nextLine();
//		Student s = new Student(id, name, age, address);
		Student s = new Student();
		s.setId(id);
		s.setName(name);
		s.setAge(age);
		s.setAddress(address);
		
		arr.add(s);
		saveStudentsInfoTofile(arr);
		System.out.println("添加學生成功");
	}
	
	public static void deleteStudent(ArrayList<Student> arr) throws IOException {
		System.out.println("請輸入您要刪除的學生的學號");
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		boolean hasDelete = false;
		for (int i=0; i<arr.size(); i++) {
			Student s = arr.get(i);
			if (s.getId().equals(id)) {
				arr.remove(i);
				hasDelete = true;
				break;
			}
		}
		saveStudentsInfoTofile(arr);
		if (hasDelete)
			System.out.println("刪除學生成功");
		else 
			System.out.println("您輸入的學生id不存在");
	}
	
	public static void updateStudent(ArrayList<Student> arr) throws IOException {
		System.out.println("請輸入要修改的學生的id");
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		int index = -1;
		for (int i=0; i<arr.size(); i++) {
			Student s = arr.get(i);
			if (s.getId().equals(id)) {
				index = i;
			}
		}
		if (index == -1) {
			System.out.println("您要修改的學生id不存在");
		}
		else {
			Student s = arr.get(index);
			System.out.println("請輸入學生的姓名");
			Scanner sc1 = new Scanner(System.in);
			String name = sc1.nextLine();
			System.out.println("請輸入學生的年齡");
			Scanner sc2 = new Scanner(System.in);
			String age = sc2.nextLine();
			System.out.println("請輸入學生的地址");
			Scanner sc3 = new Scanner(System.in);
			String address = sc3.nextLine();
			s.setName(name);
			s.setAddress(address);
			s.setAge(age);
			System.out.println("修改修改成功");
		}
		saveStudentsInfoTofile(arr);
	}
	
	public static void saveStudentsInfoTofile(ArrayList<Student> arr) throws IOException {
		BufferedWriter bw = null;
		String destFile = "./students.text";
		File file = new File(destFile);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			StringBuilder data = new StringBuilder();
			for (int i = 0; i<arr.size(); i++) {
				data.append(arr.get(i).getId());
				data.append("," + arr.get(i).getName());
				data.append("," + arr.get(i).getAge());
				data.append("," + arr.get(i).getAddress());
				data.append("\n");
			}
			System.out.println(data);
			bw.write(data.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("文件寫入成功");
	}

	public static ArrayList<Student> getStudetsInfoFromfile() throws FileNotFoundException {
		String path = "./students.text";
		BufferedReader br = null;
		ArrayList<Student> arr = new ArrayList<Student>();
		File file = new File(path);
		String studentsInfo = "";
		try {
			if (file.exists()) {
				FileInputStream fileInputStream = new FileInputStream(path);
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
				br = new BufferedReader(inputStreamReader);
				String infoData = null;
				while ((infoData = br.readLine()) != null) {
					studentsInfo += infoData;
					studentsInfo += "\n";
				}
//				System.out.println("----------" + studentsInfo);
				br.close();
				
				String[] infoArr = studentsInfo.split("\n");
				for (int i=0; i<infoArr.length; i++) {
					String[] stuArr = infoArr[i].split(",");
					if (stuArr.length > 0) {
						Student s = new Student();
						s.setId(stuArr[0]);
						s.setName(stuArr[1]);
						s.setAge(stuArr[2]);
						s.setAddress(stuArr[3]);
						arr.add(s);
					}		
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return arr;
	}
}

student.java

package StudentsManager;

public class Student {
	private String id;
	private String name;
	private String age;
	private String address;
	public Student() {
		
	}
	public Student(String id, String name, String age, String address) {

		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章