This commit is contained in:
2025-12-26 21:59:02 +03:00
parent 428ee00dae
commit 7155e5ae51
21 changed files with 183 additions and 105 deletions
+65 -1
View File
@@ -1,8 +1,13 @@
import exceptions.IllegalWeaponUsageException;
import items.Item; import items.Item;
import items.MoneyBag;
import items.Vehicle;
import items.Weapon; import items.Weapon;
import items.enums.VehicleType;
import items.enums.WeaponType; import items.enums.WeaponType;
import persons.Bandit; import persons.Bandit;
import persons.Commissioner; import persons.Commissioner;
import persons.Person;
import persons.Policeman; import persons.Policeman;
import persons.enums.Role; import persons.enums.Role;
@@ -14,18 +19,77 @@ public class Main {
Bandit A = new Bandit("A", 27); Bandit A = new Bandit("A", 27);
Policeman B = new Policeman("B", 28); Policeman B = new Policeman("B", 28);
Commissioner C = new Commissioner("C", 30); 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.planRobbery();
A.executeRobbery(); A.executeRobbery();
MoneyBag bag = new MoneyBag(A);
car2.setTrunk(bag);
bag.setIsLost(true);
System.out.println();
B.pursue(A); B.pursue(A);
B.arrest(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()) { while (!A.getGuilty()) {
C.interrogate(A); C.interrogate(A);
} }
System.out.println();
C.conclude(A); C.conclude(A);
} }
} }
+1 -5
View File
@@ -1,12 +1,8 @@
package exceptions; package exceptions;
public class CannotBeHidedException extends RuntimeException { public class CannotBeHidedException extends CustomException {
public CannotBeHidedException(String message) { public CannotBeHidedException(String message) {
super(message); super(message);
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
+13
View File
@@ -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();
}
}
+1 -5
View File
@@ -1,12 +1,8 @@
package exceptions; package exceptions;
public class ForbiddenTargetException extends RuntimeException { public class ForbiddenTargetException extends CustomException {
public ForbiddenTargetException(String message) { public ForbiddenTargetException(String message) {
super(message); super(message);
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
@@ -0,0 +1,7 @@
package exceptions;
public class GuiltyPoliceException extends CustomException {
public GuiltyPoliceException(String message) {
super(message);
}
}
@@ -1,13 +1,8 @@
package exceptions; package exceptions;
public class IllegalWeaponUsageException extends RuntimeException { public class IllegalWeaponUsageException extends CustomException {
public IllegalWeaponUsageException(String message) { public IllegalWeaponUsageException(String message) {
super(message); super(message);
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
@@ -1,13 +1,7 @@
package exceptions; package exceptions;
public class SuspiciousItemNotFoundException extends RuntimeException { public class SuspiciousItemNotFoundException extends CustomException {
public SuspiciousItemNotFoundException() { public SuspiciousItemNotFoundException() {
super("Money bag not found"); super("Money bag not found");
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
@@ -1,13 +1,7 @@
package exceptions; package exceptions;
public class UnpreparedRobberyException extends RuntimeException { public class UnpreparedRobberyException extends CustomException {
public UnpreparedRobberyException(String message) { public UnpreparedRobberyException(String message) {
super(message); super(message);
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
+1 -6
View File
@@ -1,13 +1,8 @@
package exceptions; package exceptions;
public class VehicleDriverException extends Exception { public class VehicleDriverException extends CustomException {
public VehicleDriverException(String message) { public VehicleDriverException(String message) {
super(message); super(message);
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
+1 -7
View File
@@ -1,13 +1,7 @@
package exceptions; package exceptions;
public class VehicleIsBrokenException extends RuntimeException { public class VehicleIsBrokenException extends CustomException {
public VehicleIsBrokenException(String message) { public VehicleIsBrokenException(String message) {
super(message); super(message);
} }
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
} }
+21 -6
View File
@@ -1,15 +1,16 @@
package items; package items;
import items.interfaces.CanBeShooted; import items.interfaces.ShootableItem;
import items.interfaces.Suspicious; import items.interfaces.Suspicious;
import persons.Person; import persons.Person;
import java.util.UUID; import java.util.UUID;
public abstract class Item implements CanBeShooted, Suspicious { public abstract class Item implements ShootableItem, Suspicious {
private double weight; private double weight;
private Person owner; private Person owner;
private boolean isBroken; private boolean isBroken;
boolean isLost = false; boolean isLost = false;
private int durability = 1;
// partNumber shenanigans // partNumber shenanigans
private final String partNumber = UUID.randomUUID().toString(); private final String partNumber = UUID.randomUUID().toString();
@@ -35,7 +36,7 @@ public abstract class Item implements CanBeShooted, Suspicious {
@Override @Override
public String toString() { public String toString() {
return "%s %f %b".formatted(owner.toString(), weight, isBroken); return "Вещь [владелец:%s, вес:%f, сломан:%b]".formatted(owner.toString(), weight, isBroken);
} }
// suspicious // suspicious
@@ -62,14 +63,28 @@ public abstract class Item implements CanBeShooted, Suspicious {
public void setOwner(Person owner) {this.owner = owner;} public void setOwner(Person owner) {this.owner = owner;}
// break // 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) { if (Math.random() > 0.7 && this.isBroken) {
this.isBroken = false; this.isBroken = false;
System.out.println(this.toString() + " удалось починить");
} }
return isBroken; System.out.println(this.toString() + " не удалось починить!");
} }
public boolean getIsBroken() { return this.isBroken; } public boolean getIsBroken() { return this.isBroken; }
public void setIsBroken(boolean isBroken) {this.isBroken = isBroken; }
public int getDurability() {return durability;}
} }
+3 -8
View File
@@ -1,10 +1,10 @@
package items; package items;
import exceptions.SuspiciousItemNotFoundException; import items.interfaces.ShootableItem;
import items.interfaces.Suspicious; import items.interfaces.Suspicious;
import persons.Person; import persons.Person;
public class MoneyBag extends Item implements Suspicious { public class MoneyBag extends Item implements Suspicious, ShootableItem {
private boolean isLost = false; private boolean isLost = false;
private record Money() { private record Money() {
@@ -32,12 +32,7 @@ public class MoneyBag extends Item implements Suspicious {
} }
public boolean getIsLost() { public boolean getIsLost() {
if (Math.random() > 0.5 && this.isLost) { return this.isLost;
this.isLost = false;
return true;
} else {
return false;
}
} }
// money // money
+10 -4
View File
@@ -5,19 +5,20 @@ import exceptions.VehicleDriverException;
import exceptions.VehicleIsBrokenException; import exceptions.VehicleIsBrokenException;
import items.enums.VehicleType; import items.enums.VehicleType;
import items.interfaces.ShootableItem;
import persons.Person; import persons.Person;
import items.interfaces.CanBeInvestigated; import items.interfaces.CanBeInvestigated;
import java.util.ArrayList; import java.util.ArrayList;
public class Vehicle extends Item implements CanBeInvestigated { public class Vehicle extends Item implements CanBeInvestigated, ShootableItem {
private final VehicleType type; private final VehicleType type;
private Person driver; private Person driver;
private String destination; private String destination;
private boolean isMoving; private boolean isMoving;
private ArrayList<Person> passengers; private ArrayList<Person> passengers = new ArrayList<Person>();
private ArrayList<Item> trunk; private ArrayList<Item> trunk = new ArrayList<Item>();
private int durability; private int durability;
private final int speed; private final int speed;
@@ -98,9 +99,14 @@ public class Vehicle extends Item implements CanBeInvestigated {
public void doBreak() { public void doBreak() {
if (this.durability > 0) { if (this.durability > 0) {
this.durability--; this.durability--;
if (this.durability == 0) {
this.setIsBroken(true);
}
} else { } else {
this.isMoving = false; this.isMoving = false;
super.doBreak(); this.setIsBroken(true);
} }
} }
+12 -19
View File
@@ -1,13 +1,11 @@
package items; package items;
import exceptions.IllegalWeaponUsageException; import exceptions.IllegalWeaponUsageException;
import exceptions.VehicleDriverException;
import items.enums.VehicleType;
import items.enums.WeaponType; import items.enums.WeaponType;
import items.interfaces.CanBeShooted; import items.interfaces.ShootableItem;
import items.interfaces.Suspicious; import items.interfaces.Suspicious;
import persons.Person; import persons.Person;
import persons.enums.Role; import persons.interfaces.ShootablePerson;
public class Weapon extends Item implements Suspicious { public class Weapon extends Item implements Suspicious {
@@ -27,32 +25,27 @@ public class Weapon extends Item implements Suspicious {
this.accuracy = type.getAccuracy(); 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()) { if (from != this.getOwner()) {
throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия"); throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия");
} }
to.setHurt(true);
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() + "!"); 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()) { if (from != this.getOwner()) {
throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия"); throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия");
} }
to.doBreak(); 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() + " сломан!");
}
} }
@@ -2,10 +2,12 @@ package items.interfaces;
import persons.Person; import persons.Person;
public interface CanBeShooted { public interface ShootableItem {
boolean isBroken = false; boolean isBroken = false;
int durability = 1;
void doBreak(); void doBreak();
boolean getIsBroken(); boolean getIsBroken();
Person getOwner(); Person getOwner();
int getDurability();
} }
+3 -6
View File
@@ -10,10 +10,10 @@ import persons.enums.Role;
import persons.interfaces.Manhuntable; import persons.interfaces.Manhuntable;
public class Bandit extends Person implements Manhuntable { public class Bandit extends Person implements Manhuntable {
private boolean isArrested = false; boolean isArrested = false;
boolean guilty = false;
private boolean isInRobbery = false; private boolean isInRobbery = false;
private boolean isPreparedToRobbery = false; private boolean isPreparedToRobbery = false;
private boolean guilty = false;
public Bandit(String name, Integer age, Role disguiseAs, boolean isHurt) { public Bandit(String name, Integer age, Role disguiseAs, boolean isHurt) {
super(name, age, disguiseAs, 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();
}
} }
+2 -2
View File
@@ -22,7 +22,7 @@ public class Commissioner extends Policeman {
throw new ForbiddenTargetException(target.getName() + " не арестован!"); throw new ForbiddenTargetException(target.getName() + " не арестован!");
} }
if (target.getIsHurt()) { if (target.getHurt()) {
throw new ForbiddenTargetException(target.getName() + " ранен!"); throw new ForbiddenTargetException(target.getName() + " ранен!");
} }
@@ -35,7 +35,7 @@ public class Commissioner extends Policeman {
throw new ForbiddenTargetException(target.getName() + " отрицает вину"); throw new ForbiddenTargetException(target.getName() + " отрицает вину");
} }
if (target.getIsHurt()) { if (target.getHurt()) {
throw new ForbiddenTargetException(target.getName() + " ранен!"); throw new ForbiddenTargetException(target.getName() + " ранен!");
} }
+15 -7
View File
@@ -2,16 +2,17 @@ package persons;
import items.Item; import items.Item;
import persons.enums.Role; import persons.enums.Role;
import persons.interfaces.ShootablePerson;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
public abstract class Person { public abstract class Person implements ShootablePerson {
private String name; private String name;
private Integer age; private Integer age;
private Role disguiseAs; private Role disguiseAs;
private ArrayList<Item> items = new ArrayList<>(); private ArrayList<Item> items = new ArrayList<>();
private boolean isHurt; private boolean hurt = false;
// passport shenanigans // passport shenanigans
private final String passport = UUID.randomUUID().toString(); private final String passport = UUID.randomUUID().toString();
@@ -20,14 +21,14 @@ public abstract class Person {
this.name = name; this.name = name;
this.age = age; this.age = age;
this.disguiseAs = disguiseAs; this.disguiseAs = disguiseAs;
this.isHurt = isHurt; this.hurt = isHurt;
} }
public Person(String name, Integer age) { public Person(String name, Integer age) {
this.name = name; this.name = name;
this.age = age; this.age = age;
this.disguiseAs = Role.CIVILIAN; this.disguiseAs = Role.CIVILIAN;
this.isHurt = false; this.hurt = false;
} }
@Override @Override
@@ -45,9 +46,11 @@ public abstract class Person {
@Override @Override
public String toString() { 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 // passport
public String getPassport() {return passport;} public String getPassport() {return passport;}
@@ -69,8 +72,13 @@ public abstract class Person {
public void setDisguiseAs(Role disguiseAs) {this.disguiseAs = disguiseAs;} public void setDisguiseAs(Role disguiseAs) {this.disguiseAs = disguiseAs;}
// health // health
public void doHurt() {this.isHurt = true;} public void setHurt(boolean hurt) {
public boolean isHurt() {return this.isHurt;} this.hurt = hurt;
if (!hurt) {
System.out.println(this.name + " вылечен!");
}
}
public boolean getHurt() {return this.hurt;}
//items //items
public void addItem(Item item) {this.items.add(item);} public void addItem(Item item) {this.items.add(item);}
+9 -5
View File
@@ -1,6 +1,7 @@
package persons; package persons;
import exceptions.ForbiddenTargetException; import exceptions.ForbiddenTargetException;
import exceptions.GuiltyPoliceException;
import items.Item; import items.Item;
import persons.enums.Role; import persons.enums.Role;
@@ -9,7 +10,6 @@ import persons.interfaces.Manhuntable;
public class Policeman extends Person { public class Policeman extends Person {
private Person chasing = null; private Person chasing = null;
public Policeman(String name, Integer age, Role disguiseAs, boolean isHurt) { public Policeman(String name, Integer age, Role disguiseAs, boolean isHurt) {
@@ -20,12 +20,14 @@ public class Policeman extends Person {
super(name, age); super(name, age);
} }
public Person getChasing() {return this.chasing;}
public void pursue(Manhuntable target){ public void pursue(Manhuntable target){
if (target.getDisguiseAs() == Role.POLICEMAN || target.getDisguiseAs() == Role.COMMISSIONER) { if (target.getDisguiseAs() == Role.POLICEMAN || target.getDisguiseAs() == Role.COMMISSIONER) {
throw new ForbiddenTargetException( throw new ForbiddenTargetException(
this.getName() + " не может преследовать " + target.getName() + " он полицейский или комиссар" this.getName() + " не может преследовать " + target.getName() + " он полицейский или комиссар"
); );
} else if (target.getIsHurt()) { } else if (target.getHurt()) {
throw new ForbiddenTargetException( throw new ForbiddenTargetException(
this.getName() + " не может преследовать " + target.getName() + " он ранен" this.getName() + " не может преследовать " + target.getName() + " он ранен"
); );
@@ -43,7 +45,7 @@ public class Policeman extends Person {
this.getName() + " не может арестовать " + target.getName() + " он полицейский или комиссар" this.getName() + " не может арестовать " + target.getName() + " он полицейский или комиссар"
); );
} else if (target.getIsHurt()) { } else if (target.getHurt()) {
throw new ForbiddenTargetException( throw new ForbiddenTargetException(
this.getName() + " не может арестовать " + target.getName() + " он ранен" this.getName() + " не может арестовать " + target.getName() + " он ранен"
); );
@@ -60,12 +62,14 @@ public class Policeman extends Person {
for (Item item : trunk) { for (Item item : trunk) {
if (found) {break;} if (found) {break;}
if (item.getIsSuspicious() || !item.getIsLost()) { if (item.getIsSuspicious() && !item.getIsLost()) {
found = true; found = true;
item.setIsLost(false);
System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item); 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; found = true;
item.setIsLost(false);
System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item); System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item);
} }
} }
+3 -2
View File
@@ -5,15 +5,16 @@ import persons.enums.Role;
public interface Manhuntable { public interface Manhuntable {
boolean isArrested = false; boolean isArrested = false;
boolean isHurt = false;
boolean guilty = false; boolean guilty = false;
boolean hurt = false;
void arrest(); void arrest();
boolean getGuilty(); boolean getGuilty();
void setGuilty(boolean guilty); void setGuilty(boolean guilty);
boolean getIsArrested(); boolean getIsArrested();
boolean getIsHurt(); boolean getHurt();
Role getDisguiseAs(); Role getDisguiseAs();
String getName(); String getName();
Person getPerson(); Person getPerson();
void setHurt(boolean hurt);
} }
@@ -0,0 +1,9 @@
package persons.interfaces;
public interface ShootablePerson {
boolean hurt = false;
void setHurt(boolean hurt);
boolean getHurt();
String getName();
}