Skip to content

Commit 88c61e6

Browse files
committed
Add CommandResult class
1 parent b3bb543 commit 88c61e6

28 files changed

+137
-118
lines changed

collection.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
id,bandName,x,y,creationDate,numberOfParticipants,albumsCount,description,genre,personName,height,eyeColor,hairColor,nationality,personX,personY,personZ

src/main/java/client/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean requestToServer(UserInputManager userInputManager) throws IOExcep
8585
out.println(response.getResponseBody());
8686
} else {
8787
ArgObjectForClient argObject = new ArgObjectForClient(clientCommandsManager, request.getArgsOfCommand(), null);
88-
out.println(request.getCommand().execute(argObject));
88+
out.println(request.getCommand().execute(argObject).getResult());
8989
}
9090
} catch (IllegalArgumentException e) {
9191
out.println(e.getMessage());

src/main/java/client/IOutils/fileUtils/FileCollectionInitializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public HashSet<MusicBand> initializeCollection(Iterable<CSVRecord> records) thro
2929
if (!objectValidation.checkObject(OneMusicBand)) {
3030
return null;
3131
}
32-
} //todo
32+
}
3333
return musicBands;
3434
}
3535

@@ -49,9 +49,11 @@ public MusicBand createMusicBand(CSVRecord record) throws ParseException {
4949
long albumsCount = Long.parseLong(record.get("albumsCount"));
5050
String description = record.get("description");
5151
MusicGenre genre = MusicGenre.valueOf(record.get("genre").toUpperCase());
52+
5253
Coordinates coordinates = createCoordinates(record);
5354
Person person = createPerson(record);
5455
MusicBandBuilder musicBandBuilder = new MusicBandBuilder();
56+
5557
return musicBandBuilder.setId(id)
5658
.setName(bandName)
5759
.setAlbumsCount(albumsCount)

src/main/java/client/IOutils/fileUtils/FileManager.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,19 @@ public HashSet<MusicBand> readCollection(String file) throws IOException, ParseE
3535
* @throws IOException in case of errors in writing to the file.
3636
*/
3737
public void writeCollection(String fileName, HashSet<MusicBand> musicBands) throws IOException {
38-
FileOutputStream fileOutputStream = null;
38+
FileOutputStream fileOutputStream;
3939
try {
4040
fileOutputStream = new FileOutputStream(fileName);
4141
} catch (FileNotFoundException e) {
42-
throw new FileNotFoundException("File is a directory or cannot be opened for any other reason.");
43-
} catch (SecurityException e) {
44-
throw new SecurityException("???");
45-
} finally {
46-
fileOutputStream.close();
47-
} //todo
42+
throw new FileNotFoundException("File is accessible.");
43+
}
4844
byte[] buffer = ("id,bandName,x,y,creationDate,numberOfParticipants,albumsCount,description,genre,personName," +
4945
"height,eyeColor,hairColor,nationality,personX,personY,personZ\n").getBytes();
5046
fileOutputStream.write(buffer, 0, buffer.length);
5147
for (MusicBand i : musicBands) {
5248
buffer = (i.getStringToSaveInFile() + "\n").getBytes();
5349
fileOutputStream.write(buffer, 0, buffer.length);
5450
}
55-
51+
fileOutputStream.close();
5652
}
5753
}

src/main/java/client/commands/ExecuteScript.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import client.IOutils.UserInputManager;
55
import client.commands.commandsUtils.ArgObjectForClient;
66
import sharedClasses.commands.Command;
7+
import sharedClasses.commands.commandsUtils.CommandResult;
8+
import sharedClasses.messageUtils.ResponseCode;
79

810
import java.io.File;
911
import java.io.FileNotFoundException;
@@ -27,40 +29,36 @@ public ExecuteScript(Client client) {
2729
fileNames = new HashSet<>();
2830
}
2931

30-
public String execute(ArgObjectForClient argObject) {
32+
public CommandResult execute(ArgObjectForClient argObject) {
3133
UserInputManager inputFromFile;
3234
try {
3335
FileReader fileReader = new FileReader(argObject.getArgs()[1]);
3436
inputFromFile = new UserInputManager(argObject.getClientCommandManager(), new Scanner(fileReader),
3537
false, new PrintStream(System.out));
3638
} catch (FileNotFoundException e) {
37-
return "Wrong file";
39+
return new CommandResult("Wrong file", ResponseCode.ERROR);
3840
}
3941
File script = new File(argObject.getArgs()[1]);
4042
if (!fileNames.add(script.getAbsolutePath())) {
41-
return "There is a loop in scripts! Execute_script wasn't executed, it was skipped.";
43+
return new CommandResult("There is a loop in scripts! Execute_script wasn't executed, it was skipped.", ResponseCode.ERROR);
4244
}
4345
String result = "Script in file " + argObject.getArgs()[1] + " was executed";
4446
boolean resultOfRequest;
4547
do {
4648
try {
4749
resultOfRequest = client.requestToServer(inputFromFile);
4850
} catch (NoSuchElementException e) {
49-
result = e.getMessage() + " (wrong input of command/object in script).";
50-
resultOfRequest = false;
51+
return new CommandResult(e.getMessage() + " (wrong input of command/object in script).", ResponseCode.ERROR);
5152
} catch (NumberFormatException e) {
52-
result = e.getMessage() + " (wrong input of object in script).";
53-
resultOfRequest = false;
53+
return new CommandResult(e.getMessage() + " (wrong input of object in script).", ResponseCode.ERROR);
5454
} catch (IllegalArgumentException e) {
55-
result = e.getMessage() + " (in script detected some unknown command)";
56-
resultOfRequest = false;
55+
return new CommandResult(e.getMessage() + " (in script detected some unknown command)", ResponseCode.ERROR);
5756
} catch (Exception e) {
58-
result = "Some exception during script execution: " + e.getMessage();
59-
resultOfRequest = false;
57+
return new CommandResult("Some exception during script execution: " + e.getMessage(), ResponseCode.ERROR);
6058
}
6159
} while (resultOfRequest);
6260
fileNames.remove(script.getAbsolutePath());
63-
return result;
61+
return new CommandResult(result, ResponseCode.OK);
6462
}
6563
}
6664

src/main/java/client/commands/Help.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import client.commands.commandsUtils.ArgObjectForClient;
44
import sharedClasses.commands.Command;
5+
import sharedClasses.commands.commandsUtils.CommandResult;
6+
import sharedClasses.messageUtils.ResponseCode;
57

68
/**
79
* Class for displaying help on available commands.
@@ -12,7 +14,7 @@ public Help() {
1214
}
1315

1416
@Override
15-
public String execute(ArgObjectForClient argObject) {
16-
return (argObject.getClientCommandManager()).getCommandsDescription();
17+
public CommandResult execute(ArgObjectForClient argObject) {
18+
return new CommandResult(argObject.getClientCommandManager().getCommandsDescription(), ResponseCode.OK);
1719
}
1820
}

src/main/java/client/commands/History.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import client.commands.commandsUtils.ArgObjectForClient;
44
import sharedClasses.commands.Command;
5+
import sharedClasses.commands.commandsUtils.CommandResult;
6+
import sharedClasses.messageUtils.ResponseCode;
57

68
import java.util.LinkedList;
79

@@ -13,12 +15,12 @@ public History() {
1315
super(false, 0, "HISTORY", "output the last 11 commands (without their arguments)", false);
1416
}
1517

16-
public String execute(ArgObjectForClient argObject) {
18+
public CommandResult execute(ArgObjectForClient argObject) {
1719
LinkedList<String> history = (argObject.getClientCommandManager()).getHistory();
1820
StringBuilder result = new StringBuilder();
1921
for (String name : history) {
2022
result.append(name).append("\n");
2123
}
22-
return result.substring(0, result.toString().length() - 1);
24+
return new CommandResult(result.substring(0, result.toString().length() - 1), ResponseCode.OK);
2325
}
2426
}

src/main/java/client/connectionUtils/AddressValidation.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ public class AddressValidation {
2121
* A method for checking the host and port.
2222
* @return true if the host and port are entered correctly; otherwise false.
2323
*/
24-
public boolean checkAddress() { //todo проверка port?
24+
public boolean checkAddress() {
2525
if (hostAndPort.length != 2) {
2626
out.println("Invalid input format. Specify host and port separated by a space after the jar-file name.");
2727
return false;
2828
}
2929
try {
3030
port = Integer.parseInt(hostAndPort[1]);
3131
host = hostAndPort[0];
32+
if (port <=0 || port >65535) {
33+
out.println("Wrong number for port");
34+
return false;
35+
}
3236
} catch (NumberFormatException e) {
33-
out.println("port - an integer number than or equal to zero.");
37+
out.println("Port - an integer number than or equal to zero.");
3438
return false;
3539
}
3640
return true;

src/main/java/server/Server.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import lombok.RequiredArgsConstructor;
44
import server.collectionUtil.CollectionManager;
5-
import server.commands.ArgObjectForServer;
5+
import server.commandsUtils.ArgObjectForServer;
6+
import sharedClasses.commands.commandsUtils.CommandResult;
67
import sharedClasses.messageUtils.Request;
78
import sharedClasses.messageUtils.Response;
89

@@ -53,24 +54,25 @@ private void openServerSocket() throws IOException {
5354
private void saveIfExit() throws IOException {
5455
try {
5556
collectionManager.saveCollection();
56-
exit();
57+
LOGGER.log(Level.INFO, "The collection was saved");
5758
} catch (FileNotFoundException e) {
5859
LOGGER.log(Level.SEVERE, "This file wasn't found");
5960
} catch (SecurityException e) {
6061
LOGGER.log(Level.SEVERE, "Write access to the file is denied");
6162
} catch (IOException e) {
62-
LOGGER.log(Level.SEVERE, "Some I/O errors occur");
63+
e.printStackTrace();
64+
LOGGER.log(Level.SEVERE, "Some I/O errors occur: " + e.getMessage());
6365
}
64-
LOGGER.log(Level.INFO, "The collection was saved");
66+
exit();
6567
run();
6668
}
6769

68-
private void exit() throws IOException {
70+
private void exit() {
6971
try {
7072
if (serverSocket != null) {
7173
serverSocket.close();
7274
}
73-
LOGGER.log(Level.INFO, "The connection with the client is broken.");
75+
LOGGER.log(Level.INFO, "The connection with the client closed.");
7476

7577
} catch (IOException e) {
7678
LOGGER.log(Level.INFO, "Error when completing the connection with the client.");
@@ -92,8 +94,8 @@ private boolean processClientRequest(Socket clientSocket) throws IOException, Cl
9294
do {
9395
request = (Request) clientReader.readObject();
9496
ArgObjectForServer argObject = new ArgObjectForServer(collectionManager, request.getArgsOfCommand(), request.getMusicBand());
95-
//CommandResult c = request.getCommand().execute(argObject);
96-
response = new Response("OK", request.getCommand().execute(argObject)); //todo разные коды возврата
97+
CommandResult commandResult = request.getCommand().execute(argObject);
98+
response = new Response(commandResult.getResponseCode(), commandResult.getResult());
9799
clientWriter.writeObject(response);
98100
if (request.getCommand().getName().equals("EXIT")) {
99101
return false;

src/main/java/server/commands/Save.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/server/commands/ArgObjectForServer.java renamed to src/main/java/server/commandsUtils/ArgObjectForServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package server.commands;
1+
package server.commandsUtils;
22

33
import lombok.Getter;
44
import server.collectionUtil.CollectionManager;
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package sharedClasses.commands;
22

33
import server.collectionUtil.CollectionManager;
4-
import server.commands.ArgObjectForServer;
4+
import server.commandsUtils.ArgObjectForServer;
5+
import sharedClasses.commands.commandsUtils.CommandResult;
6+
import sharedClasses.messageUtils.ResponseCode;
57

68
/**
79
* A class for adding a new item to the collection.
@@ -11,10 +13,10 @@ public Add() {
1113
super(true, 0, "ADD", "output help for available commands", true);
1214
}
1315

14-
public String execute(ArgObjectForServer argObject) {
16+
public CommandResult execute(ArgObjectForServer argObject) {
1517
CollectionManager collectionManager = argObject.getCollectionManager();
1618
argObject.getMusicBand().setId(collectionManager.generateId());
1719
collectionManager.add(argObject.getMusicBand());
18-
return "Music band was added.";
20+
return new CommandResult("Music band was added.", ResponseCode.OK);
1921
}
2022
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sharedClasses.commands;
22

3-
import server.commands.ArgObjectForServer;
3+
import server.commandsUtils.ArgObjectForServer;
4+
import sharedClasses.commands.commandsUtils.CommandResult;
5+
import sharedClasses.messageUtils.ResponseCode;
46

57
/**
68
* A class for adding an item to a collection if its albumsCount field is less than the minimum inside the collection.
@@ -10,12 +12,12 @@ public AddIfMin() {
1012
super(true, 0, "ADD_IF_MIN", " add a new element to the collection if its value is less than the smallest element of this collection", true);
1113
}
1214

13-
public String execute(ArgObjectForServer argObject) {
15+
public CommandResult execute(ArgObjectForServer argObject) {
1416
String result = "This band wasn't added because it has too much albums";
1517
if (argObject.getMusicBand().compareTo(argObject.getCollectionManager().getMinObject()) < 0) {
1618
argObject.getCollectionManager().add(argObject.getMusicBand());
1719
result = "Music band was added.";
1820
}
19-
return result;
21+
return new CommandResult(result, ResponseCode.OK);
2022
}
2123
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sharedClasses.commands;
22

3-
import server.commands.ArgObjectForServer;
3+
import server.commandsUtils.ArgObjectForServer;
4+
import sharedClasses.commands.commandsUtils.CommandResult;
5+
import sharedClasses.messageUtils.ResponseCode;
46

57
/**
68
* A class for cleaning the collection.
@@ -11,8 +13,8 @@ public Clear() {
1113
super(false, 0, "CLEAR", "to clear the collection", true);
1214
}
1315

14-
public String execute(ArgObjectForServer argObject) {
16+
public CommandResult execute(ArgObjectForServer argObject) {
1517
argObject.getCollectionManager().clear();
16-
return "Collection is empty now!";
18+
return new CommandResult("Collection is empty now!", ResponseCode.OK);
1719
}
1820
}

src/main/java/sharedClasses/commands/Command.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sharedClasses.commands.commandsUtils.ArgObject;
44
import lombok.Getter;
55
import lombok.RequiredArgsConstructor;
6+
import sharedClasses.commands.commandsUtils.CommandResult;
67

78
import java.io.Serializable;
89

@@ -29,6 +30,6 @@ public abstract class Command<T> implements Serializable {
2930
* @param argObject an object for storing command arguments {@link ArgObject}.
3031
* @return the result of the command execution.
3132
*/
32-
public abstract String execute(T argObject);
33+
public abstract CommandResult execute(T argObject);
3334

3435
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sharedClasses.commands;
22

3-
import server.commands.ArgObjectForServer;
3+
import server.commandsUtils.ArgObjectForServer;
4+
import sharedClasses.commands.commandsUtils.CommandResult;
5+
import sharedClasses.messageUtils.ResponseCode;
46

57
/**
68
* A class for terminating a program.
@@ -10,7 +12,7 @@ public Exit() {
1012
super(false, 0, "EXIT", "terminate the program (without saving to a file)", true);
1113
}
1214

13-
public String execute(ArgObjectForServer argObject) {
14-
return "Program is finishing...:(";
15+
public CommandResult execute(ArgObjectForServer argObject) {
16+
return new CommandResult("Program is finishing.", ResponseCode.OK);
1517
}
1618
}

0 commit comments

Comments
 (0)