Skip to content

Commit

Permalink
Java FX #16 Created default properties file & empty text input alert
Browse files Browse the repository at this point in the history
  • Loading branch information
seregamazur committed May 20, 2018
1 parent 0b6c6fd commit 7cc938c
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ deploy:
provider: releases
api_key:
secure: "fg7KBZZZBmncn1oRwwT79UOPamOwk4KjPWehLrERZd4VHwFBjht3kq5hQ1pp1OEtHtCI/QZJ4CRpoI5c5xzYKRc42qfPcKCXiuiuOeRf0XRy5A9iAFAi5uc+WJS778uLxj+3OaCBHYgiCdZ4ddnAwu8VlNnbsU3XJMJ2CvAOAr3SUm96N5M/fy1pBkkTgYBtmcEd6QHfesnKnsHUtbqXqZZQZh62YN/2ATMyvr67Ulu5Cq5P3ZvZxlBeb89CgYInwn3KnuwygDmQZKJM7Hi3Sf9ITwXqJ0LvCV7eaplYICeX6LdFGYwgtwk3pHd9tmthHQ22+2DxUmWCQXI5kTS9+0sNR3+Rrg7n73cti2NaQLu5ZOwv+KmHlO9+3MxDJk9MBjNMfZq5/dd5vHfHLMTwftiordhiN7lnZ+/3b8CT0odulJ0sGh5CqcfkmmHcyukUm7pUKBxH1HbisjwTYQk8rd2hs0Uooaobr3759GxiR26Bk4OzXvSNW4wCM3INQIJAgbH9UbpPN7ubtxm93mWYBgM2cik8otXr4aZmLsjqNffo/qih576LtfsiWWv+hxtAup4cKU777Z3m0v25Tws14pm4QSKE1X6A4H3ax8DxSRMAk5/4TRSMr8GG3eSKNhE5ohhK4deJUSeN8q6zK/6+xkzcOrjwuFffkv1VjKho1sE="
file: 'build/libs/BullsAndCows-5.0.1.jar'
file: 'build/libs/BullsAndCows-5.0.2.jar'
skip_cleanup: true
on:
all_branches: true
Expand Down
2 changes: 0 additions & 2 deletions settings.properties

This file was deleted.

3 changes: 2 additions & 1 deletion src/java/controller/ControllerAgainstComp.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private void input() {
if (textfield.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Вами не було введено число.", ButtonType.OK);
alert.showAndWait();
return;
} else {
if (guessing.getUsageNumbers().contains(Integer.parseInt(textfield.getText()))) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Ви вже вводили дане число.", ButtonType.OK);
Expand All @@ -73,7 +74,7 @@ private void input() {
}
}
if (Integer.toString(InputGetter.getInputNumb()).length() != gen.getDigits() || !CheckerNumber.hasNoDupes(InputGetter.getInputNumb())) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Ви задали число неправильно формату.\nКількість цифр в числі правильного формату: " + gen.getDigits(), ButtonType.OK);
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Потрiбно ввести число без повторiв цифр у ньому \n та правильної кiлькостi.\nКількість цифр у числі правильного формату: " + gen.getDigits(), ButtonType.OK);
alert.showAndWait();
} else {
guessing.check(gen);
Expand Down
35 changes: 28 additions & 7 deletions src/java/controller/ControllerSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
import java.io.*;
import java.util.Objects;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ControllerSettings {
private static final String DEFAULT_PROPERTY = "4";
private static Properties props = new Properties();
public static final String NUMBCOUNT = "DigitsCount";
public static final String FILE_PROPERTIES = "settings.properties";


@FXML
private javafx.scene.control.Button cancelbutton;
Expand All @@ -23,10 +30,8 @@ private void cancelSave() {

public void writeUserSettings(ChoiceBox<String> choiceBox) {
try {
String numbCount = "DigitsCount";
Properties props = new Properties();
props.setProperty(numbCount, Objects.requireNonNull(choiceBox.getSelectionModel().getSelectedItem()));
File f = new File("settings.properties");
props.setProperty(NUMBCOUNT, Objects.requireNonNull(choiceBox.getSelectionModel().getSelectedItem()));
File f = new File(FILE_PROPERTIES);
OutputStream out = new FileOutputStream(f);
props.store(out, null);
out.close();
Expand All @@ -36,16 +41,32 @@ public void writeUserSettings(ChoiceBox<String> choiceBox) {
}

public void readUserSettings(ChoiceBox<String> choiceBox) {
Properties props = new Properties();
InputStream input;
try {
File f = new File("settings.properties");
File f = new File(FILE_PROPERTIES);
if (!f.exists()) {
defaultFileProperties();
}
input = new FileInputStream(f);
props.load(input);
choiceBox.getSelectionModel().select(props.getProperty("DigitsCount"));
choiceBox.getSelectionModel().select(props.getProperty(NUMBCOUNT));
} catch (IOException ex) {
java.util.logging.Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}

public static void defaultFileProperties() {
try {
File f = new File(FILE_PROPERTIES);
if(f.createNewFile()){
props.setProperty(NUMBCOUNT, DEFAULT_PROPERTY);
OutputStream out = new FileOutputStream(f);
props.store(out, null);
out.close();
} else Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, "cannot create file property");
} catch (IOException ex) {
Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}

}
8 changes: 4 additions & 4 deletions src/java/controller/ControllerWithComp.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ private void backMenu() throws Exception {
@FXML
private void startGame() {
gen.read();

if (textfield.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Вами не було введено число.", ButtonType.OK);
alert.showAndWait();
return;
} else {
InputGetter.setInputNumber(Integer.parseInt(textfield.getText()));
}
if (Integer.toString(InputGetter.getInputNumber()).length() != gen.getDigits()
if ((Integer.toString(InputGetter.getInputNumber()).length() != gen.getDigits())
|| (!CheckerNumber.hasNoDupes(InputGetter.getInputNumber()))) {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Ви задали число неправильно формату.\nКількість цифр в числі правильного формату: " + gen.getDigits(), ButtonType.OK);
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Число потрiбно загадати без повторiв у ньому \n та правильної кiлькостi.\nКількість цифр у числі правильного формату: " + gen.getDigits(), ButtonType.OK);
alert.showAndWait();
} else {
while (computerGenerator.getGenerateStatus() != FINISHED) {
Expand All @@ -91,7 +91,7 @@ private void startGame() {
new PropertyValueFactory<>("ourBulls"));
cowcolumn.setCellValueFactory(
new PropertyValueFactory<>("ourCows"));
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Комп'ютер відгадав ваше число за " + computerGenerator.getMoves().size() + " спроб!" + gen.getDigits(), ButtonType.OK);
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Комп'ютер відгадав ваше число за " + computerGenerator.getMoves().size() + " спроб!", ButtonType.OK);
alert.showAndWait();
startbutton.setDisable(true);
textfield.setDisable(true);
Expand Down
17 changes: 13 additions & 4 deletions src/java/utils/GeneratorNumber.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package utils;

import controller.ControllerSettings;

import java.io.*;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GeneratorNumber implements Serializable {
private static final long serialVersionUID = 4405172041950251804L;
Expand All @@ -22,13 +26,18 @@ public int getNumber() {

public void read() {
Properties props = new Properties();
InputStream input = null;
InputStream input;
try {
File f = new File("settings.properties");
File f = new File(ControllerSettings.FILE_PROPERTIES);
if (!f.exists()) {
ControllerSettings.defaultFileProperties();
}
input = new FileInputStream(f);
props.load(input);
} catch (IOException ex) { java.util.logging.Logger.getLogger(GeneratorNumber.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);}
setDigits(Integer.parseInt(props.getProperty("DigitsCount")));
} catch (IOException ex) {
Logger.getLogger(GeneratorNumber.class.getName()).log(Level.SEVERE, null, ex);
}
setDigits(Integer.parseInt(props.getProperty(ControllerSettings.NUMBCOUNT)));
}

public void setDigits(int getdigits) {
Expand Down
1 change: 1 addition & 0 deletions src/java/view/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void initSettings() {
savechangesbutton.setLayoutX(117);
savechangesbutton.setLayoutY(138);
savechangesbutton.setText("Зберегти");
savechangesbutton.setDefaultButton(true);
savechangesbutton.setOnAction(event -> {
contr.writeUserSettings(choiceBox);
primaryStage.close();
Expand Down
2 changes: 1 addition & 1 deletion src/test/controller/AgainstCompTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testBackClick () {
clickOn("#backbutton");
}
@Test
public void testEmptyFieldInput () {
public void testEmptyFieldInput () {
clickOn("#inputbutton");
press(KeyCode.SPACE);

Expand Down
4 changes: 2 additions & 2 deletions src/test/controller/MainMenuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import org.junit.After;
import org.junit.Test;
import org.testfx.api.FxToolkit;
import org.testfx.framework.junit.ApplicationTest;
import view.MainMenu;

import java.io.File;
import java.net.URL;

public class MainMenuTest extends ApplicationTest {
String[] args;
@Override
public void start(Stage stage) throws Exception {
URL url = new File("src/resources/fxml/MainMenu.fxml").toURI().toURL();
Expand All @@ -38,6 +37,7 @@ public void testWithClick () {
@Test
public void testSettingsClick () {
clickOn("#settingsbutton");
press(KeyCode.SPACE);
}


Expand Down
1 change: 0 additions & 1 deletion src/test/controller/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public void tearDown () throws Exception {
@Test
public void initSettings(){
new Thread(() -> settings.initSettings());

}
@Test
public void testCancel () {
Expand Down
8 changes: 8 additions & 0 deletions src/test/utils/GeneratorNumberTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package utils;

import controller.ControllerSettings;
import org.junit.Test;

import java.io.File;

public class GeneratorNumberTest {
private GeneratorNumber gen = new GeneratorNumber();

Expand All @@ -16,6 +19,11 @@ public void testGenerate() {
public void testRead(){
gen.read();
}
@Test
public void testNoFile(){
File f = new File(ControllerSettings.FILE_PROPERTIES);
if(f.delete()){gen.read();}
}


}
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION_CODE=5.0.1
VERSION_CODE=5.0.2

0 comments on commit 7cc938c

Please sign in to comment.