Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leonid ecobike #1

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/ecobike/CommandExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ecobike;

import com.ecobike.command.AddEBikeCommand;
import com.ecobike.command.AddFoldingBikeCommand;
import com.ecobike.command.AddSpeedelecBikeCommand;
import com.ecobike.command.Command;
import com.ecobike.command.FindCommand;
import com.ecobike.command.ShowCommand;
import com.ecobike.command.StopProgramCommand;
import com.ecobike.command.WriteToFileCommand;
import java.util.HashMap;
import java.util.Map;

/**
* Utility class responsible for execution properly command for operation chosen by user.
*/
public class CommandExecutor {
/**
* Map contains properly command for all operations.
*/
private static final Map<Operation, Command> allKnownCommandsMap = new HashMap<>();

static {
allKnownCommandsMap.put(Operation.SHOW_CATALOG, new ShowCommand());
allKnownCommandsMap.put(Operation.ADD_FOLDING_BIKE, new AddFoldingBikeCommand());
allKnownCommandsMap.put(Operation.ADD_SPEEDELEC_BIKE, new AddSpeedelecBikeCommand());
allKnownCommandsMap.put(Operation.ADD_E_BIKE, new AddEBikeCommand());
allKnownCommandsMap.put(Operation.FIND_ITEMS_BY_BRAND, new FindCommand());
allKnownCommandsMap.put(Operation.WRITE_TO_FILE, new WriteToFileCommand());
allKnownCommandsMap.put(Operation.STOP_PROGRAM, new StopProgramCommand());
}

private CommandExecutor() {
}

/**
* Method execute command for specified operation.
* @param operation chosen by user.
*/
public static void execute(Operation operation) {
allKnownCommandsMap.get(operation).execute();
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/ecobike/Communicator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.ecobike;

import com.ecobike.model.Bike;
import java.util.List;

/**
* Interface should be implemented by class responsible for communication with user.
*/
public interface Communicator {
/**
* Method writs message to user.
*/
void writeMessage(String message);

/**
* Method reads string from user.
*
* @return entered string.
*/
String readString();

/**
* Method reads not empty string from user.
* @return entered string.
*/
String readNotEmptyString();

/**
* Method reads integer from user.
* For empty entry returns 0.
*
* @return 0 or positive int value.
*/
int readInt();

/**
* Method reads integer from user.
* Only positive values are allowed.
*
* @return positive int value.
*/
int readPositiveInt();

/**
* Method reads boolean from user.
* @return boolean value from user.
*/
boolean readBoolean();

/**
* Method prints info about bikes from the list
* page by page.
*
* @param bikes list of bikes for printing
*/
void printBikes(List<Bike> bikes);

/**
* Ask user to confirm operation.
*
* @param message to be shown for user.
* @return true if confirms or false otherwise.
*/
boolean confirmAction(String message);
}
161 changes: 161 additions & 0 deletions src/main/java/com/ecobike/ConsoleCommunicator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.ecobike;

import com.ecobike.model.Bike;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
* Class responsible for communication with user with console.
*/
public class ConsoleCommunicator implements Communicator {

private static final BufferedReader reader
= new BufferedReader(new InputStreamReader(System.in));
/**
* Number of bikes to be shown on one page.
*/
private static final int BIKES_ON_PAGE_QUANTITY = 50;

/**
* Method writs message to user.
*/
@Override
public void writeMessage(String message) {
System.out.println(message);
}

/**
* Method reads string from user.
*
* @return entered string
*/
@Override
public String readString() {
String entry = null;
while (entry == null) {
try {
entry = reader.readLine();
} catch (IOException e) {
writeMessage("Repeat your entry:");
}
}
return entry;
}

/**
* Method reads not empty string from user.
*
* @return entered string.
*/
@Override
public String readNotEmptyString() {
String entry;
while ((entry = readString()).isEmpty()) {
writeMessage("Empty entry is not allowed. Repeat, please.");
}
return entry;
}

/**
* Method reads integer from user.
* For empty entry returns 0.
*
* @return 0 or positive int value.
*/
@Override
public int readInt() {
while (true) {
String entry = readString();
if (entry.isEmpty()) {
return 0;
}
int intValue;
try {
if ((intValue = Integer.parseInt(entry)) >= 0) {
return intValue;
}
} catch (NumberFormatException e) {
writeMessage("Repeat your entry (only positive number):");
}
}
}

/**
* Method reads integer from user.
* Only positive values are allowed.
*
* @return positive int value.
*/
@Override
public int readPositiveInt() {
int entry;
while ((entry = readInt()) == 0) {
writeMessage("Empty entry or zero is not allowed. Repeat, please.");
}
return entry;
}

/**
* Method reads boolean from user.
*
* @return boolean value from user.
*/
@Override
public boolean readBoolean() {
writeMessage("Type 1 for TRUE or 2 for FALSE");
while (true) {
String entry = readString();
if (entry.equals("1")) {
return true;
}
if (entry.equals("2")) {
return false;
}
writeMessage("Wrong entry. Type 1 for TRUE or 2 for FALSE");
}
}

/**
* Method prints info about bikes from the list
* page by page (max 50 on one page by default).
*
* @param bikes list of bikes for printing
*/
@Override
public void printBikes(List<Bike> bikes) {
Rommelua marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < bikes.size(); i++) {
writeMessage(i + 1 + ". " + bikes.get(i).toOutputString());
if ((i + 1) % BIKES_ON_PAGE_QUANTITY == 0) {
writeMessage("");
do {
writeMessage("For showing next page press \"Enter\"");
} while (!readString().isEmpty());
}
}
}

/**
* Ask user to confirm operation.
*
* @param message to be shown for user.
* @return true if confirms or false otherwise.
*/
@Override
public boolean confirmAction(String message) {
writeMessage("You are going to:");
writeMessage(message);
writeMessage("");
do {
writeMessage("Enter \"y\" for \"yes\" or \"n\" for \"no\"");
String entry = readString();
if (entry.equalsIgnoreCase("y")) {
return true;
}
if (entry.equalsIgnoreCase("n")) {
return false;
}
} while (true);
}
}
Loading