使用另一个类读取CSV文件,然后登录Java [英] Reading a CSV file using another class then login java

查看:121
本文介绍了使用另一个类读取CSV文件,然后登录Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个类中读取CSV文件,然后获取详细信息,并将其与主类中用户提供的登录信息进行比较.我可以通过将所有内容放在主类中来做到这一点,但是看起来并不整洁.

I am trying to read a CSV file from another class then take the details and compare them with the login information given by the user in the main class. I can do it by putting everything in the main class but it doesn't look very neat.

-主要-

package supscription.service;

/**
 *
 * @author jakec
 */
import java.util.*;
public class SubscriptionService {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    String username;
    String password;

    Scanner input = new Scanner(System.in);

    System.out.println("Log in:");
    System.out.println("username: ");
    username = input.nextLine();

    System.out.println("password: ");
    password = input.nextLine();

    users check = new users(username, password);

    if(check.auth()) 
        System.out.println("You are logged in");
    else
        System.out.println("Log in failed");
    }

}

-用户-

 public class users {

private String username;
private String password;
private  String File = ("MEMBER.csv");
Scanner scan = new Scanner (SubscriptionService.class.getResourceAsStream(File)).useDelimiter(",");
String user = scan.nextLine();
String pass = scan.nextLine();

public users(String user, String pass){
    username = user;
    password = pass;
}

public boolean auth(){
    if((username.equals (user)) && (password.equals (pass)))
        return true;
    else
        return false;
}

}

-CSV-

Member_ID,Username,Password
11001,Jack Brown,123
11009,James White,12
11014,Barbara Jones,58
...

推荐答案

您的方法是正确的.要搜索特定用户,您需要先阅读所有用户并将其存储在list中.您可以执行以下操作:

Your approch is correct. To search for a particular user, you need to first read all the users and store them in let's say a list. You can do the following:

  1. 创建一个user类以保存用户信息
  2. 创建一种读取所有用户的方法
  3. 创建一种搜索用户的方法
  1. Create a user class to hold user information
  2. Create a method to read all the users
  3. Create a method to search user

对于第1步,用户类可以具有ID,用户名和密码,例如:

For Step 1, the user class can have id, username and password , e,g.:

class User{

    private int id;
    private String username;
    private String password;

    public User(int id, String username, String password){
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

对于步骤2,您可以创建服务类并读取所有用户,例如:

For Step 2, you can create a service class and read all the users, e.g.:

class Users{

    private static List<User> users;

    public static synchronized void readUsers(){
        if(null == users){
            users = new ArrayList<User>();
            String File = ("MEMBER.csv");
            Scanner scan = new Scanner (Users.class.getResourceAsStream(File));
            String line;
            while((line = scan.nextLine()) != null){
                String[] tokens = line.split(",");
                users.add(new User(Integer.parseInt(tokens[0]), tokens[1], tokens[2]));
            }
            scan.close();
        }
    }
}

对于步骤3,您需要在服务类中添加另一个方法,该方法将搜索列表并返回truefalse,例如:

For Step 3, you need to add another method in service class which will search into the list and return true or false, e.g.:

public static synchronized boolean find(String username, String password){
    if(null == users){
        throw new IllegalStateException("user list is not initialised");
    }

    return users.stream()
            .filter(u -> u.getUsername().equals(username))
            .filter(u -> u.getPassword().equals(password))
            .findFirst()
            .isPresent();
}

这将根据列表中是否存在User返回truefalse.

This would return true or false based on whether User exists in the list.

在您的main类中,您可以使用以下流程:

In your main class, you can use the following flow:

  • 初始化用户列表
  • 通过命令行读取用户名和密码
  • 调用find方法并输出适当的输出
  • Initialise User list
  • Read username and password via command line
  • Call find method and print appropriate output

请注意,Users类中的所有方法都是static,因此您无需创建对象.您可以直接调用方法,例如Users.readUsers();

Please note that all the methods in Users class are static so you don't need to create an object. You can call the methods directly, e.g. Users.readUsers();

这篇关于使用另一个类读取CSV文件,然后登录Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆