Merge branch 'lab4'
This commit is contained in:
commit
3c92334ba0
11
src/Annotations/ValidLength.java
Normal file
11
src/Annotations/ValidLength.java
Normal file
@ -0,0 +1,11 @@
|
||||
package Annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ValidLength {
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package Characters;
|
||||
|
||||
import Annotations.ValidLength;
|
||||
import Entity.Entity;
|
||||
import Items.Item;
|
||||
|
||||
@ -25,6 +26,8 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
||||
|
||||
private final Gender gender;
|
||||
private Entity position;
|
||||
|
||||
@ValidLength
|
||||
private String currentState = "";
|
||||
protected Feels feel;
|
||||
protected Item itemInHand;
|
||||
@ -62,6 +65,7 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
||||
|
||||
protected final void setPosition(Entity obj) {
|
||||
this.position = obj;
|
||||
this.validateFields();
|
||||
}
|
||||
|
||||
public final String getState() {
|
||||
@ -70,6 +74,7 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
||||
|
||||
protected final void setState(String state) {
|
||||
this.currentState = state;
|
||||
this.validateFields();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -89,4 +94,34 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.name, this.gender);
|
||||
}
|
||||
|
||||
private void validateFields() {
|
||||
for (java.lang.reflect.Field field : Character.class.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(ValidLength.class)) {
|
||||
String value = getValueForField(this, field);
|
||||
if (value.length() < 3 || value.length() > 20) {
|
||||
setValueForField(this, field, "unknown");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String getValueForField(Object obj, java.lang.reflect.Field field) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
return (String) field.get(obj);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void setValueForField(Object obj, java.lang.reflect.Field field, String value) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
field.set(obj, value);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,12 +90,7 @@ public class Karlson extends Character implements IGrabbable, IFlyable {
|
||||
})
|
||||
};
|
||||
|
||||
int minPath = Integer.MAX_VALUE;
|
||||
for (Path path: paths) {
|
||||
minPath = Math.min(path.getResult(), minPath);
|
||||
}
|
||||
|
||||
return minPath;
|
||||
return Arrays.stream(paths).map(Path::getResult).min(Integer::compare).get();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4,20 +4,32 @@ import Entity.Entity;
|
||||
import Items.Item;
|
||||
|
||||
public class Malysh extends Character {
|
||||
public Mouth mouth = new Mouth();
|
||||
private final Mouth mouth = new Mouth(32);
|
||||
public Malysh() {
|
||||
super("Малыш", Gender.MAN ,Feels.NORMAL);
|
||||
super("Малыш", Gender.MAN, Feels.NORMAL);
|
||||
}
|
||||
public class Mouth {
|
||||
private class Mouth {
|
||||
private final int numsOfTeeth;
|
||||
Mouth(int numsOfTeeth) {
|
||||
this.numsOfTeeth = numsOfTeeth;
|
||||
}
|
||||
public void speak(String speech) {
|
||||
if (speech == null) {
|
||||
Malysh.this.setState(String.format("%s ничего не сказал", Malysh.this.getGender()));
|
||||
return;
|
||||
}
|
||||
if (numsOfTeeth < 8) {
|
||||
Malysh.this.setState(String.format("%s что-то пробормотал", Malysh.this.getGender()));
|
||||
return;
|
||||
}
|
||||
Malysh.this.setState(String.format("%s сказал: \"%s\"", Malysh.this.getName(), speech));
|
||||
}
|
||||
}
|
||||
|
||||
public void speak(String speach) {
|
||||
this.mouth.speak(speach);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveTo(Entity obj) {
|
||||
this.setPosition(obj);
|
||||
|
||||
@ -43,7 +43,7 @@ public class StoryTeller {
|
||||
System.out.println(frenBok.getState());
|
||||
malysh.setFeel(Feels.DEAD_INSIDE);
|
||||
System.out.println(malysh.getState());
|
||||
malysh.mouth.speak(null);
|
||||
malysh.speak(null);
|
||||
System.out.println(malysh.getState());
|
||||
door.closeDoor();
|
||||
karlson.moveTo(door);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user