Skip to content

Commit d4bc2ff

Browse files
committed
Fix some bugs
1 parent 88c61e6 commit d4bc2ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1520
-1535
lines changed

build.gradle

-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ sourceCompatibility = "8"
77
targetCompatibility = "8"
88

99
dependencies {
10-
implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.9.0'
11-
implementation 'commons-io:commons-io:2.11.0'
12-
implementation 'javax.servlet:javax.servlet-api:4.0.1'
13-
implementation 'javax.servlet:servlet-api:2.5'
14-
implementation 'javax.inject:javax.inject:1'
15-
implementation 'javax.validation:validation-api:2.0.1.Final'
16-
implementation 'user.java.io:user.java.io:2020.134.36653'
17-
implementation 'javax.annotation:javax.annotation-api:1.3.2'
18-
implementation 'com.github.nbbrd.java-io-util:java-io-parent:0.0.19'
1910
compileOnly 'org.projectlombok:lombok:1.18.22'
2011
annotationProcessor 'org.projectlombok:lombok:1.18.22'
2112

src/main/java/client/IOutils/InputAndOutput.java renamed to client/src/main/java/IOutils/InputAndOutput.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client.IOutils;
1+
package IOutils;
22

33
import lombok.Getter;
44
import lombok.RequiredArgsConstructor;
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
1-
package client.IOutils;
2-
3-
import client.IOutils.readers.ObjectReader;
4-
import client.commands.commandsUtils.ClientCommandsManager;
5-
import sharedClasses.data.MusicBand;
6-
import sharedClasses.commands.Command;
7-
import sharedClasses.messageUtils.Request;
8-
9-
import java.io.PrintStream;
10-
import java.util.*;
11-
12-
/**
13-
* A class for recognizing user input (from the console or file).
14-
*/
15-
public class UserInputManager {
16-
private final ClientCommandsManager clientcommandsManager;
17-
private final ObjectReader objectReader;
18-
private final Scanner input;
19-
private final boolean showMessages;
20-
private final PrintStream out;
21-
22-
public UserInputManager(ClientCommandsManager clientCommandsManager, Scanner input, boolean showMessages, PrintStream printStream) {
23-
this.clientcommandsManager = clientCommandsManager;
24-
this.input = input;
25-
objectReader = new ObjectReader(input, showMessages);
26-
this.showMessages = showMessages;
27-
this.out = printStream;
28-
}
29-
30-
/**
31-
* A method for reading user input.
32-
*
33-
* @return true if the program execution can be continued; false if the program execution should be stopped.
34-
*/
35-
public Request input() {
36-
printInviteMessage();
37-
if (!input.hasNext()) return null;
38-
String[] s = input.nextLine().split(" ");
39-
Command command = clientcommandsManager.getCommand(s[0].toUpperCase(Locale.ROOT));
40-
try {
41-
if (command == null) {
42-
throw new IllegalArgumentException("There's no such command");
43-
}
44-
clientcommandsManager.addToHistory(command.getName());
45-
if (!checkArgsCount(s, command))
46-
throw new IllegalArgumentException("Wrong amount of arguments. Please, try again! (You can use command \"help\" for more information.)");
47-
} catch (NumberFormatException e) {
48-
throw e;
49-
}
50-
return new Request(command, s, readObjectIfNecessary(command));
51-
}
52-
53-
/**
54-
* A method for confirming the user's action.
55-
*
56-
* @return true if the user confirms the action; false if the user rejects the action.
57-
*/
58-
private boolean askQuestion() {
59-
while (true) {
60-
out.println("yes/no?");
61-
if (!input.hasNext()) return true;
62-
String answer = input.nextLine();
63-
if (answer.equals("yes")) return true;
64-
if (answer.equals("no")) return false;
65-
}
66-
}
67-
68-
/**
69-
* Method for checking the number of command parameters.
70-
*
71-
* @param s array of user input.
72-
* @param command the command entered by the user.
73-
* @return true if the number of arguments is correct, otherwise false.
74-
*/
75-
private boolean checkArgsCount(String[] s, Command command) {
76-
if (command.getCountOfArgs() == s.length - 1) return true;
77-
return false;
78-
}
79-
80-
/**
81-
* A method for reading a collection item, if necessary to execute a command.
82-
*
83-
* @param command the current executable command.
84-
* @return read object {@link MusicBand} or null if the object is not required.
85-
*/
86-
private MusicBand readObjectIfNecessary(Command command) {
87-
if (!command.isNeedObject()) return null;
88-
return objectReader.readObject();
89-
}
90-
91-
/**
92-
* A method for displaying an input prompt for the user (in the case of reading data from the console).
93-
*/
94-
private void printInviteMessage() {
95-
if (showMessages) System.out.println("Enter command (if you don't know commands, enter command \"help\"):");
96-
}
97-
}
1+
package IOutils;
2+
3+
import IOutils.readers.ObjectReader;
4+
import commands.commandsUtils.ClientCommandsManager;
5+
import data.MusicBand;
6+
import commands.Command;
7+
import messageUtils.Request;
8+
9+
import java.io.PrintStream;
10+
import java.util.*;
11+
12+
/**
13+
* A class for recognizing user input (from the console or file).
14+
*/
15+
public class UserInputManager {
16+
private final ClientCommandsManager clientcommandsManager;
17+
private final ObjectReader objectReader;
18+
private final Scanner input;
19+
private final boolean showMessages;
20+
private final PrintStream out;
21+
22+
public UserInputManager(ClientCommandsManager clientCommandsManager, Scanner input, boolean showMessages, PrintStream printStream) {
23+
this.clientcommandsManager = clientCommandsManager;
24+
this.input = input;
25+
objectReader = new ObjectReader(input, showMessages);
26+
this.showMessages = showMessages;
27+
this.out = printStream;
28+
}
29+
30+
/**
31+
* A method for reading user input.
32+
*
33+
* @return true if the program execution can be continued; false if the program execution should be stopped.
34+
*/
35+
public Request input() {
36+
printInviteMessage();
37+
if (!input.hasNext()) return null;
38+
String[] s = input.nextLine().split(" ");
39+
Command command = clientcommandsManager.getCommand(s[0].toUpperCase(Locale.ROOT));
40+
try {
41+
if (command == null) {
42+
throw new IllegalArgumentException("There's no such command");
43+
}
44+
clientcommandsManager.addToHistory(command.getName());
45+
if (!checkArgsCount(s, command))
46+
throw new IllegalArgumentException("Wrong amount of arguments. Please, try again! (You can use command \"help\" for more information.)");
47+
} catch (NumberFormatException e) {
48+
throw e;
49+
}
50+
return new Request(command, s, readObjectIfNecessary(command));
51+
}
52+
53+
/**
54+
* A method for confirming the user's action.
55+
*
56+
* @return true if the user confirms the action; false if the user rejects the action.
57+
*/
58+
private boolean askQuestion() {
59+
while (true) {
60+
out.println("yes/no?");
61+
if (!input.hasNext()) return true;
62+
String answer = input.nextLine();
63+
if (answer.equals("yes")) return true;
64+
if (answer.equals("no")) return false;
65+
}
66+
}
67+
68+
/**
69+
* Method for checking the number of command parameters.
70+
*
71+
* @param s array of user input.
72+
* @param command the command entered by the user.
73+
* @return true if the number of arguments is correct, otherwise false.
74+
*/
75+
private boolean checkArgsCount(String[] s, Command command) {
76+
if (command.getCountOfArgs() == s.length - 1) return true;
77+
return false;
78+
}
79+
80+
/**
81+
* A method for reading a collection item, if necessary to execute a command.
82+
*
83+
* @param command the current executable command.
84+
* @return read object {@link MusicBand} or null if the object is not required.
85+
*/
86+
private MusicBand readObjectIfNecessary(Command command) {
87+
if (!command.isNeedObject()) return null;
88+
return objectReader.readObject();
89+
}
90+
91+
/**
92+
* A method for displaying an input prompt for the user (in the case of reading data from the console).
93+
*/
94+
private void printInviteMessage() {
95+
if (showMessages) System.out.println("Enter command (if you don't know commands, enter command \"help\"):");
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
package client.IOutils.readers;
2-
3-
import client.IOutils.InputAndOutput;
4-
import sharedClasses.data.Coordinates;
5-
import lombok.RequiredArgsConstructor;
6-
7-
@RequiredArgsConstructor
8-
public class CoordinatesReader {
9-
private final InputAndOutput inputAndOutput;
10-
11-
public Coordinates readCoordinates() {
12-
Float x = readXCoordinate();
13-
int y = readYCoordinate();
14-
return new Coordinates(x, y);
15-
}
16-
17-
public Float readXCoordinate() {
18-
while (true) {
19-
try {
20-
Float x = Float.parseFloat(inputAndOutput.readLine("Input x coordinate. Max field value: 146.").replace(",", "."));
21-
if (x > 146) {
22-
inputAndOutput.printLine("Wrong format of input! Max field value: 146. ");
23-
} else return x;
24-
} catch (NumberFormatException e) {
25-
inputAndOutput.printLine("Wrong format of input! It should be a float number.");
26-
}
27-
if (!inputAndOutput.isShowMessages()) throw new NumberFormatException("Wrong format of X coordinate.");
28-
}
29-
}
30-
31-
public int readYCoordinate() {
32-
while (true) {
33-
try {
34-
return Integer.parseInt(inputAndOutput.readLine("Input y coordinate. It must be integer."));
35-
} catch (NumberFormatException e) {
36-
inputAndOutput.printLine("Wrong format of input! It must be integer.");
37-
}
38-
if (!inputAndOutput.isShowMessages()) throw new NumberFormatException("Wrong format of Y coordinate.");
39-
}
40-
41-
}
42-
}
1+
package IOutils.readers;
2+
3+
import IOutils.InputAndOutput;
4+
import data.Coordinates;
5+
import lombok.RequiredArgsConstructor;
6+
7+
@RequiredArgsConstructor
8+
public class CoordinatesReader {
9+
private final InputAndOutput inputAndOutput;
10+
11+
public Coordinates readCoordinates() {
12+
Float x = readXCoordinate();
13+
int y = readYCoordinate();
14+
return new Coordinates(x, y);
15+
}
16+
17+
public Float readXCoordinate() {
18+
while (true) {
19+
try {
20+
Float x = Float.parseFloat(inputAndOutput.readLine("Input x coordinate. Max field value: 146.").replace(",", "."));
21+
if (x > 146) {
22+
inputAndOutput.printLine("Wrong format of input! Max field value: 146. ");
23+
} else return x;
24+
} catch (NumberFormatException e) {
25+
inputAndOutput.printLine("Wrong format of input! It should be a float number.");
26+
}
27+
if (!inputAndOutput.isShowMessages()) throw new NumberFormatException("Wrong format of X coordinate.");
28+
}
29+
}
30+
31+
public int readYCoordinate() {
32+
while (true) {
33+
try {
34+
return Integer.parseInt(inputAndOutput.readLine("Input y coordinate. It must be integer."));
35+
} catch (NumberFormatException e) {
36+
inputAndOutput.printLine("Wrong format of input! It must be integer.");
37+
}
38+
if (!inputAndOutput.isShowMessages()) throw new NumberFormatException("Wrong format of Y coordinate.");
39+
}
40+
41+
}
42+
}

0 commit comments

Comments
 (0)