/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
import java.util.logging.Level;
import java.util.logging.Logger;
class Main {
public static final String AUSTIN_POWERS
= "Austin Powers"; public static final String WEAPONS
= "weapons"; public static final String BANNED_SUBSTANCE
= "banned substance";
//Stepik code: start
public static class UntrustworthyMailWorker implements MailService {
private RealMailService innerRealMailService = new RealMailService();
private final MailService[] mailServices;
UntrustworthyMailWorker(MailService[] mailServices) {
this.mailServices = mailServices;
}
public Sendable processMail(Sendable mail) {
Sendable temp = mail;
for (MailService mailService: mailServices) {
temp = mailService.processMail(temp);
}
return innerRealMailService.processMail(temp);
}
public RealMailService getRealMailService() {
return innerRealMailService;
}
}
public static class Spy implements MailService{
public Logger logger;
Spy(Logger logger) {
this.logger = logger;
}
public Sendable processMail(Sendable anMail) {
if (anMail instanceof MailMessage) {
MailMessage mail = (MailMessage) anMail;
if (mail.getFrom().equals(AUSTIN_POWERS) || mail.getTo().equals(AUSTIN_POWERS)) {
logger.
log(Level.
WARNING,
"Detected target mail correspondence: from {0} to {1} \"{2}\")",
new Object[] {mail.
getFrom(), mail.
getTo(), mail.
getMessage()}); } else {
logger.
log(Level.
INFO,
"Usual correspondence: from {0} to {1}",
new Object[] {mail.
getFrom(), mail.
getTo()}); }
}
return anMail;
}
}
public static class Thief implements MailService {
private int minimal, summary = 0;
Thief(int minimal) {
this.minimal = minimal;
}
public int getStolenValue() {
return summary;
}
public Sendable processMail(Sendable anMail) {
if (anMail instanceof MailPackage) {
MailPackage mail = (MailPackage) anMail;
if (mail.getContent().getPrice() >= minimal) {
summary += mail.getContent().getPrice();
return new MailPackage(anMail.getFrom(), anMail.getTo(), new Package("stones instead of " + mail.getContent().getContent(), 0));
}
}
return anMail;
}
}
public IllegalPackageException
(String message
) { super(message);
}
super(message, cause);
}
}
public StolenPackageException
(String message
) { super(message);
}
super(message, cause);
}
}
public static class Inspector implements MailService {
public Sendable processMail(Sendable anMail) {
if (anMail instanceof MailPackage) {
MailPackage mail = (MailPackage) anMail;
if (mail.getContent().getContent().contains(WEAPONS) || mail.getContent().getContent().contains(BANNED_SUBSTANCE)) {
throw new IllegalPackageException(mail.getContent().getContent());
}
if (mail.getContent().getContent().contains("stones")) {
throw new StolenPackageException(mail.getContent().getContent());
}
} else if (anMail instanceof MailMessage) {
MailMessage mail = (MailMessage) anMail;
if (mail.getMessage().contains(WEAPONS) || mail.getMessage().contains(BANNED_SUBSTANCE)) {
throw new IllegalPackageException(mail.getMessage());
}
if (mail.getMessage().contains("stones")) {
throw new StolenPackageException(mail.getMessage());
}
}
return anMail;
}
}
//Тут Писать свой КОД
//Stepik code: end
public static void main
(String[] args
) { Logger logger = Logger.getLogger(Main.class.getName());
Inspector inspector = new Inspector();
Spy spy = new Spy(logger);
Thief thief = new Thief(10000);
UntrustworthyMailWorker worker = new UntrustworthyMailWorker(new MailService[]{spy, thief, inspector});
AbstractSendable[] correspondence = {
new MailMessage("Oxxxymiron", "Гнойный", " Я здесь чисто по фану, поглумиться над слабым\n" +
"Ты же вылез из мамы под мой дисс на Бабана...."),
new MailMessage("Гнойный", "Oxxxymiron", "....Что? Так болел за Россию, что на нервах терял ганглии.\n" +
"Но когда тут проходили митинги, где ты сидел? В Англии!...."),
new MailMessage("Жириновский", AUSTIN_POWERS, "Бери пацанов, и несите меня к воде."),
new MailMessage(AUSTIN_POWERS, "Пацаны", "Го, потаскаем Вольфовича как Клеопатру"),
new MailPackage("берег", "море", new Package("ВВЖ", 32)),
new MailMessage("NASA", AUSTIN_POWERS, "Найди в России ракетные двигатели и лунные stones"),
new MailPackage(AUSTIN_POWERS, "NASA", new Package("ракетный двигатель ", 2500000)),
new MailPackage(AUSTIN_POWERS, "NASA", new Package("stones ", 1000)),
new MailPackage("Китай", "КНДР", new Package("banned substance ", 10000)),
new MailPackage(AUSTIN_POWERS, "Жопа запрещенная группировка", new Package("tiny bomb", 9000)),
new MailMessage(AUSTIN_POWERS, "Психиатр", "Помогите"),
};
for (AbstractSendable p : correspondence) {
try {
print("До: ", p);
Sendable sendable = worker.processMail(p);
print("После: ", sendable);
} catch (StolenPackageException | IllegalPackageException e) {
logger.log(Level.WARNING, "из: " + p.getFrom() + " куда: " + p.getTo() + " Содержимое: "
+ (p instanceof MailMessage ? ((MailMessage) p).getMessage() : ((MailPackage) p).getContent().getContent()
+ " Цена=" + ((MailPackage) p).getContent().getPrice()) + " Exceptions: " + e);
}
}
}
public static void print
(String prefix, Sendable p
) { System.
out.
println(prefix
+ "из: " + p.
getFrom() + " куда: " + p.
getTo() + " Содержимое: " + (p instanceof MailMessage ? ((MailMessage) p).getMessage() : ((MailPackage) p).getContent().getContent()
+ " Цена=" + ((MailPackage) p).getContent().getPrice()));
}
/*
Интерфейс: сущность, которую можно отправить по почте.
У такой сущности можно получить от кого и кому направляется письмо.
*/
public static interface Sendable {
}
/*
Абстрактный класс,который позволяет абстрагировать логику хранения
источника и получателя письма в соответствующих полях класса.
*/
public static abstract class AbstractSendable implements Sendable {
this.from = from;
this.to = to;
}
@Override
return from;
}
@Override
return to;
}
@Override
public boolean equals
(Object o
) { if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractSendable that = (AbstractSendable) o;
if (!from.equals(that.from)) return false;
if (!to.equals(that.to)) return false;
return true;
}
}
/*
Письмо, у которого есть текст, который можно получить с помощью метода `getMessage`
*/
public static class MailMessage extends AbstractSendable {
super(from, to);
this.message = message;
}
return message;
}
@Override
public boolean equals
(Object o
) { if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
MailMessage that = (MailMessage) o;
if (message != null ? !message.equals(that.message) : that.message != null) return false;
return true;
}
}
/*
Посылка, содержимое которой можно получить с помощью метода `getContent`
*/
public static class MailPackage extends AbstractSendable {
private final Package content;
public MailPackage
(String from,
String to,
Package content
) { super(from, to);
this.content = content;
}
public Package getContent() {
return content;
}
@Override
public boolean equals
(Object o
) { if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
MailPackage that = (MailPackage) o;
if (!content.equals(that.content)) return false;
return true;
}
}
/*
Класс, который задает посылку. У посылки есть текстовое описание содержимого и целочисленная ценность.
*/
public static class Package {
private final int price;
public Package(String content,
int price
) { this.content = content;
this.price = price;
}
return content;
}
public int getPrice() {
return price;
}
@Override
public boolean equals
(Object o
) { if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Package aPackage = (Package) o;
if (price != aPackage.price) return false;
if (!content.equals(aPackage.content)) return false;
return true;
}
}
/*
Интерфейс, который задает класс, который может каким-либо образом обработать почтовый объект.
*/
public static interface MailService {
Sendable processMail(Sendable mail);
}
/*
Класс, в котором скрыта логика настоящей почты
*/
public static class RealMailService implements MailService {
@Override
public Sendable processMail(Sendable mail) {
// Здесь описан код настоящей системы отправки почты.
return mail;
}
}
}