upd
This commit is contained in:
+65
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package exceptions;
|
||||
|
||||
public class GuiltyPoliceException extends CustomException {
|
||||
public GuiltyPoliceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+21
-6
@@ -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;}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+10
-4
@@ -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<Person> passengers;
|
||||
private ArrayList<Item> trunk;
|
||||
private ArrayList<Person> passengers = new ArrayList<Person>();
|
||||
private ArrayList<Item> trunk = new ArrayList<Item>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-19
@@ -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();
|
||||
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() + " сломан!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() + " ранен!");
|
||||
}
|
||||
|
||||
|
||||
+15
-7
@@ -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<Item> 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);}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package persons.interfaces;
|
||||
|
||||
public interface ShootablePerson {
|
||||
boolean hurt = false;
|
||||
|
||||
void setHurt(boolean hurt);
|
||||
boolean getHurt();
|
||||
String getName();
|
||||
}
|
||||
Reference in New Issue
Block a user