问题
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.
--Main--
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");
}
}
--users--
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;
}
}
--The CSV--
Member_ID,Username,Password
11001,Jack Brown,123
11009,James White,12
11014,Barbara Jones,58
...
回答1:
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:
- Create a
userclass to hold user information - Create a method to read all the users
- Create a method to search user
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;
}
}
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();
}
}
}
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();
}
This would return true or false based on whether User exists in the list.
In your main class, you can use the following flow:
- Initialise User list
- Read username and password via command line
- Call
findmethod and print appropriate output
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();
来源:https://stackoverflow.com/questions/42190602/reading-a-csv-file-using-another-class-then-login-java