一、主函数(用户能看到的操作界面及命令执行)
package Demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class 实验 {
public static void main(String[] args) {
while(true) {
System.out.println("====《登录系统》====");
System.out.println("1、登录");
System.out.println("2、注册");
System.out.println("3、退出");
System.out.print("——选择:");
Scanner in = new Scanner(System.in);
int number = in.nextInt();
Login_OR_Registration lor = new Login_OR_Registration();
switch (number) {
case 1:
System.out.println();
System.out.println("登陆中...");
System.out.print("账号:");
String user = in.next();
System.out.print("密码:");
String password = in.next();
boolean flag = lor.Login(user,password,false);
if(flag) {
System.out.println("登录成功!!!");
}else {
System.out.println("登录失败!!!");
}
System.out.println();
break;
case 2:
System.out.println();
System.out.println("注册中...");
System.out.print("账号:");
String user1 = in.next();
System.out.print("密码:");
String password1 = in.next();
do{
boolean estimate = true;
boolean flag2 = lor.Login(user1,password1,estimate);
if(flag2) {
System.out.println("此账号已注册!!!");
}else {
_User ue = new _User(user1, password1);
lor.Registration(ue);
System.out.println("注册成功!!!");
}
}while(false);
System.out.println();
break;
case 3:
System.out.println("退出成功!!!");
System.exit(0);
break;
default:
System.out.println("请重新选择!!!");
break;
}
}
}
}
二、定义接口(登录方法与注册方法)
interface connector{
abstract boolean Login(String user,String password,boolean estimate);
abstract void Registration(_User ue);
}
三、实现接口
class Login_OR_Registration implements connector{
private static String path = "c:"+File.separator+"test"+File.separator+"zhanghao.txt";
private static File file = new File(path);
static {
if (file.exists()) {
}else {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("文件创建失败!!!");
}
}
}
public boolean Login(String user,String password,boolean estimate) {
boolean flag = false;
boolean pd =estimate;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line= null;
while((line = br.readLine())!=null) {
String[] split = line.split("=");
if(pd) {
if(split[0].equals(user)) {
flag =true;
}
}else {
if(split[0].equals(user) && split[1].equals(password)) {
flag =true;
}
}
}
} catch (FileNotFoundException e) {
System.out.println("找不到该文件!!!");
} catch (IOException e) {
System.out.println("登录失败!!!");
}finally {
if(br !=null) {
try {
br.close();
} catch (IOException e) {
System.out.println("资源释放失败!!!");
}
}
}
return flag;
}
public void Registration (_User ue) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file,true));
bw.write(ue.getUser_()+"="+ue.getPassword_());
bw.newLine();
bw.flush();
} catch (IOException e) {
System.out.println("用户注册失败!!!");
}finally {
if(bw !=null) {
try {
bw.close();
} catch (IOException e) {
System.out.println("释放失败!!!");
}
}
}
}
}
四、封装类(将当前注册的账号密码封装在此类中)
class _User{
private String user_;
private String password_;
public _User(String user_, String password_) {
this.user_ = user_;
this.password_ = password_;
}
public String getUser_() {
return user_;
}
public void setUser_(String user_) {
this.user_ = user_;
}
public String getPassword_() {
return password_;
}
public void setPassword_(String password_) {
this.password_ = password_;
}
}
五、运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/484942394a8abd98a559dad397820bb9.png)