第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何創(chuàng)建一個java程序讓用戶使用密碼登錄并存儲數(shù)據(jù)

如何創(chuàng)建一個java程序讓用戶使用密碼登錄并存儲數(shù)據(jù)

守候你守候我 2023-06-28 15:43:04
我正在嘗試制作一個簡單的java密碼管理器(我不擔心我現(xiàn)在如何存儲密碼,因為我只是想首先讓整體結(jié)構(gòu)正確)。我陷入了如何允許用戶登錄的困境。我有一個存儲用戶名和密碼的文件 - 因此,我首先使用掃描儀從未經(jīng)身份驗證的用戶獲取輸入,然后對照文本文件檢查它是否與帳戶匹配。如果用戶輸入與用戶名和密碼匹配,我怎樣才能讓該用戶訪問他的 User 對象?我遇到的問題是這個用戶對象已經(jīng)被創(chuàng)建(當創(chuàng)建帳戶時),但是我如何讓用戶登錄并訪問用戶類中的所有方法,即changeUserPassword或getPassword?我已經(jīng)考慮過使用不同的設(shè)計模式,例如觀察者,但我認為這些模式不適合我想要做的事情。我想知道是否有人知道我可以遵循的設(shè)計模式,或者知道一個實現(xiàn),可以讓我訪問已創(chuàng)建的用戶類之外的對象(用戶)(即已經(jīng)創(chuàng)建帳戶)并進行更改它。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?        }    }}
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

這不是您與數(shù)據(jù)庫交互的方式。編寫一個User類來存儲用戶名和密碼。然后編寫一個數(shù)據(jù)庫類來讀取文本文件并創(chuàng)建 User 對象的 ArrayList。然后使用 User.name 進行搜索,并在登錄前檢查 User.password 進行驗證。


數(shù)據(jù)庫會將文本文件中的數(shù)據(jù)“加載”到 ArrayList 中,這將是您的程序?qū)⑴c之交互的內(nèi)容。如果您想重寫文本文件,請編寫一個保存函數(shù)來清除并打印到相關(guān)文件。


這是一個數(shù)據(jù)庫類的示例(來自我的一個舊項目),它以以下格式讀取學生的數(shù)據(jù): name:HARSH|PID:12|major:null|minor:null|CGPA:4.100000|college:null|email:abcd@gmail.com


import java.util.*;

import java.text.*;

import java.time.*;

import java.io.*;


public class Database{

    public List<Student> studentList;

    private long lastModified;

    @SuppressWarnings("unchecked")


    public Object loadDb()

    {//Loads the database from file.

        File data = new File("database.db");

        if(lastModified == data.lastModified())

        {

            return studentList;//Returning main memory;

        }

        Scanner sc;

        try

        {

            sc = new Scanner(data).useDelimiter("\n");//To get individual lines.

            String line;

            if(studentList!=null)

            {//Clearing main memory.

                studentList.clear();

            }

            else

            {//Creating new main memory.

                studentList = new ArrayList<Student>();

            }


            while(sc.hasNext())

            {

                line = sc.next();

                Scanner l = new Scanner(line).useDelimiter("\\|");//To get info

                String name, PID, major, minor, cgpaSt, college, email;

                name = l.next().split(":")[1];

                PID = l.next().split(":")[1];

                major = l.next().split(":")[1];

                minor = l.next().split(":")[1];

                cgpaSt = l.next().split(":")[1];

                college = l.next().split(":")[1];

                email = l.next().split(":")[1];

                double CGPA = Double.valueOf(cgpaSt);

                Student stud = new Student(name,PID, major, minor, CGPA, college, email);//Creating new student with same info.

                studentList.add(stud);//Adding the student to memory.

            }

            sc.close();

        }

        catch(Exception e)

        {

            e.printStackTrace();

        }   

        return studentList;

    }

    @SuppressWarnings("unchecked")

    public void updateDb(Object studList)

    {//Updates the database.

        File data = new File("database.db");

        PrintWriter fs;

        try

        {

            fs = new PrintWriter(data);

        }

        catch(IOException e)

        {

            System.out.println("IO Exception!");

            return;

        }

        fs.flush();

        ArrayList<Student>studs = (ArrayList<Student>)studList;

        for(int i = 0;i<studs.size();i++)

        {

            fs.print(studs.get(i).toString());

            if(i != studs.size() -1)

            {

                fs.print("\n");

            }

        }

        fs.close();

        lastModified = data.lastModified();

        loadDb();//Loading updated database.

    }



}

您必須toString()為您的用戶類編寫一個函數(shù),并根據(jù)您的格式修改讀數(shù)。Database userData = new Database();這將允許您在中心類中創(chuàng)建一個變量。然后,您可以與studentList(在您的例子中為userList)交互,并搜索該用戶,然后檢查他們的密碼是否正確。


查看完整回答
反對 回復 2023-06-28
  • 1 回答
  • 0 關(guān)注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號