Map<String, String> d = new HashMap<>();
void input(String u, String p, String e) {
read();
if (e.equals("login")) login(u, p);
else if (e.equals("register")) register(u, p);
write();
}
void read() {
d = new HashMap<>();
String s = "";
try {
s = new String(Files.readAllBytes(Paths.get("data.txt")));
}catch(IOException e) {
e.printStackTrace();
}
String [] pairs = s.split("\n");
for (int i = 0; i < pairs.length; i++) {
d.put(pairs[i].split(",")[0], pairs[i].split(",")[1]);
}
}
void write() {
try (FileWriter m = new FileWriter("data.txt")) {
for (Map.Entry<String, String> entry : d.entrySet()) {
m.write(entry.getKey() + "," + entry.getValue() + "\n");
}
m.close();
}catch (IOException e) {
e.printStackTrace();
}
}
boolean login(String u, String p) {
return (d.get(u).equals(p)) ? true : false;
}
boolean register(String u, String p) {
if (d.containsKey(u)) return false;
d.put(u, p);
return true;
}