Java-學生管理系統(改進版)

學生類

package com.it02;

public class Student {
	private String id;
	private String name;
	private String age;
	private String addr;
	public Student() {
		//super();
		// TODO Auto-generated constructor stub
	}
	public Student(String id, String name, String age, String addr) {
		//super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.addr = addr;
	}
	
	//屬性定義爲了private所以要定義set、get方法
	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 getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	
	//定義show方法
	
	public void show() {
		System.out.println(id+","+name+","+age+","+addr);
	}
	
	
	
	
	
}

主方法

package com.it02;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import com.it02.Student;

/*
 * 	學生管理系統的主類
 * 	1-定義學生類
 * 	2-主界面代碼編寫
 * 	3-查看所有學生代碼編寫
 * 	4-添加學生代碼編寫
 * 	5-刪除學生代碼編寫
 * 	6-修改學生代碼編寫
 * 
 */
public class StudentMangerIO {
	//爲了讓程序回到主界面,使用循環
	
	public static void main(String[] args) throws IOException {
		//創建集合對象,用於存儲學生對象
		//ArrayList<Student> arr = new ArrayList<Student>();
		
		//定義文件路徑
		String fileName= "a.txt";
		
		while(true) {
			//主界面
			System.out.println("--------歡迎來到學生管理系統--------");
			System.out.println("1-查看所有學生");
			System.out.println("2-添加學生");
			System.out.println("3-刪除學生");
			System.out.println("4-修改學生");
			System.out.println("5-退出");
			
			System.out.println("請輸入你的選擇:");
			Scanner sc= new Scanner(System.in);
			String choiceString = sc.nextLine();		
			
			//用switch語句
			switch(choiceString) {
			case "1":
				//查看學生
				findAllStudent(fileName);
				break;
			case "2":
				//添加學生
				addStudent(fileName);
				break;
			case "3":
				//刪除學生
				deletdStudent( fileName);
				break;
			case "4":
				//修改學生
				updateStudent(fileName);
				break;
			case "5":
				//退出
				//System.out.println("謝謝你的使用");
				//break;
			default:
				System.out.println("謝謝你的使用");
				//退出程序
				System.exit(0);
				break;
				
			}
			
			
			

		}
		
		}
	public static void findAllStudent(String fileName) throws IOException{
		//創建集合對象
		ArrayList<Student> arr = new ArrayList<Student>();
		
		//從文件中讀取數據
		FileToArrayList(arr, fileName);
		
		//遍歷
		//首先判斷集合是否有數據
		if(arr.size()>0) {
			System.out.println("學號"+"\t"+"姓名"+"\t"+"年齡"+"\t"+"住處");
			for(int x=0;x<arr.size();x++) {
				Student s =arr.get(x);
				System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddr());
				
			}
		}else {
			System.out.println("不好意思,沒有數據存在,請重新選擇");
			//return;
		}
		
	}
	 
	
	public static void addStudent(String fileName) throws IOException {
		
		
		//創建集合對象
		ArrayList<Student> arr = new ArrayList<Student>();
		
		//從文件中讀取數據
		FileToArrayList(arr, fileName);
		// 創建鍵盤錄入對象

		Scanner sc = new Scanner(System.in);

		// 爲了讓id被訪問到
		String id;

		// 循環
		while (true) {
			// System.out.println("請輸入學生的信息");
			System.out.println("請輸入學生學號:");
			id = sc.nextLine();

			boolean flag = false;
			// 判斷學號有沒有被佔用
			for (int x = 0; x < arr.size(); x++) {
				Student s = arr.get(x);
				// 進行學號比較
				if (s.getId().equals(id)) {
					flag = true;
					break;
				}

			}
			if (flag) {
				System.out.println("你輸入的學號已被佔用,請重新佔用");
			} else {
				break;
			}

		}

		System.out.println("請輸入學生姓名:");
		String name = sc.nextLine();

		System.out.println("請輸入學生年齡:");

		String age = sc.nextLine();

		System.out.println("請輸入學生住處:");

		String addr = sc.nextLine();

		Student s = new Student();

		s.setId(id);
		s.setName(name);
		s.setAge(age);
		s.setAddr(addr);

		// 添加集合操作時需要注意
		arr.add(s);
		
		//將數據從新寫入文件
		ArrayListToFile(arr, fileName);
		System.out.println("添加成功");

	}

	//刪除
	public static void deletdStudent(String fileName) throws IOException {
		
		//創建集合對象
		ArrayList<Student> arr = new ArrayList<Student>();
		//從文件中讀取數據
		FileToArrayList(arr, fileName);
		
		// 根據學號刪除學生
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入要刪除學生的學號:");

		String id = sc.nextLine();

		// 遍歷
		int index = -1;
		for (int x = 0; x < arr.size(); x++) {
			Student s = arr.get(x);
			if (s.getId().equals(id)) {
				index = x;
				break;
			}

		}
		if (index == -1) {
			System.out.println("用戶不存在");
		} else {
			arr.remove(index);
			System.out.println("成功");
		}
		
		//從新寫入數據、
		ArrayListToFile(arr, fileName);
	}
	
	public static void updateStudent(String fileName) throws IOException {
		//創建集合對象
		ArrayList<Student> arr = new ArrayList<Student>();
		
		//從文件中讀取數據
		FileToArrayList(arr, fileName);
		
		
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入要修改學生的學號:");

		String id = sc.nextLine();
		
		int index = -1;
		for (int x = 0; x < arr.size(); x++) {
			Student s = arr.get(x);
			if (s.getId().equals(id)) {
				index = x;
				break;
			}

		}
		if (index == -1) {
			System.out.println("用戶不存在");
		} else {
			//修改
			System.out.println("請輸入學生新姓名:");
			String name = sc.nextLine();

			System.out.println("請輸入學生新年齡:");

			String age = sc.nextLine();

			System.out.println("請輸入學生新住處:");

			String addr = sc.nextLine();
			Student s = new Student();

			s.setName(name);
			s.setAge(age);
			s.setAddr(addr);
			
			arr.set(index, s);

			System.out.println("修改成功");
			
		}
		//從新寫入數據
		ArrayListToFile(arr, fileName);
		
		
	}
	
	//從文本讀數據到集合的方法
	public static void FileToArrayList(ArrayList<Student> arr,String FileName) throws IOException {
		//創建輸入緩衝六對象
		BufferedReader br = new BufferedReader(new FileReader(FileName));
		
		
		//讀取文件
		String line;
		while((line=br.readLine())!=null) {
			//分割字符串
			String[] str=line.split(",");
			Student s = new Student();
			s.setId(str[0]);
			s.setName(str[1]);
			s.setAge(str[2]);
			s.setAddr(str[3]);	
			
			//添加集合
			arr.add(s);
		} 
		br.close();
		
	}
	
	//從集合寫數據到文件的方法
	public static void ArrayListToFile(ArrayList<Student> arr,String FileName) throws IOException{
		//創建輸出緩衝六對象
		BufferedWriter bw = new BufferedWriter(new FileWriter(FileName));
		
		//遍歷集合,得到每一個學生信息並把信息按照一定的格式寫入到文本文件
		for (int x=0;x<arr.size();x++) {
			Student s = arr.get(x);
			StringBuilder sb= new StringBuilder();
			//鏈式編程
			sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddr());
			bw.write(sb.toString());
			bw.newLine();
			bw.flush();
			
			
		}
		//釋放資源
		bw.close();
	}
	
	
	
		

}


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