Skip to content

Commit

Permalink
Reformat code after adding .editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
durimkryeziu committed Nov 16, 2024
1 parent efd21be commit d613701
Show file tree
Hide file tree
Showing 24 changed files with 525 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@

public class CharactersCounter {

private final InputStream inputStream;
private final PrintStream printStream;

public CharactersCounter(InputStream inputStream, PrintStream printStream) {
Objects.requireNonNull(inputStream, "inputStream must not be null");
Objects.requireNonNull(printStream, "printStream must not be null");
this.inputStream = inputStream;
this.printStream = printStream;
}

public void displayCharactersCount() {
askForInput();
String input = readInput();
if (input == null || input.isBlank()) {
this.printStream.println("Please enter something as input!");
return;
private final InputStream inputStream;
private final PrintStream printStream;

public CharactersCounter(InputStream inputStream, PrintStream printStream) {
Objects.requireNonNull(inputStream, "inputStream must not be null");
Objects.requireNonNull(printStream, "printStream must not be null");
this.inputStream = inputStream;
this.printStream = printStream;
}
this.printStream.printf("%s has %d characters.%n", input, input.length());
}

@SuppressWarnings("PMD.SystemPrintln")
private void askForInput() {
System.out.print("What is the input string? ");
}
public void displayCharactersCount() {
askForInput();
String input = readInput();
if (input == null || input.isBlank()) {
this.printStream.println("Please enter something as input!");
return;
}
this.printStream.printf("%s has %d characters.%n", input, input.length());
}

@SuppressWarnings("PMD.SystemPrintln")
private void askForInput() {
System.out.print("What is the input string? ");
}

private String readInput() {
try (Scanner scanner = new Scanner(this.inputStream)) {
return scanner.hasNext() ? scanner.nextLine() : null;
private String readInput() {
try (Scanner scanner = new Scanner(this.inputStream)) {
return scanner.hasNext() ? scanner.nextLine() : null;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class Main {

public static void main(String[] args) {
CharactersCounter charactersCounter = new CharactersCounter(System.in, System.out);
charactersCounter.displayCharactersCount();
}
public static void main(String[] args) {
CharactersCounter charactersCounter = new CharactersCounter(System.in, System.out);
charactersCounter.displayCharactersCount();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

final class CharactersCount {

private final String input;
private final String input;

private CharactersCount(String input) {
if (input == null || input.isBlank()) {
throw new IllegalArgumentException("input must not be blank");
private CharactersCount(String input) {
if (input == null || input.isBlank()) {
throw new IllegalArgumentException("input must not be blank");
}
this.input = input;
}
this.input = input;
}

static CharactersCount of(String input) {
return new CharactersCount(input);
}
static CharactersCount of(String input) {
return new CharactersCount(input);
}

@Override
public String toString() {
return "%s has %d characters.".formatted(this.input, this.input.length());
}
@Override
public String toString() {
return "%s has %d characters.".formatted(this.input, this.input.length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

public class CharactersCountApplication extends Application {

@Override
public void start(Stage stage) {
CharactersCountViewModel viewModel = new CharactersCountViewModel();
CharactersCountView view = new CharactersCountView(viewModel);
Scene scene = new Scene(view, 400, 200);
stage.setTitle("Counting the Number of Characters");
stage.setScene(scene);
stage.show();
}
@Override
public void start(Stage stage) {
CharactersCountViewModel viewModel = new CharactersCountViewModel();
CharactersCountView view = new CharactersCountView(viewModel);
Scene scene = new Scene(view, 400, 200);
stage.setTitle("Counting the Number of Characters");
stage.setScene(scene);
stage.show();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@

final class CharactersCountView extends VBox {

private final TextField inputTextField = new TextField();

private final Label outputLabel = new Label();

private final CharactersCountViewModel viewModel;

CharactersCountView(CharactersCountViewModel viewModel) {
this.viewModel = viewModel;
createView();
bindViewModel();
}

private void createView() {
setSpacing(10);
setAlignment(Pos.CENTER);
setPadding(new Insets(10, 20, 10, 20));
this.getChildren().addAll(new Label("What is the input string?"), this.inputTextField, this.outputLabel);
}

private void bindViewModel() {
this.inputTextField.textProperty().bindBidirectional(this.viewModel.inputProperty());
this.outputLabel.textProperty().bind(this.viewModel.outputProperty());
this.viewModel.inputProperty().addListener((obs, ov, nv) -> this.viewModel.countInputCharacters());
}
private final TextField inputTextField = new TextField();

private final Label outputLabel = new Label();

private final CharactersCountViewModel viewModel;

CharactersCountView(CharactersCountViewModel viewModel) {
this.viewModel = viewModel;
createView();
bindViewModel();
}

private void createView() {
setSpacing(10);
setAlignment(Pos.CENTER);
setPadding(new Insets(10, 20, 10, 20));
this.getChildren().addAll(new Label("What is the input string?"), this.inputTextField, this.outputLabel);
}

private void bindViewModel() {
this.inputTextField.textProperty().bindBidirectional(this.viewModel.inputProperty());
this.outputLabel.textProperty().bind(this.viewModel.outputProperty());
this.viewModel.inputProperty().addListener((obs, ov, nv) -> this.viewModel.countInputCharacters());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

final class CharactersCountViewModel {

private final StringProperty input = new SimpleStringProperty();
private final StringProperty input = new SimpleStringProperty();

private final StringProperty output = new SimpleStringProperty();
private final StringProperty output = new SimpleStringProperty();

StringProperty inputProperty() {
return this.input;
}
StringProperty inputProperty() {
return this.input;
}

StringProperty outputProperty() {
return this.output;
}
StringProperty outputProperty() {
return this.output;
}

void countInputCharacters() {
String inputValue = this.input.getValue();
if (inputValue == null || inputValue.isBlank()) {
this.output.set("Please enter some input string.");
} else {
this.output.set(CharactersCount.of(inputValue).toString());
void countInputCharacters() {
String inputValue = this.input.getValue();
if (inputValue == null || inputValue.isBlank()) {
this.output.set("Please enter some input string.");
} else {
this.output.set(CharactersCount.of(inputValue).toString());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class Main {

public static void main(String[] args) {
Application.launch(CharactersCountApplication.class);
}
public static void main(String[] args) {
Application.launch(CharactersCountApplication.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@

class CharactersCounterTests {

@Test
void displayCharactersCount_GivenNoInput_ShouldAskToInputSomething() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CharactersCounter charactersCounter = new CharactersCounter(new ByteArrayInputStream(new byte[]{}), new PrintStream(outputStream));
@Test
void displayCharactersCount_GivenNoInput_ShouldAskToInputSomething() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CharactersCounter charactersCounter = new CharactersCounter(new ByteArrayInputStream(new byte[]{}),
new PrintStream(outputStream));

charactersCounter.displayCharactersCount();
charactersCounter.displayCharactersCount();

assertThat(outputStream).hasToString("Please enter something as input!" + System.lineSeparator());
}
assertThat(outputStream).hasToString("Please enter something as input!" + System.lineSeparator());
}

@Test
void displayCharactersCount_GivenValidInput_ShouldDisplayCharactersCount() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CharactersCounter charactersCounter = new CharactersCounter(new ByteArrayInputStream("Durim".getBytes()), new PrintStream(outputStream));
@Test
void displayCharactersCount_GivenValidInput_ShouldDisplayCharactersCount() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CharactersCounter charactersCounter = new CharactersCounter(new ByteArrayInputStream("Durim".getBytes()),
new PrintStream(outputStream));

charactersCounter.displayCharactersCount();
charactersCounter.displayCharactersCount();

assertThat(outputStream).hasToString("Durim has 5 characters." + System.lineSeparator());
}
assertThat(outputStream).hasToString("Durim has 5 characters." + System.lineSeparator());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@

class CharactersCountTests {

@NullAndEmptySource
@ValueSource(strings = {" "})
@ParameterizedTest(name = "Given input: [{0}]")
void of_GivenInvalidInput_ShouldThrowException(String input) {
assertThatIllegalArgumentException().isThrownBy(() -> CharactersCount.of(input)).withMessage("input must not be blank");
}
@NullAndEmptySource
@ValueSource(strings = {" "})
@ParameterizedTest(name = "Given input: [{0}]")
void of_GivenInvalidInput_ShouldThrowException(String input) {
assertThatIllegalArgumentException()
.isThrownBy(() -> CharactersCount.of(input))
.withMessage("input must not be blank");
}

@Test
void of_GivenValidInput_ShouldReturnCharactersCount() {
String input = "Durim";
@Test
void of_GivenValidInput_ShouldReturnCharactersCount() {
String input = "Durim";

CharactersCount charactersCount = CharactersCount.of(input);
CharactersCount charactersCount = CharactersCount.of(input);

assertThat(charactersCount).hasToString("Durim has 5 characters.");
}
assertThat(charactersCount).hasToString("Durim has 5 characters.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@

class CharactersCountViewModelTests {

@NullAndEmptySource
@ValueSource(strings = {" "})
@ParameterizedTest(name = "Given input: [{0}]")
void countInputCharacters_GivenInputIsEmpty_ShouldOutputPleaseEnterSomeInputString(String input) {
CharactersCountViewModel viewModel = new CharactersCountViewModel();
viewModel.inputProperty().setValue(input);
@NullAndEmptySource
@ValueSource(strings = {" "})
@ParameterizedTest(name = "Given input: [{0}]")
void countInputCharacters_GivenInputIsEmpty_ShouldOutputPleaseEnterSomeInputString(String input) {
CharactersCountViewModel viewModel = new CharactersCountViewModel();
viewModel.inputProperty().setValue(input);

viewModel.countInputCharacters();
viewModel.countInputCharacters();

assertThat(viewModel.outputProperty().getValue()).isEqualTo("Please enter some input string.");
}
assertThat(viewModel.outputProperty().getValue()).isEqualTo("Please enter some input string.");
}

@Test
void countInputCharacters_GivenInputIsHelloWorld_ShouldOutputCharacterCount() {
CharactersCountViewModel viewModel = new CharactersCountViewModel();
viewModel.inputProperty().setValue("Hello, World!");
@Test
void countInputCharacters_GivenInputIsHelloWorld_ShouldOutputCharacterCount() {
CharactersCountViewModel viewModel = new CharactersCountViewModel();
viewModel.inputProperty().setValue("Hello, World!");

viewModel.countInputCharacters();
viewModel.countInputCharacters();

assertThat(viewModel.outputProperty().getValue()).isEqualTo("Hello, World! has 13 characters.");
}
assertThat(viewModel.outputProperty().getValue()).isEqualTo("Hello, World! has 13 characters.");
}

}
Loading

0 comments on commit d613701

Please sign in to comment.