1. 程式人生 > >java中用集合編寫簡單的學生資訊管理系統

java中用集合編寫簡單的學生資訊管理系統

public class Info {
 
	private int stu_id;
	private String stu_name;
	private String stu_sex;
	private int stu_age;
	private int chinese;
	private int math;
	private int english;
	private String stu_address;
	public int getStu_id() {
		return stu_id;
	}
	public void setStu_id(int stu_id) {
		this.stu_id = stu_id;
	}
	public String getStu_name() {
		return stu_name;
	}
	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}
	public String getStu_sex() {
		return stu_sex;
	}
	public void setStu_sex(String stu_sex) {
		this.stu_sex = stu_sex;
	}
	public int getStu_age() {
		return stu_age;
	}
	public void setStu_age(int stu_age) {
		this.stu_age = stu_age;
	}
	public int getChinese() {
		return chinese;
	}
	public void setChinese(int chinese) {
		this.chinese = chinese;
	}
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
	public int getEnglish() {
		return english;
	}
	public void setEnglish(int english) {
		this.english = english;
	}
	public String getStu_address() {
		return stu_addrese;
	}
	public void setStu_address(String stu_address) {
		this.stu_addrese = stu_address;
	}
	
	public double getAvgScose(){
		return (math+chinese+english)/3;
		
	}
	
	public int getSunScose(){
		return math+chinese+english;
		
	}
	
	
}
import java.io.ObjectInputStream.GetField;
import java.util.Scanner;

public class ChooseDemo {

	/*
	 *得到int型別的資料 
	 */
	public int getInputIntInfo(String info){
		int input_int=0;
		try{
			System.out.println(info);
			Scanner s=new Scanner(System.in);
			 input_int=s.nextInt();
		}catch (Exception e) {
			System.out.println("輸入錯誤!!請重新輸入。");
			input_int=getInputIntInfo(info);
		}
			return input_int;
		
	}
	
	/*
	 *得到String型別的資料 
	 */
	public String getInputStringInfo(String string){
		String input_String="";
		try{
			System.out.println(string);
			Scanner s=new Scanner(System.in);
			 input_String=s.next();
			}catch (Exception e) {
				System.out.println("輸入錯誤!!請重新輸入");
				input_String=getInputStringInfo(string);
			}
		return input_String;
	}
	
	public void menu(){
		System.out.println("************************");
		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("8.查詢學生資訊(根據姓名)");
		System.out.println("9.退出系統");
		System.out.println("************************");
	}
	
	public void Choose(int coad){
		switch (coad) {
		//新增學生資訊
			case 1:
				System.out.println("<---你已進入新增學生資訊介面--->");
				
				Info add_info=getStudentInfo();
			    if(!(Handle.selectStuId(add_info.getStu_id()))){
    				if(Handle.AddInfo(add_info)){
    					System.out.println("\n新增成功!!\n");
    				}else{
    					System.out.println("\n新增失敗!!\n");
    				}
				 }else{
					 System.out.println("該學號已存在!!!	請重新新增!!");
				 }
				break;
		//修改學生資訊(根據學號修改)	
			case 2:
				System.out.println("<---你已進入修改學生資訊(根據學號修改)介面--->");
				
				int update_id=getInputIntInfo("請輸入學號");
			
				  if(Handle.selectStuId(update_id)){
					System.out.println("<---請輸入新的學生資訊--->");
					Info update_infoById=getStudentInfo();
					  if(!(Handle.selectStuId(update_infoById.getStu_id()))||update_infoById.getStu_id()==update_id){
    					if(Handle.updateInfoById(update_id,update_infoById)){
    						System.out.println("\n修改成功!!!\n");
    					}else{
    						System.out.println("\n修改失敗!!!\n");
    						}
    				}else{
    					    System.out.println("該學號的學生資訊已存在!!!	請重新新增!!");
    					}
				}else{
					System.out.println("\n沒有該學號的學生資訊!!請重新選擇!\n");
				}	
				break;
		//修改學生資訊(根據姓名修改)	
			case 3:
				System.out.println("<---你已進入修改學生資訊(根據姓名修改)介面--->");
				String update_name=getInputStringInfo("請輸入姓名");
				
			
				if(Handle.selectStuName(update_name)){
					System.out.println("<---請輸入新的學生資訊--->");
					Info update_infoByName=getStudentInfo();
					if(!(Handle.selectStuId(update_infoByName.getStu_id()))||update_name.equals(update_infoByName.getStu_name())){
    						if(Handle.updateInfoByName(update_name,update_infoByName)){
    							System.out.println("\n修改成功!!\n");
    						}else{
    							System.out.println("\n修改失敗!!\n");
    							}
						}else{
						   System.out.println("該學號的學生資訊已存在!!!	請重新新增!!");
						}
					}else{
						System.out.println("\n沒有該學號的學生資訊!!請重新選擇!\n");
					}
				break;
		//刪除學生資訊(根據學號刪除)
			case 4:
				System.out.println("<---你已進入刪除學生資訊(根據學號刪除)介面--->");
				int delete_id=getInputIntInfo("請輸入學號");
			
				 if(Handle.selectStuId(delete_id)){
					if(Handle.deleteInfoById(delete_id)){
						System.out.println("\n刪除成功!!!\n");
					}else{
						System.out.println("\n刪除失敗!!!\n");
						}
				}else{
					System.out.println("\n沒有該學號的學生資訊!!請重新選擇!\n");
				}	
				break;
		//刪除學生資訊(根據姓名刪除)	
			case 5:
				System.out.println("<---你已進入刪除學生資訊(根絕姓名刪除)介面--->");
				String delete_name=getInputStringInfo("請輸入姓名");
				
				if(Handle.selectStuName(delete_name)){
					if(Handle.deleteInfoByName(delete_name)){
						System.out.println("\n刪除成功!!!\n");
					}else{
						System.out.println("\n刪除失敗!!!\n");
					}
				}else{
					System.out.println("\n沒有該學生的資訊!!請重新選擇!\n");
				}
				break;
		//查詢所有學生資訊	
			case 6:
				System.out.println("<---你已進入所有學生資訊介面--->");
				Handle.selectAllInfo();
				break;
		//查詢學生資訊介面(根據學號查詢)	
			case 7:
				System.out.println("<---你已進入查詢學生資訊介面(根據學號查詢)--->");
				int select_id=getInputIntInfo("請輸入學號");
				if(Handle.selectStuId(select_id)){
					Info id=Handle.selectInfoById(select_id);
					if(id!=null){
					}
				}else{
					System.out.println("\n沒有該學生資訊!!請重新選擇!\n");
				}
				break;
		//查詢學生資訊介面(根據姓名查詢)
			case 8:
				System.out.println("<---你已進入查詢學生資訊介面(根據姓名查詢)--->");
				String select_name=getInputStringInfo("請輸入姓名");
				if(Handle.selectStuName(select_name)){
					Info name=Handle.selectInfoByName(select_name);
					if(name!=null){
					}
				}else{
					System.out.println("\n沒有該學生資訊!!請重新選擇!\n");
				}
				break;
		//退出系統
			case 9:System.out.println("退出成功!");
				System.exit(0);
				break;
		default:
			System.out.println("沒有該選項!!請重新輸入!");
			break;
		}
	}
	//給 Info類的學生屬性進行賦值
	private Info getStudentInfo(){
		Info info=new Info();
		info.setStu_id(getInputIntInfo("請輸入學號"));
		info.setStu_name(getInputStringInfo("請輸入姓名"));
		info.setStu_sex(getInputStringInfo("請輸入性別"));
		info.setStu_age(getInputIntInfo("請輸入年齡"));
		info.setChinese(getInputIntInfo("請輸入語文成績"));
		info.setMath(getInputIntInfo("請輸入數學成績"));
		info.setEnglish(getInputIntInfo("請輸入英語成績"));
		info.setStu_address(getInputStringInfo("請輸入地址"));
		info.getAvgScose();
		info.getSunScose();
		return info;
		
	}
}
import java.util.ArrayList;

public class Handle {

	private static ArrayList list=new ArrayList();
	//新增學生資訊
	public static boolean AddInfo(Info add_info) {
		boolean add=false;
		add=list.add(add_info);
		return add;
	}
	//根據學號修改學生資訊
	public static boolean updateInfoById(int id, Info update_infoById) {
		boolean update=false;
		for(Object obj:list){
			Info info=(Info)obj;
			if(id==info.getStu_id()){
				list.set(list.indexOf(info), update_infoById);
				update=true;
				break;
			}
		}
		return update;
	}
	
	//根據姓名修改學生資訊
	public static boolean updateInfoByName(String name, Info update_infoByName) {
		boolean update=false;
		for(Object obj:list){
			Info info=(Info)obj;
			if(name.equals(info.getStu_name())){
				list.set(list.indexOf(info), update_infoByName);
				update=true;
				break;
			}
		}
		return update;
	}
	
	//根據學號刪除學生資訊
	public static boolean deleteInfoById(int delete_id) {
		boolean delete=false;
		for(Object obj:list){
			Info info=(Info)obj;
			if(delete_id==info.getStu_id()){
				delete=list.remove(info);
				break;
			}
		}
		return delete;
	}
	

	//根據姓名刪除資訊
	public static boolean deleteInfoByName(String delete_name) {
		boolean delete=false;
		for(Object obj:list){
			Info info=(Info)obj;
			if(delete_name.equals(info.getStu_name())){
				delete=list.remove(info);
				break;
			}
		}
		return delete;
	}
	
	
	//查詢所有學生資訊
	public static void selectAllInfo() {
		for(Object obj:list){
			Info info=(Info)obj;
			StringBuilder stringBuilder=new StringBuilder();
			stringBuilder.append("學號:"+info.getStu_id());
			stringBuilder.append("\t姓名:"+info.getStu_name());
			stringBuilder.append("\t性別:"+info.getStu_sex());
			stringBuilder.append("\t年齡:"+info.getStu_age());
			stringBuilder.append("\n語文成績:"+info.getChinese());
			stringBuilder.append("\t數學成績:"+info.getMath());
			stringBuilder.append("\t英語成績:"+info.getEnglish());
			stringBuilder.append("\t平均成績:"+info.getAvgScose());
			stringBuilder.append("\t總成績:"+info.getSunScose());
			stringBuilder.append("\n地址:"+info.getStu_address()+"\n");
			System.out.println(stringBuilder.toString());
		}
		
	}
	
	//根據學號查詢資訊
	public static Info selectInfoById(int select_id) {
		Info in=null;
		for(Object obj:list){
			Info info=(Info)obj;
			if(select_id==info.getStu_id()){
				System.out.println("\n該學生資訊如下:\n");
				StringBuilder stringBuilder=new StringBuilder();//同上
				stringBuilder.append("學號:"+info.getStu_id());
				stringBuilder.append("\t姓名:"+info.getStu_name());
				stringBuilder.append("\t性別:"+info.getStu_sex());
				stringBuilder.append("\t年齡:"+info.getStu_age());
				stringBuilder.append("\n語文成績:"+info.getChinese());
				stringBuilder.append("\t數學成績:"+info.getMath());
				stringBuilder.append("\t英語成績:"+info.getEnglish());
				stringBuilder.append("\t平均成績:"+info.getAvgScose());
				stringBuilder.append("\t總成績:"+info.getSunScose());
				stringBuilder.append("\n地址:"+info.getStu_address()+"\n");
				System.out.println(stringBuilder.toString());
				in=info;
			}
		}
		return in;
	}
	
	//根據姓名查詢資訊
	public static Info selectInfoByName(String select_name) {
		Info in=null;
		for(Object obj:list){
			Info info=(Info)obj;
			if(select_name.equals(info.getStu_name())){
				StringBuilder stringBuilder=new StringBuilder();
				stringBuilder.append("學號:"+info.getStu_id());
				stringBuilder.append("\t姓名:"+info.getStu_name());
				stringBuilder.append("\t性別:"+info.getStu_sex());
				stringBuilder.append("\t年齡:"+info.getStu_age());
				stringBuilder.append("\n語文成績:"+info.getChinese());
				stringBuilder.append("\t數學成績:"+info.getMath());
				stringBuilder.append("\t英語成績:"+info.getEnglish());
				stringBuilder.append("\t平均成績:"+info.getAvgScose());
				stringBuilder.append("\t總成績:"+info.getSunScose());
				stringBuilder.append("\n地址:"+info.getStu_address()+"\n");
				System.out.println(stringBuilder.toString());
				in=info;
			}
		}
		return in;
	}
	
	//得到學號
	public static boolean selectStuId(int id) {
		boolean fag=false;
		for(Object obj:list){
			Info info=(Info)obj;
			if(id==info.getStu_id()){
				fag=true;
				break;
			}
		}
		return fag;
	}
	
	//得到姓名
	public static boolean selectStuName(String name){
		boolean fag=false;
		for(Object obj:list){
			Info info=(Info)obj;
			if(name.equals(info.getStu_name())){
				fag=true;
				break;
			}
		}
		return fag;
	}
	
	
}
public class TestMemo {

	
	public static void main(String[] args) {
		
		ChooseDemo ch=new ChooseDemo();
		while(true){
		ch.menu();
		int choose=ch.getInputIntInfo("<請輸入數字選擇操作>");
		ch.Choose(choose);
		}
	}

}