Init commit

This commit is contained in:
2025-12-25 23:38:48 +03:00
commit 0bf18fa70f
28 changed files with 862 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Environment-dependent path to Maven home directory
/mavenHomeManager.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="homebrew-24" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/lab3-4prog.iml" filepath="$PROJECT_DIR$/lab3-4prog.iml" />
</modules>
</component>
</project>
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+31
View File
@@ -0,0 +1,31 @@
import items.Item;
import items.Weapon;
import items.enums.WeaponType;
import persons.Bandit;
import persons.Commissioner;
import persons.Policeman;
import persons.enums.Role;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
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));
A.planRobbery();
A.executeRobbery();
B.pursue(A);
B.arrest(A);
while (!A.getGuilty()) {
C.interrogate(A);
}
C.conclude(A);
}
}
@@ -0,0 +1,12 @@
package exceptions;
public class CannotBeHidedException extends RuntimeException {
public CannotBeHidedException(String message) {
super(message);
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
@@ -0,0 +1,12 @@
package exceptions;
public class ForbiddenTargetException extends RuntimeException {
public ForbiddenTargetException(String message) {
super(message);
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
@@ -0,0 +1,13 @@
package exceptions;
public class IllegalWeaponUsageException extends RuntimeException {
public IllegalWeaponUsageException(String message) {
super(message);
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
@@ -0,0 +1,13 @@
package exceptions;
public class SuspiciousItemNotFoundException extends RuntimeException {
public SuspiciousItemNotFoundException() {
super("Money bag not found");
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
@@ -0,0 +1,13 @@
package exceptions;
public class UnpreparedRobberyException extends RuntimeException {
public UnpreparedRobberyException(String message) {
super(message);
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
@@ -0,0 +1,13 @@
package exceptions;
public class VehicleDriverException extends Exception {
public VehicleDriverException(String message) {
super(message);
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
@@ -0,0 +1,13 @@
package exceptions;
public class VehicleIsBrokenException extends RuntimeException {
public VehicleIsBrokenException(String message) {
super(message);
}
@Override
public String getMessage() {
return "uh..." + super.getMessage();
}
}
+75
View File
@@ -0,0 +1,75 @@
package items;
import items.interfaces.CanBeShooted;
import items.interfaces.Suspicious;
import persons.Person;
import java.util.UUID;
public abstract class Item implements CanBeShooted, Suspicious {
private double weight;
private Person owner;
private boolean isBroken;
boolean isLost = false;
// partNumber shenanigans
private final String partNumber = UUID.randomUUID().toString();
public Item(Person owner, Double weight, Boolean isBroken) {
this.weight = weight;
this.owner = owner;
this.isBroken = isBroken;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Item)) return false;
Item item = (Item) o;
return partNumber == item.getPartNumber();
}
@Override
public int hashCode() {
return partNumber.hashCode();
}
@Override
public String toString() {
return "%s %f %b".formatted(owner.toString(), weight, isBroken);
}
// suspicious
public boolean getIsSuspicious() {
return false;
}
// lost&find
public boolean getIsLost() {
return this.isLost;
}
abstract public void setIsLost(boolean isLost);
// part number
public String getPartNumber() {return partNumber;}
// weight
public double getWeight() { return weight; }
public void setWeight(double weight) { this.weight = weight; }
// owner
public Person getOwner() {return owner;}
public void setOwner(Person owner) {this.owner = owner;}
// break
public void doBreak() {this.isBroken = !this.isBroken;}
public boolean fix() {
if (Math.random() > 0.7 && this.isBroken) {
this.isBroken = false;
}
return isBroken;
}
public boolean getIsBroken() { return this.isBroken; }
}
+47
View File
@@ -0,0 +1,47 @@
package items;
import exceptions.SuspiciousItemNotFoundException;
import items.interfaces.Suspicious;
import persons.Person;
public class MoneyBag extends Item implements Suspicious {
private boolean isLost = false;
private record Money() {
private static final int amount = 300; // $
private static final double weight = 5;
}
public MoneyBag(Person owner, boolean isBroken, boolean isLost) {
super(owner, Money.weight, isBroken);
this.isLost = isLost;
}
public MoneyBag(Person owner) {
super(owner, Money.weight, false);
}
// suspicious
public boolean getIsSuspicious() {
return true;
}
// lost&find
public void setIsLost(boolean isLost) {
this.isLost = isLost;
}
public boolean getIsLost() {
if (Math.random() > 0.5 && this.isLost) {
this.isLost = false;
return true;
} else {
return false;
}
}
// money
public int getAmountOfMoney() {return Money.amount; }
}
+109
View File
@@ -0,0 +1,109 @@
package items;
import exceptions.CannotBeHidedException;
import exceptions.VehicleDriverException;
import exceptions.VehicleIsBrokenException;
import items.enums.VehicleType;
import persons.Person;
import items.interfaces.CanBeInvestigated;
import java.util.ArrayList;
public class Vehicle extends Item implements CanBeInvestigated {
private final VehicleType type;
private Person driver;
private String destination;
private boolean isMoving;
private ArrayList<Person> passengers;
private ArrayList<Item> trunk;
private int durability;
private final int speed;
// suspicious stuff
boolean isSuspicious = false;
public Vehicle(
Person owner, Double weight, Boolean isBroken,
VehicleType type, Person driver, String destination,
boolean isMoving, int durability, int speed
) {
super(owner, weight, isBroken);
this.type = type;
this.driver = driver;
this.destination = destination;
this.isMoving = isMoving;
this.durability = durability;
this.speed = speed;
}
public Vehicle(Person owner, Boolean isBroken, VehicleType type, Person driver, String destination) {
super(owner, type.getWeight(), isBroken);
this.type = type;
this.driver = driver;
this.destination = destination;
this.isMoving = false;
this.durability = type.getDurability();
this.speed = type.getSpeed();
}
// destination
public boolean move(String destination) throws VehicleIsBrokenException {
if (this.getIsBroken()) {
this.destination = destination;
return true;
} else {
throw new VehicleIsBrokenException("Машина " + this.getOwner() + " под управлением " + this.driver + " сломана");
}
}
public String getDestination() {return this.destination;}
// info
public VehicleType getType() {return this.type;}
public boolean getIsMoving() {return this.isMoving;}
public int getSpeed() {return this.speed;}
// passengers
public Person[] getPassengers() {
return this.passengers.toArray(new Person[0]);
}
public void setPassengers(ArrayList<Person> p) {this.passengers = p;}
public void setPassengers(Person p) {this.passengers.add(p);}
// trunk
public Item[] getTrunk() {return this.trunk.toArray(new Item[0]);}
public void setTrunk(ArrayList<Item> t) {this.trunk = t;}
public void setTrunk(Item t) {this.trunk.add(t);}
// "I drive" - Ryan Gosling
public Person getDriver() {return this.driver;}
public void setDriver(Person driver) throws VehicleDriverException {
if (passengers.contains(driver)) {
this.driver = driver;
} else {
throw new VehicleDriverException(driver.getName() + " не в машине либо уже является водителем");
}
}
@Override
public void setIsLost(boolean isLost) throws CannotBeHidedException {
throw new CannotBeHidedException("Машина не может быть спрятана");
}
// damage
@Override
public void doBreak() {
if (this.durability > 0) {
this.durability--;
} else {
this.isMoving = false;
super.doBreak();
}
}
public int getDurability() {return this.durability;}
}
+67
View File
@@ -0,0 +1,67 @@
package items;
import exceptions.IllegalWeaponUsageException;
import exceptions.VehicleDriverException;
import items.enums.VehicleType;
import items.enums.WeaponType;
import items.interfaces.CanBeShooted;
import items.interfaces.Suspicious;
import persons.Person;
import persons.enums.Role;
public class Weapon extends Item implements Suspicious {
private final WeaponType type;
private final double accuracy;
private boolean isLost;
public Weapon(Person owner, Double weight, Boolean isBroken, WeaponType type, double accuracy) {
super(owner, weight, isBroken);
this.type = type;
this.accuracy = accuracy;
}
public Weapon(Person owner, WeaponType type) {
super(owner, type.getWeight(), false);
this.type = type;
this.accuracy = type.getAccuracy();
}
public void shoot(Person from, Person 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() + "!");
}
}
public void shoot(Person from, CanBeShooted to) throws IllegalWeaponUsageException {
if (from != this.getOwner()) {
throw new IllegalWeaponUsageException(from.getName() + " не является владельцем оружия");
}
to.doBreak();
System.out.println(from.getName() + " Выстрелил в " + to.getOwner() + "!");
}
@Override
public boolean getIsSuspicious() {return true;}
@Override
public boolean getIsLost() {return isLost;}
@Override
public void setIsLost(boolean isLost) {this.isLost = isLost;}
}
+23
View File
@@ -0,0 +1,23 @@
package items.enums;
public enum VehicleType {
POLICE_CAR(5, 2000, 6),
ARMORED_CAR(3, 4000, 10),
BANDIT_CAR(4, 2000, 6),
CIVILIAN(4, 2000, 6);
private final int speed;
private final double weight;
private final int durability;
VehicleType(int speed, int weight, int durability) {
this.speed = speed;
this.weight = weight;
this.durability = durability;
}
public int getSpeed() { return this.speed; }
public double getWeight() { return this.weight; }
public int getDurability() { return this.durability; }
}
+18
View File
@@ -0,0 +1,18 @@
package items.enums;
public enum WeaponType {
PISTOL(0.7, 0.7),
MACHINE_GUN(0.4, 5.5);
private final double accuracy;
private final double weight;
WeaponType(double acc, double weight) {
this.accuracy = acc;
this.weight = weight;
}
public double getAccuracy() { return this.accuracy; }
public double getWeight() { return this.weight; }
}
@@ -0,0 +1,11 @@
package items.interfaces;
import items.Item;
import java.util.ArrayList;
public interface CanBeInvestigated {
ArrayList<Item> trunk = new ArrayList<>();
Item[] getTrunk();
}
+11
View File
@@ -0,0 +1,11 @@
package items.interfaces;
import persons.Person;
public interface CanBeShooted {
boolean isBroken = false;
void doBreak();
boolean getIsBroken();
Person getOwner();
}
+11
View File
@@ -0,0 +1,11 @@
package items.interfaces;
import persons.Person;
public interface Suspicious {
boolean isLost = false;
boolean getIsSuspicious();
boolean getIsLost();
void setIsLost(boolean isLost);
}
+75
View File
@@ -0,0 +1,75 @@
package persons;
import exceptions.ForbiddenTargetException;
import exceptions.UnpreparedRobberyException;
import items.enums.WeaponType;
import items.MoneyBag;
import items.Weapon;
import persons.enums.Role;
import persons.interfaces.Manhuntable;
public class Bandit extends Person implements Manhuntable {
private boolean isArrested = 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);
}
public Bandit(String name, Integer age) {
super(name, age);
}
// person
public Person getPerson() {return this;}
// robbery
public void planRobbery() {
System.out.println(this.getName() + " планирует ограбление");
this.addItem(new Weapon(this, WeaponType.PISTOL));
this.isPreparedToRobbery = true;
}
public void executeRobbery() throws UnpreparedRobberyException{
if (this.isPreparedToRobbery) {
this.isInRobbery = true;
System.out.println(this.getName() + " грабит банк! ");
this.addItem(new MoneyBag(this));
} else {
throw new UnpreparedRobberyException(this.getName() + " не готов!");
}
}
public boolean getIsInRobbery() {return this.isInRobbery;}
// guilty
public void setGuilty(boolean guilty) {
this.guilty = guilty;
if (guilty) {
System.out.println(this.getName() + " подтверждает вину!");
} else {
System.out.println(this.getName() + " отрицает вину!");
}
}
public boolean getGuilty() {return this.guilty;}
// arrest
public void arrest() {
if (this.isInRobbery) {
this.isArrested = true;
System.out.println(this.getName() + " арестован!");
} else {
throw new ForbiddenTargetException(this.getName() + " не грабит банк!");
}
}
public boolean getIsArrested() {return isArrested;}
public boolean getIsHurt() {
return super.isHurt();
}
}
+44
View File
@@ -0,0 +1,44 @@
package persons;
import exceptions.ForbiddenTargetException;
import items.Item;
import persons.enums.Role;
import persons.interfaces.Manhuntable;
import java.util.ArrayList;
public class Commissioner extends Policeman {
public Commissioner(String name, Integer age, Role disguiseAs, boolean isHurt) {
super(name, age, disguiseAs, isHurt);
}
public Commissioner(String name, Integer age) {
super(name, age);
}
public void interrogate(Manhuntable target) throws ForbiddenTargetException {
if (!target.getIsArrested()) {
throw new ForbiddenTargetException(target.getName() + " не арестован!");
}
if (target.getIsHurt()) {
throw new ForbiddenTargetException(target.getName() + " ранен!");
}
System.out.println(this.getName() + " ведет переговоры с " + target.getName() + " как с " + target.getDisguiseAs());
target.setGuilty(Math.random() < 0.5);
}
public void conclude(Manhuntable target) throws ForbiddenTargetException {
if (!target.getGuilty()) {
throw new ForbiddenTargetException(target.getName() + " отрицает вину");
}
if (target.getIsHurt()) {
throw new ForbiddenTargetException(target.getName() + " ранен!");
}
System.out.println(target.getName() + " как " + target.getDisguiseAs() + " был пойман");
}
}
+82
View File
@@ -0,0 +1,82 @@
package persons;
import items.Item;
import persons.enums.Role;
import java.util.ArrayList;
import java.util.UUID;
public abstract class Person {
private String name;
private Integer age;
private Role disguiseAs;
private ArrayList<Item> items = new ArrayList<>();
private boolean isHurt;
// passport shenanigans
private final String passport = UUID.randomUUID().toString();
public Person(String name, Integer age, Role disguiseAs, boolean isHurt) {
this.name = name;
this.age = age;
this.disguiseAs = disguiseAs;
this.isHurt = isHurt;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
this.disguiseAs = Role.CIVILIAN;
this.isHurt = false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person p = (Person) o;
return passport == p.getPassport();
}
@Override
public int hashCode() {
return passport.hashCode();
}
@Override
public String toString() {
return "%s %d %s %b".formatted(name, age, disguiseAs, isHurt);
}
// passport
public String getPassport() {return passport;}
// speaking
public void speak(String text) {
System.out.println(this.name + " says " + text);
}
// age
public Integer getAge() {return age;}
public void setAge(Integer age) {this.age = age;}
// name
public String getName() {return name;}
public void setName(String name) {this.name = name;}
// disguise
public Role getDisguiseAs() {return disguiseAs;}
public void setDisguiseAs(Role disguiseAs) {this.disguiseAs = disguiseAs;}
// health
public void doHurt() {this.isHurt = true;}
public boolean isHurt() {return this.isHurt;}
//items
public void addItem(Item item) {this.items.add(item);}
public void removeItem(int item) {this.items.remove(item);}
public Item[] getItems() {
return items.toArray(new Item[0]);
}
}
+78
View File
@@ -0,0 +1,78 @@
package persons;
import exceptions.ForbiddenTargetException;
import items.Item;
import persons.enums.Role;
import items.interfaces.CanBeInvestigated;
import persons.interfaces.Manhuntable;
public class Policeman extends Person {
private Person chasing = null;
public Policeman(String name, Integer age, Role disguiseAs, boolean isHurt) {
super(name, age, disguiseAs, isHurt);
}
public Policeman(String name, Integer age) {
super(name, age);
}
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()) {
throw new ForbiddenTargetException(
this.getName() + " не может преследовать " + target.getName() + " он ранен"
);
} else {
System.out.println(
this.getName() + " преследует " + target.getName() + " замаскированного как " + target.getDisguiseAs()
);
this.chasing = target.getPerson();
}
}
public void arrest(Manhuntable target){
if (target.getDisguiseAs() == Role.POLICEMAN || target.getDisguiseAs() == Role.COMMISSIONER) {
throw new ForbiddenTargetException(
this.getName() + " не может арестовать " + target.getName() + " он полицейский или комиссар"
);
} else if (target.getIsHurt()) {
throw new ForbiddenTargetException(
this.getName() + " не может арестовать " + target.getName() + " он ранен"
);
} else {
System.out.println(target.getName() + " арестован как " + target.getDisguiseAs());
target.arrest();
}
}
public void searchVehicle(CanBeInvestigated vehicle){
boolean found = false;
Item[] trunk = vehicle.getTrunk();
for (Item item : trunk) {
if (found) {break;}
if (item.getIsSuspicious() || !item.getIsLost()) {
found = true;
System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item);
} else if (item.getIsSuspicious() || item.getIsLost() || Math.random() > 0.5) {
found = true;
System.out.println(this.getName() + " нашел в машине подозрительный предмет " + item);
}
}
if (!found) {
System.out.println(this.getName() + " не нашел ничего подозрительного");
}
}
}
+8
View File
@@ -0,0 +1,8 @@
package persons.enums;
public enum Role {
BANDIT,
CIVILIAN,
POLICEMAN,
COMMISSIONER;
}
+19
View File
@@ -0,0 +1,19 @@
package persons.interfaces;
import persons.Person;
import persons.enums.Role;
public interface Manhuntable {
boolean isArrested = false;
boolean isHurt = false;
boolean guilty = false;
void arrest();
boolean getGuilty();
void setGuilty(boolean guilty);
boolean getIsArrested();
boolean getIsHurt();
Role getDisguiseAs();
String getName();
Person getPerson();
}