Well, I feel I've done everything expected, but the validator doesn't believe I've met any of the requirements, beginning with the very declaration of the ClientGuiController class. I'm really at a loss, as I was pretty confident about this code:). What am I missing?
package com.codegym.task.task30.task3008.client;
import com.codegym.task.task30.task3008.ConsoleHelper;
import com.codegym.task.task30.task3008.Message;
import com.codegym.task.task30.task3008.MessageType;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.ConsoleHandler;
public class BotClient extends Client {
// Override the following methods:
// a) getSocketThread(). It should create and return a BotSocketThread object.
@Override
protected SocketThread getSocketThread() {
return new BotSocketThread();
}
// b) shouldSendTextFromConsole(). It should always return false.
// We don't want the bot to send text entered from the console.
@Override
protected boolean shouldSendTextFromConsole() {
return false;
}
// c) getUserName(). This method should generate a new bot name, for example,
// date_bot_X, where X is any integer number from 0 to 99.
// Use the Math.random() method to generate X.
@Override
public String getUserName() {
int X = (int) (Math.random() * 100);
return "date_bot_" + X;
}
// 4) Add the main() method.
// It should create a new BotClient object and call its run() method.
public static void main(String[] args) throws IOException {
BotClient botClient = new BotClient();
botClient.run();
}
public class BotSocketThread extends SocketThread {
//1) Override the clientMainLoop() method:
@Override
protected void clientMainLoop() throws IOException, ClassNotFoundException {
// a) Use the sendTextMessage() method to send this message:
sendTextMessage("Hello, there. I'm a bot. I understand the following commands: date, day, month, year, time, hour, minutes, seconds.");
// b) Call the parent class's clientMainLoop() method.
super.clientMainLoop();
}
@Override
protected void processIncomingMessage(String message) {
// 2) Override the processIncomingMessage(String message) method.
// It should process incoming messages as follows:
// a) Display the text of the received message on the console.
/*String received = null;
try {
received = connection.receive().getData();
ConsoleHelper.writeMessage(received);
} catch (IOException | ClassNotFoundException e) {
}*/
String received = message;
ConsoleHelper.writeMessage(received);
// b) Get the sender's name and the message text from the message.
// They are separated by ": ".
if (!received.contains(": ")) return;
String[] messageData = message.split(": ");
String name = messageData[0];
String text = messageData[1];
// c) Send a reply based on the text of the received message.
String pattern = null;
// If the text of the message is:
// "date" - send a message containing the current date in the format "d.MM.YYYY";
if (text.equals("date")) pattern = "d.MM.YYYY";
// "day" - in the format "d"; "month" - "MMMM";
else if (text.equals("day")) pattern = "d";
else if (text.equals("month")) pattern = "MMMM";
// "year" - "YYYY"; "time" - "H:mm:ss"; "hour" - "H";
else if (text.equals("year")) pattern = "YYYY";
else if (text.equals("time")) pattern = "H:mm:ss";
else if (text.equals("hour")) pattern = "H";
// "minutes" - "m"; "seconds" - "s".
else if (text.equals("minutes")) pattern = "m";
else if (text.equals("seconds")) pattern = "s";
else return;
// Use the format above to create a SimpleDateFormat object.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
// To get the current date, you must use the Calendar class and getTime() method.
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
String formattedDate = simpleDateFormat.format(currentDate);
// The response must include the name of the client
// who sent the request and is waiting for a response,
// e.g. if Bob sent the "time" request, we should send the following response:
// "Information for Bob: 12:30:47".
sendTextMessage("Information for " + name + ": " + formattedDate);
}
}
}