Skip to content

Commit c1cb4a5

Browse files
committed
Fix some bugs
1 parent d4bc2ff commit c1cb4a5

File tree

6 files changed

+42
-67
lines changed

6 files changed

+42
-67
lines changed

client/src/main/java/IOutils/UserInputManager.java

+6-25
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,20 @@ public UserInputManager(ClientCommandsManager clientCommandsManager, Scanner inp
3232
*
3333
* @return true if the program execution can be continued; false if the program execution should be stopped.
3434
*/
35-
public Request input() {
35+
public Request input() throws NumberFormatException {
3636
printInviteMessage();
3737
if (!input.hasNext()) return null;
3838
String[] s = input.nextLine().split(" ");
3939
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;
40+
if (command == null) {
41+
throw new IllegalArgumentException("There's no such command");
4942
}
43+
clientcommandsManager.addToHistory(command.getName());
44+
if (!checkArgsCount(s, command))
45+
throw new IllegalArgumentException("Wrong amount of arguments. Please, try again! (You can use command \"help\" for more information.)");
5046
return new Request(command, s, readObjectIfNecessary(command));
5147
}
5248

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-
6849
/**
6950
* Method for checking the number of command parameters.
7051
*

client/src/main/java/IOutils/readers/PersonReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Double readHeight() {
8181
inputAndOutput.printLine("Height must be more than 0.");
8282
}
8383
}
84-
} catch (NumberFormatException e) {
84+
} catch (IllegalArgumentException e) {
8585
inputAndOutput.printLine("Wrong format of input! Height must be a number!");
8686
}
8787
if (!inputAndOutput.isShowMessages()) throw new NumberFormatException("Wrong format of height.");

client/src/main/java/clientApp/Client.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ public void run() throws IOException, InterruptedException {
3232
boolean statusOfRequest = true;
3333
boolean statusOfConnection;
3434
while (statusOfRequest && countOfConnections <= maxCountOfConnection) {
35-
try {
36-
statusOfConnection = connectToServer();
37-
if (statusOfConnection) statusOfRequest = requestToServer(userInputManager);
38-
} catch (IOException | IllegalArgumentException | ClassNotFoundException e) {
39-
out.println(e.getMessage());
40-
}
35+
statusOfConnection = connectToServer();
36+
if (statusOfConnection) statusOfRequest = requestToServer(userInputManager);
4137
}
4238
disconnect();
4339
out.println("The client has completed his work.");
@@ -71,7 +67,7 @@ private void disconnect() throws IOException {
7167
if (socketChannel != null) socketChannel.close();
7268
}
7369

74-
public boolean requestToServer(UserInputManager userInputManager) throws IOException, ClassNotFoundException, IllegalArgumentException {
70+
public boolean requestToServer(UserInputManager userInputManager) {
7571
try {
7672
Request request = null;
7773
Response response;
@@ -93,10 +89,11 @@ public boolean requestToServer(UserInputManager userInputManager) throws IOExcep
9389
} while (request == null || !request.getNameOfCommand().equals("EXIT"));
9490
return false;
9591
} catch (IOException e) {
96-
throw new IOException("The connection to the server is broken.");
92+
out.println("The connection to the server is broken.");
93+
return false;
9794
} catch (ClassNotFoundException e) {
98-
throw new ClassNotFoundException("An error occurred while reading the data.");
95+
out.println("An error occurred while reading the data.");
96+
return false;
9997
}
10098
}
101-
10299
}

collection.csv

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
id,bandName,x,y,creationDate,numberOfParticipants,albumsCount,description,genre,personName,height,eyeColor,hairColor,nationality,personX,personY,personZ
22
1,fddfd,34.0,34,2022-04-27,4,34,34,JAZZ,45,45.0,RED,RED,INDIA,434.0,3,43
3+
2,123,31.0,3,2022-04-27,21,12,23,JAZZ,21213,21.0,,RED,INDIA,2.0,32,1

server/src/main/java/serverApp/App.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,49 @@
1212
import java.util.regex.Pattern;
1313

1414
public class App {
15-
static Logger LOGGER = Logger.getLogger(App.class.getName());
15+
static Logger logger = Logger.getLogger(App.class.getName());
1616

1717
public static void main(String[] args) {
1818
int port = 0;
1919
try {
2020
port = Integer.parseInt(args[0]);
2121
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
22-
LOGGER.log(Level.SEVERE, "Specify the port when launching the jar file.");
22+
logger.log(Level.SEVERE, "Specify the port when launching the jar file.");
2323
System.exit(-1);
2424
}
2525
CollectionManager collectionManager = new CollectionManager();
2626
Map env = System.getenv();
2727
String fileName = (String) env.get("FILE_NAME");
2828
if (fileName == null) {
29-
LOGGER.log(Level.SEVERE, "No file name in environment variable FILE_NAME");
29+
logger.log(Level.SEVERE, "No file name in environment variable FILE_NAME");
3030
System.exit(-1);
3131
}
3232
Pattern pattern = Pattern.compile("/*/dev/*");
3333
File file = new File(fileName);
3434
Matcher matcher = pattern.matcher(file.getAbsolutePath());
3535
if (matcher.find()) {
36-
LOGGER.log(Level.SEVERE, "Incorrect file name or data in file.");
36+
logger.log(Level.SEVERE, "Incorrect file name or data in file.");
3737
System.exit(-1);
3838
}
3939
try {
4040
if (!collectionManager.fillCollection(fileName)) {
41-
LOGGER.log(Level.SEVERE, "Incorrect file name or data in file.");
41+
logger.log(Level.SEVERE, "Incorrect file name or data in file.");
4242
System.exit(-1);
4343
}
4444
} catch (IOException | ParseException | NumberFormatException e) {
45-
LOGGER.log(Level.SEVERE, e.getMessage());
45+
logger.log(Level.SEVERE, e.getMessage());
4646
System.exit(-1);
4747
} catch (Exception e) {
48-
LOGGER.log(Level.SEVERE, "Some exception: " + e.getMessage());
48+
logger.log(Level.SEVERE, "Some exception: " + e.getMessage());
4949
System.exit(-1);
5050
}
5151
Server server = new Server(port, collectionManager);
5252
try {
5353
server.run();
5454
} catch (IOException e) {
55-
LOGGER.log(Level.SEVERE, e.getMessage());
55+
logger.log(Level.SEVERE, e.getMessage());
56+
} catch (ClassNotFoundException e) {
57+
logger.log(Level.SEVERE, e.getMessage());
5658
}
5759
}
5860
}

server/src/main/java/serverApp/Server.java

+17-23
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.net.ServerSocket;
1515
import java.net.Socket;
1616
import java.util.logging.Level;
17-
import static serverApp.App.LOGGER;
17+
import static serverApp.App.logger;
1818

1919

2020
@RequiredArgsConstructor
@@ -24,20 +24,12 @@ public class Server {
2424
private ServerSocket serverSocket;
2525

2626

27-
public void run() throws IOException {
27+
public void run() throws IOException, ClassNotFoundException {
2828
boolean status = true;
2929
openServerSocket();
3030
while (status) {
31-
try {
32-
Socket clientSocket = connectToClient();
33-
status = processClientRequest(clientSocket);
34-
} catch (ClassNotFoundException e) {
35-
LOGGER.log(Level.SEVERE, e.getMessage());
36-
status = false;
37-
} catch (IOException e) {
38-
LOGGER.log(Level.WARNING, e.getMessage());
39-
status = false;
40-
}
31+
Socket clientSocket = connectToClient();
32+
status = processClientRequest(clientSocket);
4133
}
4234
saveIfExit();
4335
}
@@ -46,21 +38,21 @@ private void openServerSocket() throws IOException {
4638
try {
4739
serverSocket = new ServerSocket(port);
4840
} catch (IOException e) {
49-
throw new IOException("Failed to open ServerSocket.");
41+
logger.log(Level.WARNING, "Failed to open ServerSocket.");
5042
}
5143
}
5244

53-
private void saveIfExit() throws IOException {
45+
private void saveIfExit() throws IOException, ClassNotFoundException {
5446
try {
5547
collectionManager.saveCollection();
56-
LOGGER.log(Level.INFO, "The collection was saved");
48+
logger.log(Level.INFO, "The collection was saved");
5749
} catch (FileNotFoundException e) {
58-
LOGGER.log(Level.SEVERE, "This file wasn't found");
50+
logger.log(Level.SEVERE, "This file wasn't found");
5951
} catch (SecurityException e) {
60-
LOGGER.log(Level.SEVERE, "Write access to the file is denied");
52+
logger.log(Level.SEVERE, "Write access to the file is denied");
6153
} catch (IOException e) {
6254
e.printStackTrace();
63-
LOGGER.log(Level.SEVERE, "Some I/O errors occur: " + e.getMessage());
55+
logger.log(Level.SEVERE, "Some I/O errors occur: " + e.getMessage());
6456
}
6557
exit();
6658
run();
@@ -71,16 +63,16 @@ private void exit() {
7163
if (serverSocket != null) {
7264
serverSocket.close();
7365
}
74-
LOGGER.log(Level.INFO, "The connection with the client closed.");
66+
logger.log(Level.INFO, "The connection with the client closed.");
7567

7668
} catch (IOException e) {
77-
LOGGER.log(Level.INFO, "Error when completing the connection with the client.");
69+
logger.log(Level.INFO, "Error when completing the connection with the client.");
7870
}
7971
}
8072

8173
private Socket connectToClient() throws IOException {
8274
Socket clientSocket = serverSocket.accept();
83-
LOGGER.log(Level.INFO, "The connection with the client has been successfully established");
75+
logger.log(Level.INFO, "The connection with the client has been successfully established");
8476
return clientSocket;
8577
}
8678

@@ -102,9 +94,11 @@ private boolean processClientRequest(Socket clientSocket) throws IOException, Cl
10294
clientWriter.flush();
10395
} while (true);
10496
} catch (IOException e) {
105-
throw new IOException("Disconnection from the client.");
97+
logger.log(Level.WARNING, "Disconnection from the client.");
98+
return false;
10699
} catch (ClassNotFoundException e) {
107-
throw new ClassNotFoundException("An error occurred while reading the received data!");
100+
logger.log(Level.SEVERE,"An error occurred while reading the received data!");
101+
return false;
108102
}
109103
}
110104
}

0 commit comments

Comments
 (0)