天天看點

Day06_【學生管理系統】v1.0

【注】:功能描述

系統首頁:選擇身份,輸入賬号密碼登入

教師賬号:admin 密碼:admin123

教師功能:查詢學生清單、添加學生、修改學生、删除學生、傳回首頁、退出系統

學生賬号:student 密碼:student123

學生功能:查詢學生清單、傳回首頁、退出系統

【注】:暫時采用數組實作資料存儲。POP。未定義其他類,人員資訊僅包含姓名,無修改賬号密碼等功能。其他功能日後完善。

Day06_【學生管理系統】v1.0

Day09_【學生管理系統】v1.1

Day11_【學生管理系統】v2.0

package com.csr;

import java.util.Scanner;

public class StudentManager {

	public static void main(String[] args) {
		StudentManager sysManager = new StudentManager();
		Scanner input = new Scanner(System.in);
		String[] stuList = {"丁丁", "迪西", "拉拉"};
		while (true) {
			boolean end1 = false;
			sysManager.begin();
			System.out.println("1:教師登入      2:學生登入");
			int choice = input.nextInt();
			boolean flag = sysManager.selectLogin(choice);
			while (true) {
				if (flag == true) {
					break;
				}
				sysManager.begin();
				flag = sysManager.selectLogin(choice);
			}	
			if (choice == 1) {
				while (true) {
					boolean end2 = false;
					System.out.println("1:學生清單      2:學生添加      3:學生修改      4:學生删除      5:傳回首頁      6:退出");
					int choice_t = input.nextInt();
					switch (choice_t) {
						case 1: // 查詢
							showList(stuList);
							break;
						case 2: // 添加
							stuList = addStu(stuList);
							break;
						case 3: // 修改
							stuList = repStu(stuList);
							break;
						case 4: // 删除
							stuList = delStu(stuList); 	
							break;
						case 5:	// 傳回
							end2 = true; 
							break;
						case 6: // 退出
							end1 = true;
							end2 = true;
							break;
						default:
							System.out.println("輸入有誤請重新輸入");
							break;
					}
					if (end2) {
						break;
					}
				}
			} else {
				while (true) {
					boolean end2 = false;
					System.out.println("1:學生清單      2:傳回首頁      3:退出");
					int choice_s = input.nextInt();
					switch (choice_s) {
						case 1: // 查詢
							showList(stuList);
							break;
						case 2: // 傳回
							end2 = true;
							break;
						case 3: // 退出
							end1 = true;
							end2 = true;
							break;	
						default:
							System.out.println("輸入有誤請重新輸入");
							break;
					}
					if (end2) {
						break;
					}
				}			
			}
			if (end1) {
				break;
			}
		}		
	}
	
	public static boolean tea_login() {
		String[] teacher = {"admin","admin123"};
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入您的賬号");
		String idt = in.next();
		System.out.println("請輸入您的密碼");
		String keyt = in.next();
		if (idt.equals(teacher[0]) && keyt.equals(teacher[1])) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean stu_login() {
		String[] student = {"student","student123"};
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入您的賬号");
		String ids = in.next();
		System.out.println("請輸入您的密碼");
		String keys = in.next();
		if (ids.equals(student[0]) && keys.equals(student[1])) {
			return true;
		} else {
			return false;
		}
	}
	
	public static void showList(String[] l) {
		for (int i = 0; i < l.length; i++) {
			System.out.print("學生姓名:");
			System.out.println(l[i]);
		}
	}
	
	public static String[] addStu(String[] l) {
		String a[] = new String[l.length+1];
		System.out.println("請輸入學生姓名:");
		String name = new Scanner(System.in).next();
		for (int i = 0; i < l.length; i++) {
			a[i] = l[i];
		}
		a[l.length] = name;
		System.out.println("添加成功");
		return a;
	}
	
	public static String[] repStu(String[] l) {
		String[] r = new String[l.length];
		System.out.println("請輸入您需要修改的學生姓名");
		int count = 0;
		String name_old = new Scanner(System.in).next();
		for (int i = 0; i < l.length; i++) {
			if (l[i].equals(name_old)) {
				count++;
			}
		}
		if (count != 0) {
			System.out.println("請輸重新确認該學生的姓名");
			String name_new = new Scanner(System.in).next();
			for (int i = 0; i < l.length; i++) {
				r[i] = l[i];
				if (l[i].equals(name_old)) {
					r[i]= name_new; 
				}
			}
			System.out.println("修改成功");
			return r;
		} else {
			System.out.println("沒有查找到該學生");
			return l;
		}	
	}
	
	public static String[] delStu(String[] l) {
		String[] d = new String[l.length-1];
		System.out.println("請輸入您需要删除的學生姓名");
		String name = new Scanner(System.in).next();
		int count = 0;
		for (int i = 0; i < l.length; i++) {
			if (l[i].equals(name)) {
				count++;
			}
		}
		if (count != 0) {
			for (int i = 0,j = 0; i < l.length; i++,j++) {
				if (!l[i].equals(name)) {
					d[j]= l[i]; 
				} else {
					j--;
				}
			}
			System.out.println("刪除成功");
			return d;
		} else {
			System.out.println("刪除失敗,沒有找到對應的學生");
			return l;
		}		
	}
	
	public void begin() {
		System.out.println("*********************************");
		System.out.println("*******歡迎進入學生管理系統*******");
		System.out.println("*********************************");
	}
	
	public boolean selectLogin(int para) {
		boolean f = true;
		if (para == 1) {
			f = tea_login();
		} else if (para == 2) {
			f =  stu_login();
		} else {
			f = false;
		}
		return f;
	}

}