java----學生管理系統(簡易版)


public class students {
	
	private String id;
	
	private String name;
	
	private String age;
	
	private String address;
	
	private int[] score=new int[3];
	
	public students() {
		
	}
	public students(String id, String name, String age, String address,int arr[]) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
		for(int i=0;i<3;i++)
		{
			this.score[i]=arr[i];
		}
	}

	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;
	}
	public void setScore(int arr[]) {
		for(int i=0;i<3;i++)
		{
			this.score[i]=arr[i];
		}
	}
	
	public int getChineseScore() {
		return score[0];
	}
	
	public void setChineseScore(int score) {
		this.score[0]=score;
	}
	
	public int getMathScore() {
		return score[1];
	}
	
	public void setMathScore(int score) {
		this.score[1]=score;
	}
	
	public int getEnglishScore() {
		return score[2];
	}
	
	public void setEnglishScore(int score) {
		this.score[2]=score;
	}

}

  

 

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

public class StuMange {
	public static void main(String[] args) throws IOException {
		ArrayList<students> array=new ArrayList<students>();
		LoadAllInfo(array);
		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("6 學號排序");
			System.out.println("7 退出");
			System.out.println("請輸入你的選擇:");
			Scanner sc = new Scanner(System.in);
			String choiceString = sc.nextLine();
			switch (choiceString) {
			case "1":
				findAllStudent(array);
				break;
			case "2":
				addStudent(array);
				break;
			case "3":
				deleteStudent(array);
				break;
			case "4":
				updateStudent(array);
				break;
			case "5":
			default:
				System.out.println("謝謝你的使用 ");
				System.exit(0);
				break;
			}
		}
	}
	
	private static void LoadAllInfo(ArrayList<students>arrayList) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("student.txt"));
		String line;
		while((line=br.readLine())!=null) {
			String [] strAString=line.split(",");
			students s2=new students();
			s2.setId(strAString[0]);
			s2.setName(strAString[1]);
			s2.setAge(strAString[2]);
			s2.setAddress(strAString[3]);
			s2.setChineseScore(Integer.parseInt(strAString[4]));
			s2.setMathScore(Integer.parseInt(strAString[5]));
			s2.setEnglishScore(Integer.parseInt(strAString[6]));
			arrayList.add(s2);
	}
}
	
	private static void updateStudent(ArrayList<students> array) throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你要修改學生的學號:");
		String id = sc.nextLine();
		int index = -1;
		for (int x = 0; x < array.size(); x++) {
			students s = array.get(x);
			if (s.getId().equals(id)) {
				index = x;
				break;
			}
		}
		if (index != -1) {
			System.out.println("請輸入新姓名:");
			String name = sc.nextLine();
			System.out.println("請輸入新年齡:");
			String age = sc.nextLine();
			System.out.println("請輸入新地址:");
			String address = sc.nextLine();
			System.out.println("請輸入新的成績單(語數外)");
			int arr[]=new int [3];
			for(int i=0;i<3;i++)
			{
				arr[i]=sc.nextInt();
			}
			students s = new students();
			s.setId(id);
			s.setName(name);
			s.setAge(age);
			s.setAddress(address);
			s.setScore(arr);
			array.set(index, s);
			System.out.println("修改學生成功");
			RefreshInfo(array);
		} else {
			System.out.println("不好意思,你要修改的學號對應的學生信息不存在,請回去重新選擇你的操作");
			return;
		}
	}

	private static void deleteStudent(ArrayList<students> array) throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你要刪除學生的學號:");
		String id = sc.nextLine();
		int index = -1;
		for (int x = 0; x < array.size(); x++) {
			students s = array.get(x);
			if (s.getId().equals(id)) {
				index = x;
				break;
			}
		}
		if (index != -1) {
			array.remove(index);
			RefreshInfo(array);
			System.out.println("刪除學生成功");
		} else {
			System.out.println("不好意思,你要刪除的學號對應的學生信息不存在,請回去重新選擇你的操作");
			return;
		}
	}
	
	
	private static void RefreshInfo(ArrayList<students> array) throws IOException {
        BufferedWriter bW=new BufferedWriter(new FileWriter("student.txt"));
		for(int i=0;i<array.size();i++)
		{
			StringBuilder strbuilder=new StringBuilder();
			students s1=array.get(i);
			
			strbuilder.append(s1.getId()).append(",").
			append(s1.getName()).append(",").
			append(s1.getAge()).append(",").
			append(s1.getAddress()).append(",").
			append(s1.getChineseScore()).append(",").
			append(s1.getMathScore()).append(",").
			append(s1.getEnglishScore());
			
			bW.write(strbuilder.toString());
			bW.newLine();
			bW.flush();
		}
		bW.close();
}
	

	private static void addStudent(ArrayList<students> array) throws IOException {
		Scanner sc = new Scanner(System.in);
		String id;
		while (true) {
			System.out.println("請輸入學號:");
			id = sc.nextLine();

			boolean flag = false;
			for (int x = 0; x < array.size(); x++) {
				students s = array.get(x);
				if (s.getId().equals(id)) {
					flag = true;
				}
			}

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

		System.out.println("請輸入姓名:");
		String name = sc.nextLine();
		System.out.println("請輸入年齡:");
		String age = sc.nextLine();
		System.out.println("請輸入地址:");
		String address = sc.nextLine();
		System.out.println("請輸入語文,數學,英語的成績");
		int arr[]=new int[3];
		for(int i=0;i<3;i++)
		{
			arr[i]=sc.nextInt();
		}
		students s = new students();
		s.setId(id);
		s.setName(name);
		s.setAge(age);
		s.setAddress(address);
		s.setScore(arr);
		array.add(s);
		RefreshInfo(array);
		System.out.println("添加學生成功");
	}

	private static void findAllStudent(ArrayList<students> array) {
		if (array.size() == 0) {
			System.out.println("不好意思,目前沒有學生信息可供查看,請回去重新選擇你的操作");
			return;
		}
		System.out.println("學號\t姓名\t年齡\t居住地\t語文\t數學\t英語");
		for (int x = 0; x < array.size(); x++) {
			students s = array.get(x);
			System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" 
			+s.getAddress()+"\t"+s.getChineseScore()+"\t"+s.getMathScore()+"\t"+s.getEnglishScore());
		}
	}
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM