ADD::added comments to most classes to make it more understandable

This commit is contained in:
Sem van der Hoeven
2019-03-18 17:07:38 +01:00
parent ecdc9f3a4f
commit b8f4415825
11 changed files with 122 additions and 9 deletions

View File

@@ -12,11 +12,15 @@ import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
//springbootApplication is so Spring knows that this is a Spring application
@SpringBootApplication
public class Application extends javafx.application.Application {
//configurable application is for spring so it knows that it can use it
private static ConfigurableApplicationContext springContext;
//logger to log all the things that happen to the console
private static final Logger log = LoggerFactory.getLogger(Application.class);
//launch is to launch the GUI things
public static void main(String[] args) {
launch(args);
}
@@ -25,8 +29,10 @@ public class Application extends javafx.application.Application {
* This method takes an url and return a parent.
* @param url which is being loaded.
* @return parent object.
* @throws IOException if it can't find an FXML file
*/
public static Parent load(java.net.URL url) throws IOException {
//loader to load the FXML file
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(springContext::getBean);
loader.setLocation(url);
@@ -35,16 +41,22 @@ public class Application extends javafx.application.Application {
@Override
public void init() throws Exception {
//run the application
springContext = SpringApplication.run(Application.class);
}
@Override
public void start(Stage primaryStage) throws Exception {
//load the fxml file
Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/sample.fxml"));
//set the title for the window
primaryStage.setTitle("Greenify");
//set the scene
Scene scene = new Scene(rootNode);
//add the stylesheet
scene.getStylesheets()
.add(getClass().getClassLoader().getResource("stylesheets/LoginWindowStyle.css").toExternalForm());
.add(getClass().getClassLoader().getResource("stylesheets/LoginWindowStyle.css")
.toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}

View File

@@ -11,6 +11,9 @@ import javafx.util.Duration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//Class that controls the dashboard fxml file (the GUI Screen)
@Controller
public class DashBoardController {
@Autowired
@@ -36,12 +39,19 @@ public class DashBoardController {
@FXML
private Label welcomebacktext;
FadeTransition fadeTrans;
FadeTransition fadeTrans; //transition for switching between the different panels
/**
* loads the 'welcome back' text before anything else.
*/
public void initialize() {
welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!");
}
/**
* adds a fade transition for switching between the different panes.
* @param node the node on which the transition needs to act
*/
public void addFadeTransition(Node node) {
fadeTrans = new FadeTransition(Duration.millis(400), node);

View File

@@ -11,7 +11,7 @@ import javafx.stage.Stage;
import javafx.stage.Window;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//class that controls the actions for the register window
@Controller
public class RegisterWindowController {

View File

@@ -17,6 +17,7 @@ import org.springframework.stereotype.Controller;
import java.io.IOException;
//class that controls the actions for the login screen
@Controller
public class UserController {
@Autowired
@@ -34,12 +35,22 @@ public class UserController {
@FXML
private Button signupButton;
/**
* handles when the user clicks on the login button.
* it checks if the username and password fields are filled
* and gives alerts if they aren't filled in.
* @param event the click of the login button
* @throws IOException an exception for logging in the user
*/
@FXML
protected void handleLoginButtonAction(ActionEvent event) throws IOException {
Window owner = loginButton.getScene().getWindow();
Window owner = loginButton.getScene().getWindow(); //get the current window
if (usernameField.getText().isEmpty()) {
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Log-in Error!",
"Please enter your username");
//checks if the username field is filled,
// and gives an alert if it is not
return;
} else {
System.out.println("Username is " + usernameField.getText());
@@ -47,13 +58,18 @@ public class UserController {
if (passwordField.getText().isEmpty()) {
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Log-in Error!",
"Please enter a password");
//checks if the password field id filled,
// and gives an alert if it is not.
return;
} else {
System.out.println("Password is " + passwordField.getText());
}
//log the user in with the userService method
userService.loginUser(usernameField.getText(), passwordField.getText());
Stage current = (Stage) owner;
//after logging in, close the login window
current.close();
//open the other dashboard window
openDashboard();
}
@@ -64,17 +80,21 @@ public class UserController {
* @author sem
*/
public void openDashboard() throws IOException {
//load the fxml file
Parent dash = Application.load(this.getClass().getClassLoader()
.getResource("fxml/dashboard.fxml"));
Scene scene = new Scene(dash);
//add the stylesheet for the CSS
scene.getStylesheets().add(getClass().getClassLoader()
.getResource("stylesheets/dashboardStyle.css").toExternalForm());
Stage appStage = new Stage();
appStage.setScene(scene);
//set the title
appStage.setTitle("Greenify - " + usernameField.getText());
appStage.show();
}
//class for showing the alerts
public static class AlertHelper {
/**
* alerts for the login screen.
@@ -99,7 +119,7 @@ public class UserController {
/**
* The method handles register button.
* @param event User clicks to the button
* @throws Exception when the file couldn't find
* @throws Exception when the file couldn't be found
*/
public void handleRegisterButtonAction(ActionEvent event) throws Exception {
Parent registerWindow = Application.load(this.getClass().getClassLoader()

View File

@@ -31,14 +31,26 @@ public class UserService {
* @param password the password of the user
* @return a userDTO
*/
@SuppressWarnings("Duplicates")
//this suppressWarnings is to get rid of the errors of duplicate code
//because the methods are very similar
public UserDto registerUser(String name, String password) {
//headers for http
HttpHeaders headers = new HttpHeaders();
//set the accept header in JSÖN value
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
//connect to the server with the needed url
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/registerUser")
.queryParam("name", name)
//getting the name from the database
.queryParam("password", password);
//getting the password from the database
//create a http entity to be sent
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
//the result to be sent is a userDto
UserDto result = this.restTemplate.getForObject(builder.build()
.encode().toUri(), UserDto.class);
this.currentUser = result;
@@ -51,7 +63,9 @@ public class UserService {
* @param password the password of the user
* @return a userDTO
*/
@SuppressWarnings("Duplicates")
public UserDto loginUser(String name, String password) {
//this method is almost the same as the registerUser one, but with a different link
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser")
@@ -71,7 +85,9 @@ public class UserService {
* @param name the username of the user
* @return a userDTO
*/
@SuppressWarnings("Duplicates")
public UserDto addVeganMeal(Long id, String name) {
//this method is almost the same as the registerUser one, but with a different link
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addVeganMeal")

View File

@@ -14,6 +14,7 @@ import org.springframework.web.client.RestTemplate;
@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {
//logger that logs everything to the console
private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
@Mock
@@ -25,6 +26,7 @@ public class UserServiceTest {
@Test
public void userRegisterTest() throws Exception {
//tests if registering works
UserDto testUser = new UserDto(1L, "Eric", 0);
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
UserDto.class))
@@ -36,6 +38,7 @@ public class UserServiceTest {
@Test
public void userLoginTest() throws Exception {
//tests if logging in works
UserDto testUser = new UserDto(1L, "Eric", 0);
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
UserDto.class))
@@ -46,6 +49,7 @@ public class UserServiceTest {
@Test
public void addVeganMealTest() throws Exception {
//tests if adding a vegetarian meal works
UserDto testUser = new UserDto(1L, "Eric", 0);
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"),
UserDto.class))