From 7155e5ae51ca6e1c8f78c80a8433e0b31938ea0c Mon Sep 17 00:00:00 2001 From: "A.Klivtsov" Date: Fri, 26 Dec 2025 21:59:02 +0300 Subject: [PATCH] upd --- src/Main.java | 66 ++++++++++++++++++- src/exceptions/CannotBeHidedException.java | 6 +- src/exceptions/CustomException.java | 13 ++++ src/exceptions/ForbiddenTargetException.java | 6 +- src/exceptions/GuiltyPoliceException.java | 7 ++ .../IllegalWeaponUsageException.java | 7 +- .../SuspiciousItemNotFoundException.java | 8 +-- .../UnpreparedRobberyException.java | 8 +-- src/exceptions/VehicleDriverException.java | 7 +- src/exceptions/VehicleIsBrokenException.java | 8 +-- src/items/Item.java | 27 ++++++-- src/items/MoneyBag.java | 11 +--- src/items/Vehicle.java | 14 ++-- src/items/Weapon.java | 33 ++++------ .../{CanBeShooted.java => ShootableItem.java} | 4 +- src/persons/Bandit.java | 9 +-- src/persons/Commissioner.java | 4 +- src/persons/Person.java | 22 +++++-- src/persons/Policeman.java | 14 ++-- src/persons/interfaces/Manhuntable.java | 5 +- src/persons/interfaces/ShootablePerson.java | 9 +++ 21 files changed, 183 insertions(+), 105 deletions(-) create mode 100644 src/exceptions/CustomException.java create mode 100644 src/exceptions/GuiltyPoliceException.java rename src/items/interfaces/{CanBeShooted.java => ShootableItem.java} (65%) create mode 100644 src/persons/interfaces/ShootablePerson.java diff --git a/src/Main.java b/src/Main.java index d2acb63..4cbc340 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,8 +1,13 @@ +import exceptions.IllegalWeaponUsageException; import items.Item; +import items.MoneyBag; +import items.Vehicle; import items.Weapon; +import items.enums.VehicleType; import items.enums.WeaponType; import persons.Bandit; import persons.Commissioner; +import persons.Person; import persons.Policeman; import persons.enums.Role; @@ -14,18 +19,77 @@ public class Main { Bandit A = new Bandit("A", 27); Policeman B = new Policeman("B", 28); Commissioner C = new Commissioner("C", 30); - C.addItem(new Weapon(C, WeaponType.PISTOL)); + + Vehicle car1 = new Vehicle(B, false, VehicleType.POLICE_CAR, B, "Грабитель"); + Vehicle car2 = new Vehicle(A, false, VehicleType.BANDIT_CAR, A, "Скрыться"); + + Weapon gun = new Weapon(B, WeaponType.PISTOL); + B.addItem(gun); + + car1.setPassengers(C); A.planRobbery(); A.executeRobbery(); + MoneyBag bag = new MoneyBag(A); + car2.setTrunk(bag); + bag.setIsLost(true); + + System.out.println(); + B.pursue(A); B.arrest(A); + System.out.println(); + + while (bag.getIsLost()) { + B.searchVehicle(car2); + } + + + // ломаем машину и чиним её + System.out.println(); + + int c = 0; + while (!car2.getIsBroken()) { + System.out.println(" ---- try %d ----".formatted(c)); + System.out.println(car2.getDurability() + " " + car2.getIsBroken()); + gun.shoot(B, car2); + c++; + System.out.println(" --------------- "); + } + + while (car2.getIsBroken()) { + car2.fix(); + } + + // ломаем человека и лечим его + A.setDisguiseAs(Role.BANDIT); + gun.shoot(B, A); + System.out.println(A); + A.setHurt(false); + + // ломаем сумку и чиним её + System.out.println(); + + gun.shoot(B, bag); + while (!bag.getIsBroken()) { + bag.fix(); + } + + // ловим ошибки + try { + gun.shoot(A, B); + } catch (IllegalWeaponUsageException e) { + System.out.println(e.getMessage()); + } + + System.out.println(); while (!A.getGuilty()) { C.interrogate(A); } + System.out.println(); C.conclude(A); } } \ No newline at end of file diff --git a/src/exceptions/CannotBeHidedException.java b/src/exceptions/CannotBeHidedException.java index 0bc27cb..4b22f39 100644 --- a/src/exceptions/CannotBeHidedException.java +++ b/src/exceptions/CannotBeHidedException.java @@ -1,12 +1,8 @@ package exceptions; -public class CannotBeHidedException extends RuntimeException { +public class CannotBeHidedException extends CustomException { public CannotBeHidedException(String message) { super(message); } - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } } diff --git a/src/exceptions/CustomException.java b/src/exceptions/CustomException.java new file mode 100644 index 0000000..cd72c69 --- /dev/null +++ b/src/exceptions/CustomException.java @@ -0,0 +1,13 @@ +package exceptions; + +public class CustomException extends RuntimeException { + public CustomException(String message) { + super(message); + } + + @Override + public String getMessage() { + return "uh... " + super.getMessage(); + } + +} diff --git a/src/exceptions/ForbiddenTargetException.java b/src/exceptions/ForbiddenTargetException.java index 7d7d2e5..c346439 100644 --- a/src/exceptions/ForbiddenTargetException.java +++ b/src/exceptions/ForbiddenTargetException.java @@ -1,12 +1,8 @@ package exceptions; -public class ForbiddenTargetException extends RuntimeException { +public class ForbiddenTargetException extends CustomException { public ForbiddenTargetException(String message) { super(message); } - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } } diff --git a/src/exceptions/GuiltyPoliceException.java b/src/exceptions/GuiltyPoliceException.java new file mode 100644 index 0000000..b1e78d9 --- /dev/null +++ b/src/exceptions/GuiltyPoliceException.java @@ -0,0 +1,7 @@ +package exceptions; + +public class GuiltyPoliceException extends CustomException { + public GuiltyPoliceException(String message) { + super(message); + } +} diff --git a/src/exceptions/IllegalWeaponUsageException.java b/src/exceptions/IllegalWeaponUsageException.java index 3a268ca..b34a932 100644 --- a/src/exceptions/IllegalWeaponUsageException.java +++ b/src/exceptions/IllegalWeaponUsageException.java @@ -1,13 +1,8 @@ package exceptions; -public class IllegalWeaponUsageException extends RuntimeException { +public class IllegalWeaponUsageException extends CustomException { public IllegalWeaponUsageException(String message) { super(message); } - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } - } diff --git a/src/exceptions/SuspiciousItemNotFoundException.java b/src/exceptions/SuspiciousItemNotFoundException.java index b4078e6..a16c85f 100644 --- a/src/exceptions/SuspiciousItemNotFoundException.java +++ b/src/exceptions/SuspiciousItemNotFoundException.java @@ -1,13 +1,7 @@ package exceptions; -public class SuspiciousItemNotFoundException extends RuntimeException { +public class SuspiciousItemNotFoundException extends CustomException { public SuspiciousItemNotFoundException() { super("Money bag not found"); } - - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } - } diff --git a/src/exceptions/UnpreparedRobberyException.java b/src/exceptions/UnpreparedRobberyException.java index 2727fa5..e8a57e9 100644 --- a/src/exceptions/UnpreparedRobberyException.java +++ b/src/exceptions/UnpreparedRobberyException.java @@ -1,13 +1,7 @@ package exceptions; -public class UnpreparedRobberyException extends RuntimeException { +public class UnpreparedRobberyException extends CustomException { public UnpreparedRobberyException(String message) { super(message); } - - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } - } diff --git a/src/exceptions/VehicleDriverException.java b/src/exceptions/VehicleDriverException.java index 28d9b87..4b22fd7 100644 --- a/src/exceptions/VehicleDriverException.java +++ b/src/exceptions/VehicleDriverException.java @@ -1,13 +1,8 @@ package exceptions; -public class VehicleDriverException extends Exception { +public class VehicleDriverException extends CustomException { public VehicleDriverException(String message) { super(message); } - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } - } diff --git a/src/exceptions/VehicleIsBrokenException.java b/src/exceptions/VehicleIsBrokenException.java index 02e424e..7df0dd4 100644 --- a/src/exceptions/VehicleIsBrokenException.java +++ b/src/exceptions/VehicleIsBrokenException.java @@ -1,13 +1,7 @@ package exceptions; -public class VehicleIsBrokenException extends RuntimeException { +public class VehicleIsBrokenException extends CustomException { public VehicleIsBrokenException(String message) { super(message); } - - @Override - public String getMessage() { - return "uh..." + super.getMessage(); - } - } diff --git a/src/items/Item.java b/src/items/Item.java index 8465d0e..8d82739 100644 --- a/src/items/Item.java +++ b/src/items/Item.java @@ -1,15 +1,16 @@ package items; -import items.interfaces.CanBeShooted; +import items.interfaces.ShootableItem; import items.interfaces.Suspicious; import persons.Person; import java.util.UUID; -public abstract class Item implements CanBeShooted, Suspicious { +public abstract class Item implements ShootableItem, Suspicious { private double weight; private Person owner; private boolean isBroken; boolean isLost = false; + private int durability = 1; // partNumber shenanigans private final String partNumber = UUID.randomUUID().toString(); @@ -35,7 +36,7 @@ public abstract class Item implements CanBeShooted, Suspicious { @Override public String toString() { - return "%s %f %b".formatted(owner.toString(), weight, isBroken); + return "Вещь [владелец:%s, вес:%f, сломан:%b]".formatted(owner.toString(), weight, isBroken); } // suspicious @@ -62,14 +63,28 @@ public abstract class Item implements CanBeShooted, Suspicious { public void setOwner(Person owner) {this.owner = owner;} // break - public void doBreak() {this.isBroken = !this.isBroken;} + public void doBreak() { + if (this.durability != 0) { + this.durability--; - public boolean fix() { + if (this.durability == 0) { + this.isBroken = true; + } + } else { + this.isBroken = true; + } + } + + public void fix() { if (Math.random() > 0.7 && this.isBroken) { this.isBroken = false; + System.out.println(this.toString() + " удалось починить"); } - return isBroken; + System.out.println(this.toString() + " не удалось починить!"); } public boolean getIsBroken() { return this.isBroken; } + public void setIsBroken(boolean isBroken) {this.isBroken = isBroken; } + + public int getDurability() {return durability;} } diff --git a/src/items/MoneyBag.java b/src/items/MoneyBag.java index d971cca..805fcc5 100644 --- a/src/items/MoneyBag.java +++ b/src/items/MoneyBag.java @@ -1,10 +1,10 @@ package items; -import exceptions.SuspiciousItemNotFoundException; +import items.interfaces.ShootableItem; import items.interfaces.Suspicious; import persons.Person; -public class MoneyBag extends Item implements Suspicious { +public class MoneyBag extends Item implements Suspicious, ShootableItem { private boolean isLost = false; private record Money() { @@ -32,12 +32,7 @@ public class MoneyBag extends Item implements Suspicious { } public boolean getIsLost() { - if (Math.random() > 0.5 && this.isLost) { - this.isLost = false; - return true; - } else { - return false; - } + return this.isLost; } // money diff --git a/src/items/Vehicle.java b/src/items/Vehicle.java index d4812bc..9377273 100644 --- a/src/items/Vehicle.java +++ b/src/items/Vehicle.java @@ -5,19 +5,20 @@ import exceptions.VehicleDriverException; import exceptions.VehicleIsBrokenException; import items.enums.VehicleType; +import items.interfaces.ShootableItem; import persons.Person; import items.interfaces.CanBeInvestigated; import java.util.ArrayList; -public class Vehicle extends Item implements CanBeInvestigated { +public class Vehicle extends Item implements CanBeInvestigated, ShootableItem { private final VehicleType type; private Person driver; private String destination; private boolean isMoving; - private ArrayList passengers; - private ArrayList trunk; + private ArrayList passengers = new ArrayList(); + private ArrayList trunk = new ArrayList(); private int durability; private final int speed; @@ -98,9 +99,14 @@ public class Vehicle extends Item implements CanBeInvestigated { public void doBreak() { if (this.durability > 0) { this.durability--; + + if (this.durability == 0) { + this.setIsBroken(true); + } + } else { this.isMoving = false; - super.doBreak(); + this.setIsBroken(true); } } diff --git a/src/items/Weapon.java b/src/items/Weapon.java index e9e1170..136a515 100644 --- a/src/items/Weapon.java +++ b/src/items/Weapon.java @@ -1,13 +1,11 @@ package items; import exceptions.IllegalWeaponUsageException; -import exceptions.VehicleDriverException; -import items.enums.VehicleType; import items.enums.WeaponType; -import items.interfaces.CanBeShooted; +import items.interfaces.ShootableItem; import items.interfaces.Suspicious; import persons.Person; -import persons.enums.Role; +import persons.interfaces.ShootablePerson; public class Weapon extends Item implements Suspicious { @@ -27,32 +25,27 @@ public class Weapon extends Item implements Suspicious { this.accuracy = type.getAccuracy(); } - public void shoot(Person from, Person to) throws IllegalWeaponUsageException { + public void shoot(Person from, ShootablePerson to) throws IllegalWeaponUsageException { if (from != this.getOwner()) { throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия"); } - - if (to.getDisguiseAs() == Role.CIVILIAN) { - throw new IllegalWeaponUsageException(to.getName() + " является гражданским!"); - } - - if (to.getDisguiseAs() == Role.POLICEMAN || to.getDisguiseAs() == Role.COMMISSIONER) { - throw new IllegalWeaponUsageException(to.getName() + " является полицейским! "); - } - - if (to.getDisguiseAs() == Role.BANDIT) { - to.doHurt(); - System.out.println(from.getName() + " Выстрелил в " + to.getName() + "!"); - } + to.setHurt(true); + System.out.println(from.getName() + " Выстрелил в " + to.getName() + "!"); } - public void shoot(Person from, CanBeShooted to) throws IllegalWeaponUsageException { + public void shoot(Person from, ShootableItem to) throws IllegalWeaponUsageException { if (from != this.getOwner()) { throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия"); } to.doBreak(); - System.out.println(from.getName() + " Выстрелил в " + to.getOwner() + "!"); + System.out.println(from.getName() + " Выстрелил в " + to.getClass() + " владельца " + to.getOwner() + "!"); + + if (to.getDurability() != 0) { + System.out.println("Прочность " + to.getClass() + " владельца " + to.getOwner() + " = " + to.getDurability()); + } else { + System.out.println( to.getClass() + " владельца " + to.getOwner() + " сломан!"); + } } diff --git a/src/items/interfaces/CanBeShooted.java b/src/items/interfaces/ShootableItem.java similarity index 65% rename from src/items/interfaces/CanBeShooted.java rename to src/items/interfaces/ShootableItem.java index b8585aa..c6190cc 100644 --- a/src/items/interfaces/CanBeShooted.java +++ b/src/items/interfaces/ShootableItem.java @@ -2,10 +2,12 @@ package items.interfaces; import persons.Person; -public interface CanBeShooted { +public interface ShootableItem { boolean isBroken = false; + int durability = 1; void doBreak(); boolean getIsBroken(); Person getOwner(); + int getDurability(); } diff --git a/src/persons/Bandit.java b/src/persons/Bandit.java index 664081f..8c58b0a 100644 --- a/src/persons/Bandit.java +++ b/src/persons/Bandit.java @@ -10,10 +10,10 @@ import persons.enums.Role; import persons.interfaces.Manhuntable; public class Bandit extends Person implements Manhuntable { - private boolean isArrested = false; + boolean isArrested = false; + boolean guilty = false; private boolean isInRobbery = false; private boolean isPreparedToRobbery = false; - private boolean guilty = false; public Bandit(String name, Integer age, Role disguiseAs, boolean isHurt) { super(name, age, disguiseAs, isHurt); @@ -67,9 +67,6 @@ public class Bandit extends Person implements Manhuntable { } } - public boolean getIsArrested() {return isArrested;} + public boolean getIsArrested() {return this.isArrested;} - public boolean getIsHurt() { - return super.isHurt(); - } } diff --git a/src/persons/Commissioner.java b/src/persons/Commissioner.java index 40bce54..997c99c 100644 --- a/src/persons/Commissioner.java +++ b/src/persons/Commissioner.java @@ -22,7 +22,7 @@ public class Commissioner extends Policeman { throw new ForbiddenTargetException(target.getName() + " не арестован!"); } - if (target.getIsHurt()) { + if (target.getHurt()) { throw new ForbiddenTargetException(target.getName() + " ранен!"); } @@ -35,7 +35,7 @@ public class Commissioner extends Policeman { throw new ForbiddenTargetException(target.getName() + " отрицает вину"); } - if (target.getIsHurt()) { + if (target.getHurt()) { throw new ForbiddenTargetException(target.getName() + " ранен!"); } diff --git a/src/persons/Person.java b/src/persons/Person.java index 6bc771c..1bd33d3 100644 --- a/src/persons/Person.java +++ b/src/persons/Person.java @@ -2,16 +2,17 @@ package persons; import items.Item; import persons.enums.Role; +import persons.interfaces.ShootablePerson; import java.util.ArrayList; import java.util.UUID; -public abstract class Person { +public abstract class Person implements ShootablePerson { private String name; private Integer age; private Role disguiseAs; private ArrayList items = new ArrayList<>(); - private boolean isHurt; + private boolean hurt = false; // passport shenanigans private final String passport = UUID.randomUUID().toString(); @@ -20,14 +21,14 @@ public abstract class Person { this.name = name; this.age = age; this.disguiseAs = disguiseAs; - this.isHurt = isHurt; + this.hurt = isHurt; } public Person(String name, Integer age) { this.name = name; this.age = age; this.disguiseAs = Role.CIVILIAN; - this.isHurt = false; + this.hurt = false; } @Override @@ -45,9 +46,11 @@ public abstract class Person { @Override public String toString() { - return "%s %d %s %b".formatted(name, age, disguiseAs, isHurt); + return "Человек [имя:%s, возраст:%d, маскировка:%s, ранен:%b]".formatted(name, age, disguiseAs, hurt); } + public Person getPerson() {return this;} + // passport public String getPassport() {return passport;} @@ -69,8 +72,13 @@ public abstract class Person { public void setDisguiseAs(Role disguiseAs) {this.disguiseAs = disguiseAs;} // health - public void doHurt() {this.isHurt = true;} - public boolean isHurt() {return this.isHurt;} + public void setHurt(boolean hurt) { + this.hurt = hurt; + if (!hurt) { + System.out.println(this.name + " вылечен!"); + } + } + public boolean getHurt() {return this.hurt;} //items public void addItem(Item item) {this.items.add(item);} diff --git a/src/persons/Policeman.java b/src/persons/Policeman.java index f0f6c82..ed00deb 100644 --- a/src/persons/Policeman.java +++ b/src/persons/Policeman.java @@ -1,6 +1,7 @@ package persons; import exceptions.ForbiddenTargetException; +import exceptions.GuiltyPoliceException; import items.Item; import persons.enums.Role; @@ -9,7 +10,6 @@ import persons.interfaces.Manhuntable; public class Policeman extends Person { - private Person chasing = null; public Policeman(String name, Integer age, Role disguiseAs, boolean isHurt) { @@ -20,12 +20,14 @@ public class Policeman extends Person { super(name, age); } + public Person getChasing() {return this.chasing;} + public void pursue(Manhuntable target){ if (target.getDisguiseAs() == Role.POLICEMAN || target.getDisguiseAs() == Role.COMMISSIONER) { throw new ForbiddenTargetException( this.getName() + " не может преследовать " + target.getName() + " он полицейский или комиссар" ); - } else if (target.getIsHurt()) { + } else if (target.getHurt()) { throw new ForbiddenTargetException( this.getName() + " не может преследовать " + target.getName() + " он ранен" ); @@ -43,7 +45,7 @@ public class Policeman extends Person { this.getName() + " не может арестовать " + target.getName() + " он полицейский или комиссар" ); - } else if (target.getIsHurt()) { + } else if (target.getHurt()) { throw new ForbiddenTargetException( this.getName() + " не может арестовать " + target.getName() + " он ранен" ); @@ -60,12 +62,14 @@ public class Policeman extends Person { for (Item item : trunk) { if (found) {break;} - if (item.getIsSuspicious() || !item.getIsLost()) { + if (item.getIsSuspicious() && !item.getIsLost()) { found = true; + item.setIsLost(false); System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item); - } else if (item.getIsSuspicious() || item.getIsLost() || Math.random() > 0.5) { + } else if (item.getIsSuspicious() && item.getIsLost() && Math.random() > 0.5) { found = true; + item.setIsLost(false); System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item); } } diff --git a/src/persons/interfaces/Manhuntable.java b/src/persons/interfaces/Manhuntable.java index 5f5937f..0e42f69 100644 --- a/src/persons/interfaces/Manhuntable.java +++ b/src/persons/interfaces/Manhuntable.java @@ -5,15 +5,16 @@ import persons.enums.Role; public interface Manhuntable { boolean isArrested = false; - boolean isHurt = false; boolean guilty = false; + boolean hurt = false; void arrest(); boolean getGuilty(); void setGuilty(boolean guilty); boolean getIsArrested(); - boolean getIsHurt(); + boolean getHurt(); Role getDisguiseAs(); String getName(); Person getPerson(); + void setHurt(boolean hurt); } \ No newline at end of file diff --git a/src/persons/interfaces/ShootablePerson.java b/src/persons/interfaces/ShootablePerson.java new file mode 100644 index 0000000..9dad0a9 --- /dev/null +++ b/src/persons/interfaces/ShootablePerson.java @@ -0,0 +1,9 @@ +package persons.interfaces; + +public interface ShootablePerson { + boolean hurt = false; + + void setHurt(boolean hurt); + boolean getHurt(); + String getName(); +}