Skip to content

Commit 482427f

Browse files
#hacktoberfest-accepted
label: hacktoberfest-accepted
0 parents  commit 482427f

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

ENCAPSULATION

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
class Player{
2+
private String name;
3+
private int baseHealth;
4+
private int baseAttack;
5+
private int level;
6+
private int incrementHealth;
7+
private int incrementAttack;
8+
9+
private Armor armor;
10+
private Weapon weapon;
11+
12+
public Player(String name){
13+
this.name = name;
14+
this.baseHealth = 100;
15+
this.baseAttack = 100;
16+
this.level = 1;
17+
this.incrementHealth = 30;
18+
this.incrementAttack = 30;
19+
}
20+
21+
public void display(){
22+
System.out.println("Player\t\t: " + this.name);
23+
System.out.println("Level\t\t: " + this.level);
24+
System.out.println("MaxHealth\t: " + this.maxHealth());
25+
System.out.println("Attack\t\t: " + this.getAttackPower() + "\n");
26+
}
27+
28+
public void levelUp(){
29+
this.level++;
30+
}
31+
32+
public void setArmor(Armor armor){
33+
this.armor = armor;
34+
}
35+
36+
public void setWeapon(Weapon weapon){
37+
this.weapon = weapon;
38+
}
39+
40+
public int maxHealth(){
41+
return this.baseHealth + this.level*this.incrementHealth + this.armor.getAddHealth();
42+
}
43+
44+
public int getAttackPower(){
45+
return this.baseAttack + this.level*this.incrementAttack + this.weapon.getAttack();
46+
}
47+
48+
}
49+
50+
class Weapon{
51+
private String name;
52+
private int attack;
53+
54+
public Weapon(String name, int attack){
55+
this.name = name;
56+
this.attack = attack;
57+
}
58+
59+
public int getAttack(){
60+
return this.attack;
61+
}
62+
}
63+
64+
class Armor{
65+
private String name;
66+
private int strength;
67+
private int health;
68+
69+
public Armor(String name, int strength, int health){
70+
this.name = name;
71+
this.strength = strength;
72+
this.health = health;
73+
}
74+
75+
public int getAddHealth(){
76+
return this.strength*20 + this.health;
77+
}
78+
79+
}
80+
81+
public class Main{
82+
public static void main(String[] args) {
83+
Player player1 = new Player("Firman");
84+
Armor armor1 = new Armor("Tameng",10,100);
85+
Weapon weapon1 = new Weapon("Shotgun", 50);
86+
player1.setArmor(armor1);
87+
player1.setWeapon(weapon1);
88+
89+
Player player2 = new Player("Alia");
90+
Armor armor2 = new Armor("Helm",5,90);
91+
Weapon weapon2 = new Weapon("Panah", 30);
92+
player2.setArmor(armor2);
93+
player2.setWeapon(weapon2);
94+
95+
player1.display();
96+
player2.display();
97+
}
98+
}

0 commit comments

Comments
 (0)