Я пытаюсь сохранить количество раз, когда любой клиент запрашивает описание у класса ServerProtocol.
В настоящее время счетчик будет увеличиваться с нуля каждый раз, когда присоединяется новый клиент. Есть идеи?
Счетчик класса:
public class Counter {
private int counter;
public synchronized int get() {
return counter;
}
public synchronized void set(int n) {
counter = n;
}
public synchronized void increment() {
set(get() + 1);
}
}
Фрагмент из класса ServerProtocol:
case OPTIONS:
if (theInput.equals("1")) {
theOutput = "computer program description here -- Another? Y or N";
counter.increment();
System.out.println(counter.get());
state = ANOTHER;
Приведенный выше метод println выводит текущее значение счетчика на терминал в классе сервера:
Класс ServerProtocol:
public class ServerProtocol {
private static final int TERMS = 0;
private static final int ACCEPTTERMS = 1;
private static final int ANOTHER = 2;
private static final int OPTIONS = 3;
private int state = TERMS;
public String processInput(String theInput) {
String theOutput = null;
Counter counter = new Counter();
switch (state) {
case TERMS:
theOutput = "Terms of reference. Do you accept? Y or N";
state = ACCEPTTERMS;
break;
case ACCEPTTERMS:
if (theInput.equalsIgnoreCase("y")) {
theOutput = "1. computer program 2. picture 3. e-book";
state = OPTIONS;
} else if (theInput.equalsIgnoreCase("n")) {
theOutput = "Bye.";
} else {
theOutput = "Invalid Entry -- Terms of reference. Do you accept? Y or N";
state = ACCEPTTERMS;
}
break;
case ANOTHER:
if (theInput.equalsIgnoreCase("y")) {
theOutput = "1. computer program 2. picture 3. e-book";
state = OPTIONS;
} else if (theInput.equalsIgnoreCase("n")) {
theOutput = "Bye.";
} else {
theOutput = "Invalid Entry -- Another? Y or N";
state = ACCEPTTERMS;
}
break;
case OPTIONS:
if (theInput.equals("1")) {
theOutput = "computer program description here -- Another? Y or N";
counter.increment();
counter.get();
state = ANOTHER;
} else if (theInput.equals("2")) {
theOutput = "picture description here -- Another? Y or N";
state = ANOTHER;
} else if (theInput.equals("3")) {
theOutput = "e-book description here -- Another? Y or N";
state = ANOTHER;
} else {
theOutput = "Invalid Entry -- 1. computer program 2. picture 3. e-book";
state = OPTIONS;
}
break;
default:
System.out.println("Oops");
}
return theOutput;
}
}