diff --git a/src/main/java/org/sadtech/bot/core/domain/money/Account.java b/src/main/java/org/sadtech/bot/core/domain/money/Account.java new file mode 100644 index 0000000..fa8ab67 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/money/Account.java @@ -0,0 +1,78 @@ +package org.sadtech.bot.core.domain.money; + +import java.util.Objects; + +public class Account { + + private Integer id; + private Double totalSum; + private Integer belongsPersonId; + private Integer extinguishedPersonId; + private String description; + private AccountStatus accountStatus; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Double getTotalSum() { + return totalSum; + } + + public void setTotalSum(Double totalSum) { + this.totalSum = totalSum; + } + + public Integer getBelongsPersonId() { + return belongsPersonId; + } + + public void setBelongsPersonId(Integer belongsPersonId) { + this.belongsPersonId = belongsPersonId; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public AccountStatus getAccountStatus() { + return accountStatus; + } + + public void setAccountStatus(AccountStatus accountStatus) { + this.accountStatus = accountStatus; + } + + public Integer getExtinguishedPersonId() { + return extinguishedPersonId; + } + + public void setExtinguishedPersonId(Integer extinguishedPersonId) { + this.extinguishedPersonId = extinguishedPersonId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Account)) return false; + Account account = (Account) o; + return Objects.equals(id, account.id) && + Objects.equals(totalSum, account.totalSum) && + Objects.equals(belongsPersonId, account.belongsPersonId) && + Objects.equals(description, account.description) && + accountStatus == account.accountStatus; + } + + @Override + public int hashCode() { + return Objects.hash(id, totalSum, belongsPersonId, description, accountStatus); + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java b/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java new file mode 100644 index 0000000..bbd8622 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java @@ -0,0 +1,7 @@ +package org.sadtech.bot.core.domain.money; + +public enum AccountStatus { + + EXPOSED, CLOSED, CANCELLED, EXCEPTION; + +} diff --git a/src/main/java/org/sadtech/bot/core/exception/AppBotException.java b/src/main/java/org/sadtech/bot/core/exception/AppBotException.java new file mode 100644 index 0000000..6486ade --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/exception/AppBotException.java @@ -0,0 +1,37 @@ +package org.sadtech.bot.core.exception; + +import java.time.LocalDateTime; + +public class AppBotException extends RuntimeException { + + private final String type = "ERROR"; + private final LocalDateTime timeError = LocalDateTime.now(); + protected Integer code; + protected String description; + + public AppBotException(String message, Integer code) { + super(message); + this.description = message; + this.code = code; + } + + public AppBotException(Integer code) { + this.code = code; + } + + public String getType() { + return type; + } + + public LocalDateTime getTimeError() { + return timeError; + } + + public Integer getCode() { + return code; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/org/sadtech/bot/core/exception/MailSendException.java b/src/main/java/org/sadtech/bot/core/exception/MailSendException.java index f627380..60c36a8 100644 --- a/src/main/java/org/sadtech/bot/core/exception/MailSendException.java +++ b/src/main/java/org/sadtech/bot/core/exception/MailSendException.java @@ -1,5 +1,10 @@ package org.sadtech.bot.core.exception; -public class MailSendException extends RuntimeException { +public class MailSendException extends AppBotException { + public final static Integer CODE = 1; + + public MailSendException() { + super(CODE); + } } diff --git a/src/main/java/org/sadtech/bot/core/exception/PaymentException.java b/src/main/java/org/sadtech/bot/core/exception/PaymentException.java new file mode 100644 index 0000000..47dfe66 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/exception/PaymentException.java @@ -0,0 +1,13 @@ +package org.sadtech.bot.core.exception; + +public class PaymentException extends AppBotException { + + public PaymentException(String message, Integer code) { + super(message, code); + } + + public PaymentException(Integer code) { + super(code); + } + +} diff --git a/src/main/java/org/sadtech/bot/core/repository/AccountRepository.java b/src/main/java/org/sadtech/bot/core/repository/AccountRepository.java new file mode 100644 index 0000000..5c89e8a --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/repository/AccountRepository.java @@ -0,0 +1,13 @@ +package org.sadtech.bot.core.repository; + +import org.sadtech.bot.core.domain.money.Account; + +public interface AccountRepository { + + Integer add(Account account); + + void edit(Integer accountId, Account account); + + Account findById(Integer accountId); + +} diff --git a/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryImpl.java b/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryImpl.java new file mode 100644 index 0000000..8742c34 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryImpl.java @@ -0,0 +1,31 @@ +package org.sadtech.bot.core.repository.impl; + +import org.sadtech.bot.core.domain.money.Account; +import org.sadtech.bot.core.repository.AccountRepository; + +import java.util.HashMap; +import java.util.Map; + +public class AccountRepositoryImpl implements AccountRepository { + + private final Map saveMap = new HashMap<>(); + private Integer id = 0; + + @Override + public Integer add(Account account) { + account.setId(id); + saveMap.put(id, account); + return id++; + } + + @Override + public void edit(Integer accountId, Account account) { + account.setId(accountId); + saveMap.put(accountId, account); + } + + @Override + public Account findById(Integer accountId) { + return saveMap.get(accountId); + } +} diff --git a/src/main/java/org/sadtech/bot/core/repository/impl/TerminalComandRepositoryQueue.java b/src/main/java/org/sadtech/bot/core/repository/impl/TerminalComandRepositoryQueue.java deleted file mode 100644 index 3e6b681..0000000 --- a/src/main/java/org/sadtech/bot/core/repository/impl/TerminalComandRepositoryQueue.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.sadtech.bot.core.repository.impl; - -import org.sadtech.bot.core.domain.Mail; -import org.sadtech.bot.core.repository.EventRepository; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; - -public class TerminalComandRepositoryQueue implements EventRepository { - - private final Queue mailQueue = new ConcurrentLinkedQueue<>(); - - @Override - public void add(Mail dataObject) { - mailQueue.offer(dataObject); - } - - @Override - public void cleanAll() { - mailQueue.clear(); - } - - @Override - public Queue getEventQueue() { - return mailQueue; - } -} diff --git a/src/main/java/org/sadtech/bot/core/service/AccountService.java b/src/main/java/org/sadtech/bot/core/service/AccountService.java new file mode 100644 index 0000000..4a1bae7 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/service/AccountService.java @@ -0,0 +1,13 @@ +package org.sadtech.bot.core.service; + +import org.sadtech.bot.core.domain.money.Account; + +public interface AccountService { + + Integer add(Account account); + + Boolean pay(Integer accountId, Integer extinguishedPersonId, Double sum); + + Boolean paymentVerification(Integer accountId); + +} diff --git a/src/main/java/org/sadtech/bot/core/service/PersonService.java b/src/main/java/org/sadtech/bot/core/service/PersonService.java index 21adbd9..2c27e86 100644 --- a/src/main/java/org/sadtech/bot/core/service/PersonService.java +++ b/src/main/java/org/sadtech/bot/core/service/PersonService.java @@ -8,7 +8,7 @@ public interface PersonService { Person get(Integer id); - boolean checkPerson(Integer idPerson); + boolean checkPerson(Integer personId); Person createPerson(Integer userId); diff --git a/src/main/java/org/sadtech/bot/core/service/impl/AccountServiceImpl.java b/src/main/java/org/sadtech/bot/core/service/impl/AccountServiceImpl.java new file mode 100644 index 0000000..4df8ca8 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/service/impl/AccountServiceImpl.java @@ -0,0 +1,42 @@ +package org.sadtech.bot.core.service.impl; + +import org.sadtech.bot.core.domain.money.Account; +import org.sadtech.bot.core.domain.money.AccountStatus; +import org.sadtech.bot.core.exception.PaymentException; +import org.sadtech.bot.core.repository.AccountRepository; +import org.sadtech.bot.core.service.AccountService; + +public class AccountServiceImpl implements AccountService { + + private final AccountRepository accountRepository; + + public AccountServiceImpl(AccountRepository accountRepository) { + this.accountRepository = accountRepository; + } + + @Override + public Integer add(Account account) { + account.setAccountStatus(AccountStatus.EXPOSED); + return accountRepository.add(account); + } + + @Override + public Boolean pay(Integer accountId, Integer extinguishedPersonId, Double sum) { + Account account = accountRepository.findById(accountId); + if (account.getTotalSum().equals(sum)) { + account.setAccountStatus(AccountStatus.CLOSED); + account.setExtinguishedPersonId(extinguishedPersonId); + accountRepository.edit(accountId, account); + } else { + account.setAccountStatus(AccountStatus.EXCEPTION); + accountRepository.edit(accountId, account); + throw new PaymentException("Неверная сумма", 2); + } + return true; + } + + @Override + public Boolean paymentVerification(Integer accountId) { + return AccountStatus.CLOSED.equals(accountRepository.findById(accountId).getAccountStatus()); + } +} diff --git a/src/main/java/org/sadtech/bot/core/sender/Sent.java b/src/main/java/org/sadtech/bot/core/service/sender/Sent.java similarity index 80% rename from src/main/java/org/sadtech/bot/core/sender/Sent.java rename to src/main/java/org/sadtech/bot/core/service/sender/Sent.java index 120e56c..afab375 100644 --- a/src/main/java/org/sadtech/bot/core/sender/Sent.java +++ b/src/main/java/org/sadtech/bot/core/service/sender/Sent.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.sender; +package org.sadtech.bot.core.service.sender; import org.sadtech.bot.core.domain.BoxAnswer; diff --git a/src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java b/src/main/java/org/sadtech/bot/core/service/sender/email/EmailConfig.java similarity index 97% rename from src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java rename to src/main/java/org/sadtech/bot/core/service/sender/email/EmailConfig.java index abfbefe..974fce5 100644 --- a/src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java +++ b/src/main/java/org/sadtech/bot/core/service/sender/email/EmailConfig.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.sender.email; +package org.sadtech.bot.core.service.sender.email; import java.util.Properties; diff --git a/src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java b/src/main/java/org/sadtech/bot/core/service/sender/email/EmailSent.java similarity index 93% rename from src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java rename to src/main/java/org/sadtech/bot/core/service/sender/email/EmailSent.java index 9f20425..f746f00 100644 --- a/src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java +++ b/src/main/java/org/sadtech/bot/core/service/sender/email/EmailSent.java @@ -1,8 +1,8 @@ -package org.sadtech.bot.core.sender.email; +package org.sadtech.bot.core.service.sender.email; import org.sadtech.bot.core.domain.BoxAnswer; import org.sadtech.bot.core.exception.MailSendException; -import org.sadtech.bot.core.sender.Sent; +import org.sadtech.bot.core.service.sender.Sent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/org/sadtech/bot/core/insert/InsertWords.java b/src/main/java/org/sadtech/bot/core/utils/InsertWords.java similarity index 95% rename from src/main/java/org/sadtech/bot/core/insert/InsertWords.java rename to src/main/java/org/sadtech/bot/core/utils/InsertWords.java index 687d9a7..0dab215 100644 --- a/src/main/java/org/sadtech/bot/core/insert/InsertWords.java +++ b/src/main/java/org/sadtech/bot/core/utils/InsertWords.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.insert; +package org.sadtech.bot.core.utils; import java.util.List; import java.util.regex.Matcher; diff --git a/src/main/java/org/sadtech/bot/core/service/KeyBoards.java b/src/main/java/org/sadtech/bot/core/utils/KeyBoards.java similarity index 97% rename from src/main/java/org/sadtech/bot/core/service/KeyBoards.java rename to src/main/java/org/sadtech/bot/core/utils/KeyBoards.java index 4c04ed7..cb878f5 100644 --- a/src/main/java/org/sadtech/bot/core/service/KeyBoards.java +++ b/src/main/java/org/sadtech/bot/core/utils/KeyBoards.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.service; +package org.sadtech.bot.core.utils; import org.sadtech.bot.core.domain.keyboard.ButtonColor; import org.sadtech.bot.core.domain.keyboard.KeyBoard;