猿问

如何创建一个java程序让用户使用密码登录并存储数据

我正在尝试制作一个简单的java密码管理器(我不担心我现在如何存储密码,因为我只是想首先让整体结构正确)。我陷入了如何允许用户登录的困境。我有一个存储用户名和密码的文件 - 因此,我首先使用扫描仪从未经身份验证的用户获取输入,然后对照文本文件检查它是否与帐户匹配。如果用户输入与用户名和密码匹配,我怎样才能让该用户访问他的 User 对象?我遇到的问题是这个用户对象已经被创建(当创建帐户时),但是我如何让用户登录并访问用户类中的所有方法,即changeUserPassword或getPassword?


我已经考虑过使用不同的设计模式,例如观察者,但我认为这些模式不适合我想要做的事情。


我想知道是否有人知道我可以遵循的设计模式,或者知道一个实现,可以让我访问已创建的用户类之外的对象(用户)(即已经创建帐户)并进行更改它。


public void login() throws IOException {

    // use a scanner to get login details

    Scanner s = new Scanner(System.in);


    System.out.print("email: ");

    String email = s.next();


    System.out.print("password: ");

    String password = s.next();


    String check = email + ", " + password;


    // loop through file and check if we find a matching email and password

    File f = new File("member.txt");

    Scanner sc = new Scanner(f);


    while (sc.hasNext()) {

        if (sc.nextLine().equals(check)) {

            System.out.println("Logging in...");

            // how do i now access the user object that matches the username and password that were given?

        }

    }

}


守候你守候我
浏览 154回答 1
1回答

慕桂英546537

这不是您与数据库交互的方式。编写一个User类来存储用户名和密码。然后编写一个数据库类来读取文本文件并创建 User 对象的 ArrayList。然后使用 User.name 进行搜索,并在登录前检查 User.password 进行验证。数据库会将文本文件中的数据“加载”到 ArrayList 中,这将是您的程序将与之交互的内容。如果您想重写文本文件,请编写一个保存函数来清除并打印到相关文件。这是一个数据库类的示例(来自我的一个旧项目),它以以下格式读取学生的数据: name:HARSH|PID:12|major:null|minor:null|CGPA:4.100000|college:null|email:abcd@gmail.comimport java.util.*;import java.text.*;import java.time.*;import java.io.*;public class Database{&nbsp; &nbsp; public List<Student> studentList;&nbsp; &nbsp; private long lastModified;&nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; public Object loadDb()&nbsp; &nbsp; {//Loads the database from file.&nbsp; &nbsp; &nbsp; &nbsp; File data = new File("database.db");&nbsp; &nbsp; &nbsp; &nbsp; if(lastModified == data.lastModified())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return studentList;//Returning main memory;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc = new Scanner(data).useDelimiter("\n");//To get individual lines.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(studentList!=null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//Clearing main memory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; studentList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//Creating new main memory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; studentList = new ArrayList<Student>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(sc.hasNext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner l = new Scanner(line).useDelimiter("\\|");//To get info&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name, PID, major, minor, cgpaSt, college, email;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PID = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; major = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minor = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cgpaSt = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; college = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email = l.next().split(":")[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double CGPA = Double.valueOf(cgpaSt);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student stud = new Student(name,PID, major, minor, CGPA, college, email);//Creating new student with same info.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; studentList.add(stud);//Adding the student to memory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return studentList;&nbsp; &nbsp; }&nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; public void updateDb(Object studList)&nbsp; &nbsp; {//Updates the database.&nbsp; &nbsp; &nbsp; &nbsp; File data = new File("database.db");&nbsp; &nbsp; &nbsp; &nbsp; PrintWriter fs;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs = new PrintWriter(data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(IOException e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("IO Exception!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fs.flush();&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Student>studs = (ArrayList<Student>)studList;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i<studs.size();i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs.print(studs.get(i).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i != studs.size() -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs.print("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fs.close();&nbsp; &nbsp; &nbsp; &nbsp; lastModified = data.lastModified();&nbsp; &nbsp; &nbsp; &nbsp; loadDb();//Loading updated database.&nbsp; &nbsp; }}您必须toString()为您的用户类编写一个函数,并根据您的格式修改读数。Database userData = new Database();这将允许您在中心类中创建一个变量。然后,您可以与studentList(在您的例子中为userList)交互,并搜索该用户,然后检查他们的密码是否正确。
随时随地看视频慕课网APP

相关分类

Java
我要回答