今天花了点时间把以前的一些代码修正了一下,看起来更规整了一些,配合登陆class使用,可以实现登陆之后进行各类操作的功能:
登陆:NameI.java
package com.test;
import java.util.Scanner;
public class NameI {
public static boolean login = false;
public static int i = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
String password = input.next();
NameI tryLogin = new NameI();
tryLogin.Judge(name, password);
}
public void Judge(String name , String password){
Scanner input2 = new Scanner(System.in);
switch(name){
case "Haruki":
if("123456".equals(password)){
System.out.println("账户1登陆成功");
login = true;
System.out.println("现在可以进行数值输入操作");
NumSave.main(null);
}
case "Asakawa":
if("951".equals(password)){
System.out.println("你正在尝试登陆账户2,是否继续?(y or n)");
String tof = input2.next();
if("y".equals(tof)){
System.out.println("账户2登陆成功");
login = true;
Reg();
}else{
System.out.println("你选择了否,请选择1.登陆其他账户 2.退出登陆程序");
int doit = input2.nextInt();
if(doit == 1){
main(null);
}else{
System.exit(0);
}
}
}
case "user":
if("753".equals(password)){
System.out.println("账户3登陆成功");
login = true;
System.out.println("现在可以选择所需的操作");
Exercise.main(null);
System.exit(0);
}
default :i++;
System.out.println("登陆信息不合法,剩余尝试次数:"+(5-i)+"次");
if(i == 5){
System.out.println("错误次数累计达到5次,禁止登陆");
System.out.println("终止登陆程序");
System.exit(0);
}
main(null);
}
}
public static void Reg(){
Scanner reg = new Scanner(System.in);
System.out.println("现在可以选择要进行的操作:");
System.out.println("0.注销登陆 1:数值输入操作 2:自动求和操作 3:数值读写操作 4.档案管理操作");
System.out.println("输入操作代号");
int choose = reg.nextInt();
switch(choose){
case 0:
System.exit(0);
case 1:
NumSave.save();//调用NumSave类的save方法
case 2:
MathTest.Sum();//调用MathTest类的Sum方法
case 3:
Exercise2.main(null);
case 4:
Register.main(null);
}
}
}
相比于旧的代码,这个新的代码看起来修改更方便了,当需求出现变动时,不需要再对代码进行大规模改动,而是找到对应的method然后进行局部变动即可。
面向对象就是指这个意思吗?