I've written simple ToDo Console Application with option to create account and log into.
Actually, this option is unnecessary, because you have to create an account, but I don't know how to have the code interact with a DataBase yet. I wanted to be as much OOP as it can be. Thanks for all suggestions.
Main class
public class Main {
public static void main(String[] args) {
ToDoEngine engine = new ToDoEngine();
engine.displayMainMenu();
engine.displayUserMenu();
}
}
AccountMaker
import java.util.HashMap;
import java.util.Scanner;
public class AccountMaker {
private HashMap<String, User> loginDetails = new HashMap<>();
private String login, password;
private Scanner input = new Scanner(System.in);
void inputLoginAndPassword() {
System.out.println("Input your login");
login = input.next();
System.out.println("Input your password");
password = input.next();
}
void createAccount() throws InterruptedException {
User newUser = new User(login, password);
loginDetails.put(login, newUser);
System.out.println("Account has been created successfully");
Thread.sleep(2000);
}
HashMap<String, User> getLoginDetails() {
return loginDetails;
}
}
AccountLogger
import java.util.Scanner;
public class AccountLogger {
private AccountMaker accountMaker;
private Scanner input = new Scanner(System.in);
private String login, password;
AccountLogger(AccountMaker accountMaker) {
this.accountMaker = accountMaker;
}
void inputLoginAndPassword() {
System.out.println("Input your login");
login = input.next();
System.out.println("Input your password");
password = input.next();
}
boolean isLoginDataIncorrect() {
User user = accountMaker.getLoginDetails().get(login);
try {
if (user.getPassword().equals(password)) {
System.out.println("You've logged in.");
return false;
} else {
System.out.println("Bad login or password");
}
} catch (NullPointerException e) {
System.out.println("Bad login or password");
}
return true;
}
}
User
public class User {
private Tasker tasker;
private String login;
private String password;
User(String login, String password) {
this.login = login;
this.password = password;
}
User(Tasker tasks) {
this.tasker = tasks;
}
void addNewTask(String task) {
tasker.createTask(task);
}
void deleteTask(String task) {
tasker.deleteTask(task);
}
void showAllTasks() {
tasker.showAllTasks();
}
String getPassword() {
return password;
}
}
Tasker
import java.util.ArrayList;
import java.util.List;
public class Tasker {
private List<String> listOfTasks = new ArrayList<>();
void createTask(String task) {
listOfTasks.add(task);
}
void deleteTask(String task) {
listOfTasks.remove(task);
}
void showAllTasks() {
for (String listOfTask : listOfTasks) {
System.out.println(listOfTask);
}
}
}
ToDoEngine
import java.util.InputMismatchException;
import java.util.Scanner;
public class ToDoEngine {
private Scanner input = new Scanner(System.in);
private AccountMaker accountMaker = new AccountMaker();
private AccountLogger accountLogger = new AccountLogger(accountMaker);
private boolean loopIsTrue = true;
private Tasker tasks = new Tasker();
private User user = new User(tasks);
void displayMainMenu() {
System.out.println("What do you wanna to do?");
System.out.println("1. Add Account 2. Log into my account");
while (loopIsTrue) {
try {
getOptionsOfMainMenu(input.nextInt());
} catch (InputMismatchException e) {
System.out.println("You've inputed something wrong!");
System.out.println("What do you wanna to do?");
System.out.println("1. Add Account 2. Log into my account");
input.next();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void getOptionsOfMainMenu(int option) throws InterruptedException {
if (option >= 1 && option <= 2) {
switch (option) {
case 1:
accountMaker.inputLoginAndPassword();
accountMaker.createAccount();
System.out.println("Now let's log into you account.");
case 2:
while (loopIsTrue) {
accountLogger.inputLoginAndPassword();
if (!accountLogger.isLoginDataIncorrect()) {
loopIsTrue = false;
}
}
break;
}
}
}
public void displayUserMenu() {
loopIsTrue = true;
while (loopIsTrue) {
System.out.println("What do you wanna to do?");
System.out.println("1. Add task 2. Show my tasks 3. Delete task 4. Exit");
getOptionsOfUserMenu(input.nextInt());
}
}
private void getOptionsOfUserMenu(int option) {
if (option >= 1 && option <= 4) {
switch (option) {
case 1:
System.out.println("Write down your task.");
input.nextLine();
user.addNewTask(input.nextLine());
System.out.println("Task was added.");
break;
case 2:
System.out.println("----------------");
System.out.println("YOUR TASK LIST:");
user.showAllTasks();
System.out.println("----------------");
break;
case 3:
System.out.println("Write down your task that you want to delete from task list.");
input.nextLine();
user.deleteTask(input.nextLine());
System.out.println("Task was deleted");
loopIsTrue = false;
break;
case 4:
loopIsTrue = false;
break;
}
}
}
}
Here is github link to repository that contains these classes.