一个java作业,有好心人帮忙写下吗
1个回答
展开全部
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class StudentManager {
public static void main(String[] args) {
System.out.println("欢迎使用XXXX学生管理系统,下面请按照提示操作:\n");
boolean next = true;
Scanner scan = new Scanner(System.in);
User loginUser = null;
while(next){
System.out.print("请输入用户名:");
String userName = scan.nextLine();
System.out.print("请输入密码:");
String password = scan.nextLine();
loginUser = loginUser(userName, password);
if(loginUser == null) {
System.out.println("用户名或密码有误");
next = true;
continue;
}
next = false;
}
while(true){
System.out.println("\n**************************\n主菜单");
System.out.println("1. 查看学生");
System.out.println("2. 添加学生");
System.out.println("3. 超级管理");
System.out.println("4. 退出");
System.out.print("请选择:");
String choseVal = scan.nextLine();
System.out.println("您选择的是: "+choseVal+"\n");
if("1".equals(choseVal)) {
System.out.println("Name\t\tID\t\tMajor\t\tFree");
showAllStuds();
}else if("2".equals(choseVal)) {
System.out.print("请输入姓名,按回车确认:");
String name = scan.nextLine();
System.out.print("请输入ID,按回车确认:");
String id = scan.nextLine();
System.out.print("请输入Major,按回车确认:");
String major = scan.nextLine();
System.out.print("请输入Fee,按回车确认:");
String fee = scan.nextLine();
Student stud = new Student(id, name, major, fee);
DB.STUS.add(stud);
System.out.println("添加成功,您输入的学生信息如下:");
System.out.println("First Name\t\tID\t\tMajor\t\tFree");
showStud2(stud);
}else if("3".equals(choseVal)) {
adminManager(scan,loginUser);
}else if("4".equals(choseVal)) {
System.out.println("您已退出本系统,欢迎下次使用");
break;
}else {
System.out.println("输入命令有误,请重新输入。\n");
}
}
}
public static void adminManager(Scanner scan,User loginUser) {
while(true) {
System.out.println("\n**************************\n用户管理菜单");
System.out.println("1. 添加用户");
System.out.println("2. 查看用户名 密码");
System.out.println("3. 显示全部用户");
System.out.println("4. 返回主菜单");
System.out.print("请选择:");
String choseVal = scan.nextLine();
System.out.println("您选择的是: "+choseVal+"\n");
String userName = null;
if("1".equals(choseVal)) {
while(true) {
System.out.print("请输入姓名,按回车确认:");
userName = scan.nextLine();
if(userName==null || "".equals(userName.trim())) {
System.out.println("用户名不能为空");
continue;
}
break;
}
String password = null;
while(true) {
System.out.print("请输入6位密码,按回车确认:");
password = scan.nextLine();
if(password==null || password.length()<6) {
System.out.println("密码输入位数不够,请重新输入。\n");
continue;
}
break;
}
User user = new User(userName, password);
DB.DBUSERS.add(user);
System.out.println("添加成功,您输入的学生信息如下:");
System.out.println("Name\t\tPassword");
showUser(user);
}else if("2".equals(choseVal)) {
System.out.println("Name\t\tPassword");
showUser(loginUser);
}else if("3".equals(choseVal)) {
System.out.println("Name\t\tPassword");
showAllUsers();
}else if("4".equals(choseVal)) {
break;
}else {
System.out.println("输入命令有误,请重新输入。\n");
}
}
}
/**
* 用户登录
*/
public static User loginUser(String userName,String password) {
Iterator<User> dbIt = DB.DBUSERS.iterator();
while(dbIt.hasNext()) {
User user = dbIt.next();
if(user.getUserName().equals(userName) && user.getPassword().equals(password)) {
return user;
}
}
return null;
}
public static void showUser(User user){
System.out.println(user.getUserName()+"\t\t"+user.getPassword());
}
public static void showAllUsers(){
Iterator<User> userIt = DB.DBUSERS.iterator();
while(userIt.hasNext()) {
showUser(userIt.next());
}
}
/**
* 展示所有学生
*/
public static void showAllStuds(){
Iterator<Student> stdIt = DB.STUS.iterator();
while(stdIt.hasNext()) {
showStud1(stdIt.next());
}
}
/**
* 展示一个学生
*/
public static void showStud1(Student stud) {
System.out.println(stud.getName()+"\t\t"+stud.getId()+"\t\t"+stud.getMajor()+"\t\t"+stud.getFee());
}
/**
* 我去,怎么一个是Name一个是FirstName
*/
public static void showStud2(Student stud) {
System.out.println(stud.getFirstName()+"\t\t"+stud.getId()+"\t\t"+stud.getMajor()+"\t\t"+stud.getFee());
}
}
/**
* 用户对象
*/
class User{
private String userName;
private String password;
User(String userName,String password){
setUserName(userName);
setPassword(password);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* 学生对象
*/
class Student{
private String id;
private String name;
private String firstName;
private String major;
private String fee;
public Student(String id, String name, String major, String fee) {
setId(id);
setName(name);
setMajor(major);
setFee(fee);
}
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;
if(name == null || "".equals(name)) {
setFirstName(name);
return;
}
if(name.contains(" ")) {
setFirstName(name.split(" ")[0]);
}else {
setFirstName(name.substring(0, 1));
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getFee() {
return fee;
}
public void setFee(String fee) {
this.fee = fee;
}
}
/**
* 模拟数据库
*/
class DB{
// 用户表
static Set<User> DBUSERS = new HashSet<User>();
// 学生表
static Set<Student> STUS = new HashSet<Student>();
static {
DBUSERS.add(new User("LChaplin","1252"));
DBUSERS.add(new User("赵敏","000000"));
DBUSERS.add(new User("张无忌","000000"));
DBUSERS.add(new User("张三丰","000000"));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |