-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment5pt4.java
116 lines (96 loc) · 3.39 KB
/
assignment5pt4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class CustomCharacter {
String name;
String characterClass;
CustomWeapon weapon;
int health;
int mana;
public CustomCharacter(String name, String characterClass, CustomWeapon weapon, int health, int mana) {
this.name = name;
this.characterClass = characterClass;
this.weapon = weapon;
this.health = health;
this.mana = mana;
}
@Override
public String toString() {
return "CustomCharacter{" +
"name='" + name + '\'' +
", characterClass='" + characterClass + '\'' +
", weapon=" + weapon +
", health=" + health +
", mana=" + mana +
'}';
}
}
class CustomWeapon {
String type;
int damage;
int speed;
int range;
public CustomWeapon(String type, int damage, int speed, int range) {
this.type = type;
this.damage = damage;
this.speed = speed;
this.range = range;
}
@Override
public String toString() {
return "CustomWeapon{" +
"type='" + type + '\'' +
", damage=" + damage +
", speed=" + speed +
", range=" + range +
'}';
}
}
abstract class CustomCharacterFactory {
public abstract CustomCharacter createCustomCharacter();
public abstract CustomWeapon createCustomWeapon();
}
class WarriorSwordCharacterFactory extends CustomCharacterFactory {
public CustomCharacter createCustomCharacter() {
return new CustomCharacter("Warrior", "Warrior", createCustomWeapon(), 100, 50);
}
public CustomWeapon createCustomWeapon() {
return new CustomWeapon("Sword", 20, 10, 2);
}
}
class MageStaffCharacterFactory extends CustomCharacterFactory {
public CustomCharacter createCustomCharacter() {
return new CustomCharacter("Mage", "Mage", createCustomWeapon(), 80, 100);
}
public CustomWeapon createCustomWeapon() {
return new CustomWeapon("Staff", 15, 8, 3);
}
}
class ArcherBowCharacterFactory extends CustomCharacterFactory {
public CustomCharacter createCustomCharacter() {
return new CustomCharacter("Archer", "Archer", createCustomWeapon(), 90, 70);
}
public CustomWeapon createCustomWeapon() {
return new CustomWeapon("Bow", 18, 12, 5);
}
}
class CustomCharacterCreator {
private CustomCharacterFactory factory;
public void setFactory(CustomCharacterFactory factory) {
this.factory = factory;
}
public CustomCharacter createCustomCharacter() {
return factory.createCustomCharacter();
}
}
public class assignment5pt4 {
public static void main(String[] args) {
CustomCharacterCreator creator = new CustomCharacterCreator();
creator.setFactory(new WarriorSwordCharacterFactory());
CustomCharacter warrior = creator.createCustomCharacter();
creator.setFactory(new MageStaffCharacterFactory());
CustomCharacter mage = creator.createCustomCharacter();
creator.setFactory(new ArcherBowCharacterFactory());
CustomCharacter archer = creator.createCustomCharacter();
System.out.println("Warrior: " + warrior);
System.out.println("Mage: " + mage);
System.out.println("Archer: " + archer);
}
}