Last active
September 14, 2018 15:15
-
-
Save must1/ab36f8fe3e7dcba078099b059015a379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import repository.UserActions; | |
import java.sql.SQLException; | |
public class Login { | |
private User connectedUser; | |
private UserActions userActions; | |
public Login(UserActions userActions) { | |
this.userActions = userActions; | |
} | |
public boolean signIn(String username, String password) throws SQLException { | |
connectedUser = new User(username, password); | |
if (!userActions.signIn(connectedUser)) { | |
return false; | |
} | |
connectedUser.setID(retrieveConnectedUserID(connectedUser)); | |
return true; | |
} | |
private int retrieveConnectedUserID(User connectedUser) throws SQLException { | |
return userActions.retrieveUserID(connectedUser); | |
} | |
public User getConnectedUser() { | |
return this.connectedUser; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import controller.ToDoController; | |
import model.Login; | |
import model.Registration; | |
import model.Tasker; | |
import repository.TaskActions; | |
import repository.TaskRepository; | |
import repository.UserRepository; | |
import view.ToDoView; | |
import java.sql.SQLException; | |
public class Main { | |
public static void main(String[] args) throws SQLException { | |
TaskRepository taskRepository = new TaskRepository(); | |
UserRepository userRepository = new UserRepository(); | |
ToDoController toDoController = new ToDoController(new Tasker(taskRepository), new Registration(userRepository), new Login(userRepository), new ToDoView()); | |
toDoController.startApplication(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import repository.UserActions; | |
import java.sql.SQLException; | |
public class Registration { | |
private UserActions userActions; | |
public Registration(UserActions userActions) { | |
this.userActions = userActions; | |
} | |
public boolean createUser(String username, String password) throws SQLException { | |
return userActions.createUser(new User(username, password)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import repository.TaskActions; | |
import java.sql.SQLException; | |
import java.util.List; | |
public class Tasker { | |
private TaskActions taskActions; | |
public Tasker(TaskActions taskActions) { | |
this.taskActions = taskActions; | |
} | |
public void addTask(String taskName, User connectedUser) throws SQLException { | |
taskActions.addTask(new Task(taskName), connectedUser); | |
} | |
public void deleteTask(String taskName, User connectedUser) throws SQLException { | |
taskActions.deleteTask(new Task(taskName), connectedUser); | |
} | |
public List<Task> getTasks(User connectedUser) throws SQLException { | |
return taskActions.getTasks(connectedUser); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controller; | |
import model.*; | |
import repository.TaskActions; | |
import repository.UserActions; | |
import view.ToDoView; | |
import java.sql.SQLException; | |
import java.util.InputMismatchException; | |
import java.util.List; | |
import java.util.Scanner; | |
public class ToDoController { | |
private static final int LOGIN = 1; | |
private static final int REGISTER = 2; | |
private ToDoView toDoView; | |
private Scanner input; | |
private int option; | |
private Login modelOfLogin; | |
private Registration modelOfRegistration; | |
private Tasker modelOfTasker; | |
public ToDoController(Tasker tasker, Registration registration, Login login, ToDoView toDoView) { | |
this.toDoView = toDoView; | |
input = new Scanner(System.in); | |
this.modelOfLogin = login; | |
this.modelOfTasker = tasker; | |
this.modelOfRegistration = registration; | |
} | |
public void startApplication() { | |
toDoView.executeMainMenu(); | |
boolean isInvalid = true; | |
while (isInvalid) { | |
try { | |
option = input.nextInt(); | |
if (option == 1 || option == 2) { | |
isInvalid = false; | |
switch (option) { | |
case LOGIN: | |
executeLoginCase(); | |
break; | |
case REGISTER: | |
executeRegistrationCase(); | |
break; | |
} | |
} | |
} catch (InputMismatchException e) { | |
toDoView.showError(); | |
input.next(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private void executeUserOptions(int option) throws SQLException { | |
switch (option) { | |
case 1: | |
toDoView.showInputingTaskMessage(); | |
String taskName = input.next(); | |
modelOfTasker.addTask(taskName, modelOfLogin.getConnectedUser()); | |
break; | |
case 2: | |
toDoView.showInputingTaskMessage(); | |
taskName = input.next(); | |
modelOfTasker.deleteTask(taskName, modelOfLogin.getConnectedUser()); | |
break; | |
case 3: | |
List<Task> tasks = modelOfTasker.getTasks(modelOfLogin.getConnectedUser()); | |
for (Task task : tasks) | |
toDoView.showMessage(String.valueOf(task)); | |
break; | |
} | |
} | |
private void executeUserCase() throws SQLException { | |
do { | |
toDoView.showTaskMenu(); | |
option = input.nextInt(); | |
executeUserOptions(option); | |
} | |
while (option != 4); | |
} | |
private void executeRegistrationCase() throws SQLException { | |
String username, password; | |
toDoView.showInputingUserAndPasswordMessage(); | |
toDoView.showMessage("User: "); | |
username = input.next(); | |
toDoView.showMessage("Password: "); | |
password = input.next(); | |
if (modelOfRegistration.createUser(username, password)) | |
toDoView.showMessage("User created successfully, now you can sign in!"); | |
} | |
private void executeLoginCase() throws SQLException { | |
String username, password; | |
boolean isInvalid = true; | |
do { | |
toDoView.showInputingUserAndPasswordMessage(); | |
toDoView.showMessage("User: "); | |
username = input.next(); | |
toDoView.showMessage("Password: "); | |
password = input.next(); | |
if (modelOfLogin.signIn(username, password)) { | |
toDoView.showMessage("You've logged in"); | |
executeUserCase(); | |
isInvalid = false; | |
} else | |
toDoView.showError(); | |
} | |
while (isInvalid); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package view; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class ToDoView { | |
private Logger logger; | |
public ToDoView() { | |
logger = LoggerFactory.getLogger(ToDoView.class); | |
} | |
public void executeMainMenu() { | |
logger.info("--MAIN MENU--"); | |
logger.info("1. Sign in"); | |
logger.info("2. Sign up"); | |
} | |
public void showError() { | |
logger.info("Try again"); | |
} | |
public void showMessage(String message) { | |
logger.info(message); | |
} | |
public void showTaskMenu() { | |
logger.info("1. Add task"); | |
logger.info("2. Remove task"); | |
logger.info("3. Get all tasks"); | |
logger.info("4. Quit"); | |
} | |
public void showInputingUserAndPasswordMessage() { | |
logger.info("Put your username and password"); | |
} | |
public void showInputingTaskMessage() { | |
logger.info("Input task"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment