|
@@ -7,39 +7,71 @@ public class Client extends Thread {
|
|
private Socket socket;
|
|
private Socket socket;
|
|
private InputStream is;
|
|
private InputStream is;
|
|
private OutputStream os;
|
|
private OutputStream os;
|
|
|
|
+ private boolean authorized;
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
* @param socket сокет клиента
|
|
* @param socket сокет клиента
|
|
*/
|
|
*/
|
|
public Client(Socket socket) throws IOException {
|
|
public Client(Socket socket) throws IOException {
|
|
this.socket = socket;
|
|
this.socket = socket;
|
|
this.is = this.socket.getInputStream();
|
|
this.is = this.socket.getInputStream();
|
|
this.os = this.socket.getOutputStream();
|
|
this.os = this.socket.getOutputStream();
|
|
|
|
+ authorized = false;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
while (!isInterrupted()) {
|
|
while (!isInterrupted()) {
|
|
try {
|
|
try {
|
|
|
|
+ // Принимаем сообщение от клиента
|
|
|
|
+
|
|
ObjectInputStream objectInputStream = new ObjectInputStream(is);
|
|
ObjectInputStream objectInputStream = new ObjectInputStream(is);
|
|
|
|
+ ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
|
|
|
|
|
|
- UserMessage userMessage = (UserMessage)objectInputStream.readObject();
|
|
|
|
|
|
+ UserMessage userMessage = (UserMessage) objectInputStream.readObject();
|
|
switch (userMessage.TYPE) {
|
|
switch (userMessage.TYPE) {
|
|
|
|
+ case AUTHORIZATION:
|
|
|
|
+ authorized = Bank.authorization(userMessage.BILL_NUMBER, userMessage.PIN);
|
|
|
|
+ break;
|
|
|
|
+ case SIGN_CONTRACT:
|
|
|
|
+ SecretData secretData = Bank.insert(userMessage.PASSPORT, userMessage.PHONE, userMessage.FULLNAME, userMessage.BALANCE);
|
|
|
|
+ objectOutputStream.writeObject(secretData);
|
|
|
|
+ objectOutputStream.flush();
|
|
|
|
+ continue;
|
|
|
|
+ case BREAK_CONTRACT:
|
|
|
|
+ if (!authorized) {
|
|
|
|
+ System.out.println("Для совершения операций со счётом необходимо авторизоваться");
|
|
|
|
+ objectOutputStream.writeChars("Действие не выполнено! Необходимо авторизоваться.");
|
|
|
|
+ objectOutputStream.flush();
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ Bank.delete(userMessage.BILL_NUMBER);
|
|
|
|
+ break;
|
|
case DEPOSIT:
|
|
case DEPOSIT:
|
|
|
|
+ if (!authorized) {
|
|
|
|
+ System.out.println("Для совершения операций со счётом необходимо авторизоваться");
|
|
|
|
+ objectOutputStream.writeChars("Действие не выполнено! Необходимо авторизоваться.");
|
|
|
|
+ objectOutputStream.flush();
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
Bank.deposit(userMessage.BALANCE, userMessage.BILL_NUMBER);
|
|
Bank.deposit(userMessage.BALANCE, userMessage.BILL_NUMBER);
|
|
break;
|
|
break;
|
|
case WITHDRAW:
|
|
case WITHDRAW:
|
|
|
|
+ if (!authorized) {
|
|
|
|
+ System.out.println("Для совершения операций со счётом необходимо авторизоваться");
|
|
|
|
+ objectOutputStream.writeChars("Действие не выполнено! Необходимо авторизоваться.");
|
|
|
|
+ objectOutputStream.flush();
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
Bank.withdraw(userMessage.BALANCE, userMessage.BILL_NUMBER);
|
|
Bank.withdraw(userMessage.BALANCE, userMessage.BILL_NUMBER);
|
|
break;
|
|
break;
|
|
- case SIGN_CONTRACT:
|
|
|
|
- Bank.insert(userMessage.PASSPORT, userMessage.PHONE, userMessage.FULLNAME, userMessage.BALANCE);
|
|
|
|
- break;
|
|
|
|
- case BREAK_CONTRACT:
|
|
|
|
- Bank.delete(userMessage.BILL_NUMBER);
|
|
|
|
- break;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Посылаем ответ с результатом работы для не обработанных действий
|
|
|
|
+
|
|
|
|
+ objectOutputStream.writeChars("Действие успешно выполнено");
|
|
|
|
+ objectOutputStream.flush();
|
|
|
|
+
|
|
} catch (ClassNotFoundException | IOException exception) {
|
|
} catch (ClassNotFoundException | IOException exception) {
|
|
System.out.println(exception.getMessage());
|
|
System.out.println(exception.getMessage());
|
|
interrupt();
|
|
interrupt();
|