Guest User

Untitled

a guest
Feb 9th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. public class Main {
  2. public static void main(String[] args) {
  3. ToDoEngine engine = new ToDoEngine();
  4. engine.displayMainMenu();
  5. engine.displayUserMenu();
  6. }
  7. }
  8.  
  9. import java.util.HashMap;
  10. import java.util.Scanner;
  11.  
  12. public class AccountMaker {
  13.  
  14. private HashMap<String, User> loginDetails = new HashMap<>();
  15. private String login, password;
  16. private Scanner input = new Scanner(System.in);
  17.  
  18.  
  19. void inputLoginAndPassword() {
  20. System.out.println("Input your login");
  21. login = input.next();
  22.  
  23. System.out.println("Input your password");
  24. password = input.next();
  25. }
  26.  
  27. void createAccount() throws InterruptedException {
  28. User newUser = new User(login, password);
  29. loginDetails.put(login, newUser);
  30. System.out.println("Account has been created successfully");
  31. Thread.sleep(2000);
  32. }
  33.  
  34. HashMap<String, User> getLoginDetails() {
  35. return loginDetails;
  36. }
  37.  
  38. }
  39.  
  40. import java.util.Scanner;
  41.  
  42. public class AccountLogger {
  43.  
  44. private AccountMaker accountMaker;
  45. private Scanner input = new Scanner(System.in);
  46. private String login, password;
  47.  
  48.  
  49. AccountLogger(AccountMaker accountMaker) {
  50. this.accountMaker = accountMaker;
  51. }
  52.  
  53.  
  54. void inputLoginAndPassword() {
  55. System.out.println("Input your login");
  56. login = input.next();
  57.  
  58. System.out.println("Input your password");
  59. password = input.next();
  60. }
  61.  
  62. boolean isLoginDataIncorrect() {
  63. User user = accountMaker.getLoginDetails().get(login);
  64. try {
  65. if (user.getPassword().equals(password)) {
  66. System.out.println("You've logged in.");
  67. return false;
  68. } else {
  69. System.out.println("Bad login or password");
  70. }
  71. } catch (NullPointerException e) {
  72. System.out.println("Bad login or password");
  73. }
  74. return true;
  75. }
  76. }
  77.  
  78. public class User {
  79. private Tasker tasker;
  80. private String login;
  81. private String password;
  82.  
  83. User(String login, String password) {
  84. this.login = login;
  85. this.password = password;
  86. }
  87.  
  88. User(Tasker tasks) {
  89. this.tasker = tasks;
  90. }
  91.  
  92. void addNewTask(String task) {
  93. tasker.createTask(task);
  94. }
  95.  
  96. void deleteTask(String task) {
  97. tasker.deleteTask(task);
  98. }
  99.  
  100. void showAllTasks() {
  101. tasker.showAllTasks();
  102. }
  103.  
  104. String getPassword() {
  105. return password;
  106. }
  107.  
  108. }
  109.  
  110. import java.util.ArrayList;
  111. import java.util.List;
  112.  
  113. public class Tasker {
  114.  
  115. private List<String> listOfTasks = new ArrayList<>();
  116.  
  117.  
  118. void createTask(String task) {
  119. listOfTasks.add(task);
  120. }
  121.  
  122. void deleteTask(String task) {
  123. listOfTasks.remove(task);
  124. }
  125.  
  126. void showAllTasks() {
  127. for (String listOfTask : listOfTasks) {
  128. System.out.println(listOfTask);
  129. }
  130. }
  131. }
  132.  
  133. import java.util.InputMismatchException;
  134. import java.util.Scanner;
  135.  
  136. public class ToDoEngine {
  137.  
  138. private Scanner input = new Scanner(System.in);
  139. private AccountMaker accountMaker = new AccountMaker();
  140. private AccountLogger accountLogger = new AccountLogger(accountMaker);
  141. private boolean loopIsTrue = true;
  142. private Tasker tasks = new Tasker();
  143. private User user = new User(tasks);
  144.  
  145. void displayMainMenu() {
  146. System.out.println("What do you wanna to do?");
  147. System.out.println("1. Add Account 2. Log into my account");
  148.  
  149. while (loopIsTrue) {
  150. try {
  151. getOptionsOfMainMenu(input.nextInt());
  152. } catch (InputMismatchException e) {
  153. System.out.println("You've inputed something wrong!");
  154. System.out.println("What do you wanna to do?");
  155. System.out.println("1. Add Account 2. Log into my account");
  156. input.next();
  157. } catch (InterruptedException e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162.  
  163. private void getOptionsOfMainMenu(int option) throws InterruptedException {
  164. if (option >= 1 && option <= 2) {
  165. switch (option) {
  166. case 1:
  167. accountMaker.inputLoginAndPassword();
  168. accountMaker.createAccount();
  169. System.out.println("Now let's log into you account.");
  170. case 2:
  171. while (loopIsTrue) {
  172. accountLogger.inputLoginAndPassword();
  173. if (!accountLogger.isLoginDataIncorrect()) {
  174. loopIsTrue = false;
  175. }
  176. }
  177. break;
  178. }
  179. }
  180. }
  181.  
  182. public void displayUserMenu() {
  183. loopIsTrue = true;
  184. while (loopIsTrue) {
  185. System.out.println("What do you wanna to do?");
  186. System.out.println("1. Add task 2. Show my tasks 3. Delete task 4. Exit");
  187. getOptionsOfUserMenu(input.nextInt());
  188. }
  189. }
  190.  
  191. private void getOptionsOfUserMenu(int option) {
  192. if (option >= 1 && option <= 4) {
  193. switch (option) {
  194. case 1:
  195. System.out.println("Write down your task.");
  196. input.nextLine();
  197. user.addNewTask(input.nextLine());
  198. System.out.println("Task was added.");
  199. break;
  200. case 2:
  201. System.out.println("----------------");
  202. System.out.println("YOUR TASK LIST:");
  203. user.showAllTasks();
  204. System.out.println("----------------");
  205. break;
  206. case 3:
  207. System.out.println("Write down your task that you want to delete from task list.");
  208. input.nextLine();
  209. user.deleteTask(input.nextLine());
  210. System.out.println("Task was deleted");
  211. loopIsTrue = false;
  212. break;
  213. case 4:
  214. loopIsTrue = false;
  215. break;
  216. }
  217. }
  218. }
  219. }
Add Comment
Please, Sign In to add comment