一、项目内容
这个项目也就是期末实训中,难度相当于普通一点的项目,跟我之前发的项目2复杂度差不多。主要实现的功能是:
1、注册和登录
2、登录后查看个人的信息
3、选课以及学分的实时计算
二、代码
1、框架部分与Student类
静态的部分仍然是基础的数据,然后就是主函数调用的第一个页面的函数,这些都是大体的框架,相信大家看一眼就明白了。
import java.util.*;
public class Pro3{
static Scanner sc = new Scanner(System.in);
static HashSet<String> courseSet = new HashSet<>(); //存放所有课程
static HashSet<Student> stuSet = new HashSet<>(); //存放所有学生
static{Collections.addAll(courseSet,"Csgo-5","Lol-5","Java-5","Steam-4","Python-4","C++-3","C#-3");}
static Student s1 = new Student("AAAAA","AAAAAA");
static Student s2 = new Student("BBBBB","BBBBBB");
public static void main(String[] atghs){
Collections.addAll(stuSet,s1,s2);
panel_1();
}
public static void panel_1(){
while(true){
System.out.println("A:注册账号\nB:登录账号");
String str = sc.next();
System.out.println(str);
str = check(str,"[AB]");
if(str.equals("A")){
selectA();
}else{
selectB();
}
}
}
}
这部分是学生类,这里面除了封装的属性,调用属性的方法,构造方法,之外,还包括了自动生成的学号,这个学号是根据目前已经有的学生的数量生成,第八位学生学号就是0008,如果删除一位,再加上一位也是0008,这样并不符合实际情况,但是没有关系,练练手嘛。
import java.util.*;
class Student{
private String stuid;
private String name;
private int credit;
private HashSet<String> projects;//课程
private String password;
//构造方法
public Student(String name,String password){
this.name = name;
this.password = password;
int ax = (Pro3.stuSet.size() + 1);
String bx = "";
if(ax / 10 == 0){bx = "000" + ax;}
if(ax / 100 == 0){bx = "00" + ax;}
if(ax / 1000 == 0){bx = "0" + ax;}
if(ax / 10000 == 0){bx = "" + ax;}
this.stuid = bx;
this.credit = 0;
this.projects = new HashSet<>();
}
public String toString( ) {
return "学号:"+ stuid +" 姓名:"+name +" 学分:"+ credit +" 课程:" + projects;
}
public boolean equals(Object obj){
if(this == obj)return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student x = (Student)obj;
if(x.name.equals(this.name))return true;
return false;
}
public int hashCode(){return Objects.hash(name); }
private String getStuId(){return stuid;}
public String getName(){ return name;}
public int getCredit() {return credit;}
public String getStuid() {return stuid;}
public String getPassword() {return password;}
public HashSet<String> getProjects() {return projects;}
public void setName(String name){this.name = name;}
public void addCredit(int credit){this.credit += credit;}
public void subCredit(int credit){this.credit -= credit;}
public void setPassword(String password){this.password = password;}
public void addProjects(String ax){this.projects.add(ax);}
public void subProjects(String ax){this.projects.remove(ax);}
public HashSet<String> getPro(){return projects;}
}
2、功能模块
A、注册与登录
因为目前还没学数据库,所以注册登录部分还是比较水的,就是往集合里面添加以及遍历集合进行登录。
注册部分。
public static void selectA(){
System.out.println("请输入账号名[只能出现字母 5-10位]:");
String ax = sc.next();
//ax = check(ax,"[a-zA-Z]{5,10}");
System.out.println("请输入密码[只能出现字母/数字/_ 6-12位]:");
String bx = sc.next();
bx = check(bx,"[a-zA-Z0-9_]{6,12}");
System.out.println(check_stu(ax,bx) ? "注册成功" : "注册失败");
}
public static boolean check_stu(String ax,String bx){
Student x = new Student(ax,bx);
int s1 = stuSet.size();
stuSet.add(x);
if(stuSet.size() - s1 == 1)return true;
return false;
}
这个check方法,就是一个通用的,可以用正则表达式进行格式检查的输入值都用这个check方法。
private static String check(String in,String regex){
while(!in.matches(regex)){
System.out.println("李在赣神魔,请重新输入:");
in = sc.next();
}
return in;
}
登录部分
public static void selectB(){
System.out.println("请输入账号名[只能出现字母 5-10位]:");
String ax = sc.next();
System.out.println("请输入密码[只能出现字母/数字/_ 6-12位]:");
String bx = sc.next();
Student x =null;
for(Student ss : stuSet){if(ss.getName().equals(ax)){x = ss;break;}}
System.out.println(x);
if(login_stu(ax,bx)){
System.out.println("登录成功");
panel_2(x);
}
else System.out.println("登录失败");
}
public static boolean login_stu(String ax,String bx){
Student x = new Student(ax,bx);
int s1 = stuSet.size();
stuSet.add(x);
if(stuSet.size() - s1 == 1){stuSet.remove(x);return false;}
else return true;
}
B、个人信息界面
首先是登录后可以做的事情,我们首先把ADEF拿出来看吧!BC部分将在下一个小标题展示。
public static void select_2(Student x){
while(true){
System.out.println("A:查看个人信息\nB:添加选修课程\nC:删除选修课程\nD:修改个人信息\nE:注销账号\nF:返回首页面");
String xx = sc.next();
xx = check(xx,"[ABCDEF]");
if(xx.equals("A")){show(x);}
else if(xx.equals("B")){addClass(x);}
else if(xx.equals("C")){deleteClass(x);}
else if(xx.equals("D")){modify(x);}
else if(xx.equals("E")){remove(x);break;}
else if(xx.equals("F")){break;}
}
}
选项ADEF
//选项A
public static void show(Student x){
System.out.println(x);
}
//选项D
public static void modify(Student x){
Student ss = null;
a:while(true){
System.out.println("请输入修改后的账号名[只能出现字母 5-10位]:");
String xx = sc.next();
xx = check(xx,"[a-zA-Z]{5,10}");
System.out.println("请输入修改后的密码[只能出现字母/数字/_ 6-12位]:");
String yy = sc.next();
yy = check(yy,"[a-zA-Z0-9_]{6,12}");
if(x.getName().equals(xx) && x.getPassword().equals(yy)){
System.out.println("你根本没改,别想糊弄我!重输!");
continue a;
}
ss = new Student(xx,yy);
break;
}
for(Iterator<Student> car = stuSet.iterator(); car.hasNext(); ){
Student xx = car.next();
if(xx.equals(x)){
car.remove();
}
}
stuSet.add(ss);
}
//选项E
public static void remove(Student x){
stuSet.remove(x);
}
C、选课以及实时计算的学分
大学生基础课程有CSGO和LOL一定很合理吧.jpg,这个格式是老师定下的,也还是之前那个项目就做过的检测“-”的操作,已经是基础操作了。
public static void addClass(Student x){
HashSet<String> temp = new HashSet<>(courseSet);
temp.removeAll(x.getProjects());
System.out.println(temp+"\n请选择你需要上的课");
String bx = sc.next();
bx = check_gs(bx);
bx = check_dx_1(bx);
String dx[] = bx.split("-");
int Dx = Integer.parseInt(dx[1]);
for(Student xx : stuSet){
if(x.equals(xx)){
x.addProjects(bx);
x.addCredit(Dx);
}
}
}
public static void deleteClass(Student x){
System.out.println(x.getProjects() + "\n请选择你需要推掉的课");
String bx = sc.next();
bx = check_gs(bx);
bx = check_dx_2(bx,x);
String dx[] = bx.split("-");
int Dx = Integer.parseInt(dx[1]);
for(Student xx : stuSet){
if(xx.equals(x)){
xx.subProjects(bx);
xx.subCredit(Dx);
}
}
}
校验部分,每次删除和登录,都应该从各自的列表中进行操作,我写的逻辑是,可以选择的课程是静态的,不变的,每个学生初始都是没有选课信息的,然后进行选课,这样已经选了的课程信息就会储存在该对象中,成为这个对象的属性,每次展示自己的选课信息和没选课的信息的时候,是用静态的课程信息,减去该学生类的已经选的课程的信息。
public static String check_dx_1(String bx){
while(true){
for(Iterator<String> car = courseSet.iterator(); car.hasNext(); ){
String x = car.next();
if(x.equals(bx))return bx;
}
System.out.println("请重新输入:");
bx = sc.next();
}
}
public static String check_dx_2(String bx,Student ss){
while(true){
for(Iterator<String> car = ss.getProjects().iterator(); car.hasNext(); ){
String x = car.next();
if(x.equals(bx))return bx;
}
System.out.println("请重新输入:");
bx = sc.next();
}
}
三、小结
这个项目的复杂度,感觉跟之前的项目2差不多,但是由于我们的学习,可以简化更多的代码,使得一些方法的复用率增加,让我们的代码量减少了很多。
5万+

被折叠的 条评论
为什么被折叠?



