I'm attempting to make a login system by using a model and DAO class to write users to a file called users.txt
In my controller, I created an initialize
method:
public void initialize(){
User user = new User();
user.addUser();
}
Here's the part of my model that contains the addUser()
method:
public void addUser() {
User newUser = new User();
UserDAO uDAO = new UserDAO();
uDAO.createUser(newUser);
}
And this is my DAO class(I can't just paste the createUser()
method as it links to several other methods in the class):
package Server;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Scanner;
public class UserDAO implements UserDAOInterface {
private static final String User_File = "users.txt";
private File dataFile;
public UserDAO() {
Path dPath = FileSystems.getDefault().getPath("src/MainMenu",User_File);
dataFile = new File(dPath.toString());
}
public ArrayList<User> getAllUsers() {
Scanner sc;
String record = null;
String[] fields;
ArrayList<User> users = new ArrayList<User>();
try {
sc = new Scanner(dataFile);
while (sc.hasNextLine()) {
record = sc.nextLine();
fields = record.split(";");
String username = fields[0];
String password = fields[1];
User u = new User();
users.add(u);
}
} catch (FileNotFoundException e) {
System.out.println("No record found!");
//e.printStackTrace();
}
return users;
}
public User getUser(String username) {
ArrayList<User> users = getAllUsers();
User user = null;
for (User u: users) {
if (u.getUsername().equals(username)) {
user = u;
break;
}
}
return user;
}
public boolean createUser(User user) {
boolean existing = false;
ArrayList<User> users = getAllUsers();
for (User u: users) {
if (u.getUsername().equals(user.getUsername())) {
existing = true;
break;
}
}
if (!existing) {
users.add(user);
synToFile(users);
}
return !existing;
}
public void updateUser(User user) {
ArrayList<User> users = getAllUsers();
for (int i = 0; i < users.size(); i++) {
User u = users.get(i);
if (u.getUsername().equals(user.getUsername())){
users.set(i, user);
}
}
synToFile(users);
}
public void deleteUser(User user) {
ArrayList<User> users = getAllUsers();
User delUser = null;
for (User u: users) {
if (user.getUsername().equals(u.getUsername())){
delUser = u;
break;
}
}
if (delUser != null) {
users.remove(delUser);
synToFile(users);
}
}
public void synToFile(ArrayList<User> userList) {
if (userList == null) {
return;
}
try {
FileWriter out = new FileWriter(dataFile);
for (User u: userList) {
out.append(u.toString() + "\n");
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
I've imported everything I needed to import, and all paths are correct. I've tried deleting the text file and running the application to see if the path works, and it does manage to create the text file, but it doesn't update the file by writing anything to it.
I suspect the issue might have something to do with not creating any password for the user, and since there isn't a password field it can't write anything; but I can't figure out a way around this.
Also, nothing else in the model is important; just some attributes, accessors and mutators, and a default constructor that doesn't contain anything.
Update: Added the User class here for reference.
package Server;
public class User {
private String username;
private String password;
public User() {
}
public void addUser() {
User newUser = new User();
UserDAO uDAO = new UserDAO();
uDAO.createUser(newUser);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}