import java.util.Base64; /** * A class representing a user in the system. *

* A user has an id, a type, a name, and a password. * The id is a unique identifier (string) for the user. * The type can be either ADMIN or PUBLIC. * The name and password are stored as base64-encoded strings. * * @author Yuri Tatishchev * @version 0.1 2025-02-24 */ public class User { public enum Type { ADMIN, PUBLIC, } private String id; private Type type; private String name; private String password; public User(String id, Type type, String name, String password) { this.id = id; this.type = type; this.name = name; this.password = password; } /** * Parse a User object from a CSV string. * * @param line The CSV string, should be in the format: * id,type,base64(name),base64(password). * with escaped backslashes and commas. * @return a User object */ public static User fromCsvString(String line) { String[] parts = line.split(","); String id = parts[0]; Type type = Type.valueOf(parts[1]); String name = new String(Base64.getDecoder().decode(parts[2])); String password = new String(Base64.getDecoder().decode(parts[3])); return new User(id, type, name, password); } /** * Get a CSV formatted string from the user object. * * @return a CSV formatted string in the format: * id,type,base64(name),base64(password). */ public String toCsvString() { // don't forget to escape commas in name and password String csvName = Base64.getEncoder().encodeToString(name.getBytes()); String csvPassword = Base64.getEncoder().encodeToString(password.getBytes()); return String.join(",", id, type.toString(), csvName, csvPassword); } public Type getType() { return type; } public String getId() { return id; } public String getName() { return name; } public String getPassword() { return password; } /** * Check if the given password matches the user's password. * * @param password the inputted password to check * @return true if the password matches, false otherwise */ public boolean checkPassword(String password) { return this.password.equals(password); } /** * Check if the user is an admin. * * @return true if the user is an admin, false otherwise */ public boolean isAdmin() { return type == Type.ADMIN; } }