Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/H071221077/Pertemuan_7/No1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
abstract class Character{
protected String name;
protected int attackPower;

public String getName() {
return name;
}
public int getAttackPower() {
return attackPower;
}

public Character(String name, int attackPower) {
this.name = name;
this.attackPower = attackPower;
}

abstract int attack();
abstract int attack(AttackType attackType);
}

class Fighter extends Character{

public Fighter(String name, int attackPower) {
super(name, attackPower);
}

@Override
int attack() {
return attackPower;
}

@Override
int attack(AttackType attackType) {
int aP = attackPower;
if (attackType == AttackType.melee){
aP = attackPower * 2;
}else if (attackType == AttackType.ranged){
aP = attackPower;
}
return aP;
}
}

class Mage extends Character{

public Mage(String name, int attackPower) {
super(name, attackPower);
}

@Override
int attack() {
return attackPower;
}

@Override
int attack(AttackType attackType) {
int serangan = attackPower;
if (attackType == AttackType.fire){
serangan = attackPower * 3;
}else if (attackType == AttackType.frost){
serangan = attackPower * 2;
}
return serangan;
}
}

public class No1{
public static void printAttack(Character character){
System.out.println("Nama : "+ character.getName());
System.out.println("---Attack Information---");
System.out.println("Attack Power : "+ character.getAttackPower());
if (character instanceof Fighter){
System.out.println("Melee : " + character.attack(AttackType.melee));
System.out.println("Ranged : "+ character.attack(AttackType.ranged));
}else if (character instanceof Mage){
System.out.println("Fire : "+ character.attack(AttackType.fire));
System.out.println("Frost : "+ character.attack(AttackType.frost));
}
}
public static void main(String[] args) {
Fighter fighter = new Fighter("Cheryl", 100);
Mage mage = new Mage("Dipa", 80);
Fighter fighter2 = new Fighter("Salsa", 50);
Fighter fighter3 = new Fighter("Awa", 70);
Mage mage2 = new Mage("Nakita", 60);

Character[] hero = new Character[5];
hero[0] = fighter;
hero[1] = mage;
hero[2] = fighter2;
hero[3] = fighter3;
hero[4] = mage2;

for (Character i : hero){ //perulangan utk panggil objek yg ada pada hero trs mau diprint attackPowernya
printAttack(i);
System.out.println("");
}
}
}

enum AttackType{
melee, frost, fire, ranged
}


54 changes: 54 additions & 0 deletions src/H071221077/Pertemuan_7/No2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Product<P>{
protected String name;
protected P price;
protected String expDate;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public P getPrice() {
return price;
}

public String getExpDate() {
return expDate;
}

public Product(String name, P price, String expDate) {
this.name = name;
this.price = price;
this.expDate = expDate;
}
}

// class Koin{
// private int koin;

// public int getKoin() {
// return koin;
// }

// public Koin(int koin) {
// this.koin = koin;
// }
// }

public class No2{
public static void main(String[] args) {
Product<Integer> product = new Product<>("Kinderjoy", 10000, "2023-05-01");
Product<String> product2 = new Product<>("Sari Roti", "Rp. 15.000", "2023-05-20");
Product<Double> product3 = new Product<>("Susu Kurma", 7.5, "2023-06-01");
//Product<Koin> product4 = new Product<>("Uang", new Koin(10), "2023-06-01");

System.out.println("Product 1: " + product.getName() + " - " + product.getPrice() + " - " + product.getExpDate());
System.out.println("Product 2: " + product2.getName() + " - " + product2.getPrice() + " - " + product2.getExpDate());
System.out.println("Product 3: " + product3.getName() + " - " + product3.getPrice() + " - " + product3.getExpDate());
//System.out.println("Product 4: " + product4.getName() + " - " + product4.getPrice().getKoin() + " - " + product4.getExpDate());

}
}
75 changes: 75 additions & 0 deletions src/H071221077/Pertemuan_7/No3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.util.ArrayList;
import java.util.List;

public class No3{
public static void main(String[] args) {
Food burger = FoodFactory.getFood(FoodType.burger);
Food pizza = FoodFactory.getFood(FoodType.pizza);
Food steak = FoodFactory.getFood(FoodType.steak);

List<Food> foods = new ArrayList<>();
foods.add(burger);
foods.add(pizza);
foods.add(steak);

int total = Restaurant.calculateTotal(foods);
System.out.println("Total price: "+ total);
}
}

class FoodFactory{
static Food getFood(FoodType food){
if (food == FoodType.burger){ // tipe data "Food" krn sesuai jenisnya
return new Burger();
}else if (food == FoodType.pizza){
return new Pizza();
}else if (food == FoodType.steak){
return new Steak();
}else{
System.out.println("Error");
return null;
}
}
}

interface Food{
public int getPrice();
}

class Burger implements Food{

@Override
public int getPrice() {
return 8000;
}
}

class Pizza implements Food{

@Override
public int getPrice() {
return 10000;
}
}

class Steak implements Food{

@Override
public int getPrice() {
return 15000;
}
}

class Restaurant{
static int calculateTotal(List<Food> foods){
int totalPrice = 0;
for (Food food : foods){
totalPrice += food.getPrice();
}
return totalPrice;
}
}

enum FoodType{
burger, pizza, steak
}
Loading