0% found this document useful (0 votes)
11 views

Program java

java

Uploaded by

alifrasyidf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Program java

java

Uploaded by

alifrasyidf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Src

Com

Auth

Config

DB.java

package com.auth.config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@SuppressWarnings("CallToPrintStackTrace")
public class DB {
private static final String JDBC_DRIVER =
"com.mysql.cj.jdbc.Driver";
private static final String DB_URL =
"jdbc:mysql://localhost:3306/gamedb";
private static final String USER = "root";
private static final String PASSWORD = "spensa92";

static {
try {
Class.forName(JDBC_DRIVER); // Load driver saat aplikasi
dijalankan
} catch (ClassNotFoundException e) {
System.out.println("Driver MySQL tidak ditemukan!");
e.printStackTrace();
}
}

// Membuat koneksi ke database


private static Connection getConnection() throws
SQLException {
return DriverManager.getConnection(DB_URL, USER,
PASSWORD);
}

// Mencari akun berdasarkan email


public static int cariData(String eml, String pwd) {
String query = "SELECT ID FROM akun WHERE email = ?";
try (Connection conn = getConnection();
PreparedStatement stmt =
conn.prepareStatement(query)) {
stmt.setString(1, eml);

try (ResultSet rs = stmt.executeQuery()) {


if (rs.next()) {
return rs.getInt("ID");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0; // Jika tidak ditemukan, kembalikan 0
}

// Login akun
public static String authAkun(int id, String eml, String
password) {
String query = "SELECT EMAIL, PASSWORD FROM akun
WHERE ID = ?";
try (Connection conn = getConnection();
PreparedStatement stmt =
conn.prepareStatement(query)) {

stmt.setInt(1, id);

try (ResultSet rs = stmt.executeQuery()) {


if (rs.next()) {
String dbEmail = rs.getString("EMAIL");
String dbPassword = rs.getString("PASSWORD");

if (dbEmail.equals(eml) &&
dbPassword.equals(password)) {
return "Login Success";
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return "Incorrect email or password";
}

// Register akun
public static boolean register(String email, String password) {
String query = "INSERT INTO akun (EMAIL, PASSWORD)
VALUES (?, ?)";
try (Connection conn = getConnection();
PreparedStatement stmt =
conn.prepareStatement(query)) {

stmt.setString(1, email);
stmt.setString(2, password);

int rowsAffected = stmt.executeUpdate();


return rowsAffected > 0; // Return true jika berhasil
menambahkan data
} catch (SQLException e) {
e.printStackTrace();
}
return false; // Jika gagal
}
}
View

LoginExtendSuperV.java

package com.auth.view;

import com.auth.config.DB;
import com.main.Game;
public class LoginExtendSuperV extends SuperV {

public LoginExtendSuperV (){


}

public void viewLogin(){


String msg = "";
do {

view();
String email = getEmail();
String password = getPassword();
int a = DB.cariData(email, password);
if (a > 0){
msg = DB.authAkun(a, email, password);
System.out.println(msg);
}else{
System.out.println("Account is not registered yet");
}
} while (!msg.equals("Login Success"));
//langsung keprogram
Game game = new Game();

game.start();
}
}

LR_View.java

package com.auth.view;

import java.util.Scanner;

public class LR_View {


public static void view(){

LoginExtendSuperV login = new LoginExtendSuperV();


RegisterExtendSuperV register = new
RegisterExtendSuperV();
try (Scanner scanner = new Scanner(System.in)) {
int option;
do{
System.out.print(
"""
\n=== Welcome to TextBased-RPG Game
===
1. Login
2. Register
3. Exit
Choose your option[1/2/3]: """);

option = scanner.nextInt();
switch (option) {
case 1 -> login.viewLogin();
case 2 -> register.viewRegister();
case 3 -> System.out.println("Goodbye!");
default -> System.out.println("Invalid option. Please
choose again.");
}
} while (option !=3);
}
}
}

RegisterExtendSuperV.java

package com.auth.view;

import com.auth.config.DB;

public class RegisterExtendSuperV extends SuperV {


@SuppressWarnings("static-access")
public void viewRegister(){

view();
String email = getEmail();
String password = getPassword();
DB register = new DB();
int a = DB.cariData(email, password);
if(a > 0){
System.out.println("Email already exsist!!");

}else{
if (register.register(email, password)) {
System.out.println("Account successfully
registered");
} else{
System.out.println("Failed to register");
}
}
}

SuperV

package com.auth.view;

import java.util.Scanner;

public class SuperV {

private String email;


private static String password;
private static final Scanner scanner = new Scanner(System.in);

public SuperV(){
// this.email = email;
// this.password = password;
}

public void setEmail(){


this.email = scanner.nextLine();
}

public void setPassword(){


password = scanner.nextLine();
}
public String getEmail(){
return email;
}

public String getPassword(){


return password;
}

public void view(){


System.out.print(
"""
\nPlease Enter Your Email!!
Email: """);
setEmail();

System.out.print(
"""
\nPlease Enter Your Password!!
Password: """);
setPassword();
}

}
Main
Armor.java

package com.main;
class Armor{
private int defencePower;
private String name;

Armor(String name, int defencePower){


this.name = name;
this.defencePower = defencePower;
}

public int getDefencePower(){


return defencePower;
}

public void setDefencePower(int defencePower){


this.defencePower = defencePower;
}

public String getName(){


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

void display(){
System.out.println("Armor: " + getName() + ", Def: " +
getDefencePower());
}
}

Character.java

package com.main;
class Character{
private String name;
private int health;
private int attackPower;

public Character(String name, int health, int attackPower){


this.name = name;
this.health = health;
this.attackPower = attackPower;
}

public String getName(){


return name;
}

public void setName(String name){


this.name = name;
}

public int getHealth(){


return health;
}

public void setHealth(int health){


this.health = health;
}

public int getAttackPower(){


return attackPower;
}

public void setAttackPower(int attackPower){


this.attackPower = attackPower;
}
public boolean isAlive(){
return health > 0;
}

public void attack(Character opponent){


System.out.println(getName() + " attack " +
opponent.getName() + " with damage " + getAttackPower());
opponent.takeDamage(getAttackPower());
}

public void takeDamage(int damage){


setHealth(getHealth() - damage);
System.out.println(getName() + " suffered damage " +
damage + "\n");

if(getHealth() <= 0){


System.out.println(getName() + " is dead");
}
}

public void displayStatus(){


System.out.println(getName() + " - Health " + getHealth());
}
}

Game.java
package com.main;
import java.util.Scanner;

public class Game{


private Player player;
private Monster monster;
private Armor armor;
private Weapon weapon;
private final Scanner scanner;

public Game(){
scanner = new Scanner(System.in);
}

public void start(){


System.out.println("\n==== Welcome to Text-based RPG!
====\n");
System.out.print("Enter username: ");
String username = scanner.nextLine();
player = new Player(username);
choseWeapon();
choseArmor();
player.display();
System.out.println("\n*** Monster appear! ***");
System.out.println("Goblin !!!");
System.out.println("Defeat the monster!\n");
monster = new Monster("Goblin");

battle();
}

private void battle(){


while (player.isAlive() && monster.isAlive()){
player.displayStatus();
monster.displayStatus();

System.out.println("\nWhat do you want to do?");


System.out.println("1. Attack");
System.out.println("2. Heal");
System.out.println("3. Run");
System.out.print(">> ");
int choice = scanner.nextInt();
System.out.println("");
switch(choice){
case 1 -> player.attack(monster);
case 2 -> player.heal();
case 3 -> {
System.out.println("You run from the monster!");
System.out.println("The monster is chasing
you!!\n");
}
default -> {
System.out.println("Invalid choice!");
continue;
}
}
if(monster.isAlive()){
monster.attack(player);
}
}

if(player.isAlive()){
System.out.println("\nYou win!\n");
}else{
System.out.println("\nYou lose!\n");
}
}

private void choseWeapon(){


Weapon weapon1 = new Weapon("Sword", 20);
Weapon weapon2 = new Weapon("Spear", 20);
Weapon weapon3 = new Weapon("Sickle", 20);
System.out.println("\nPlease Choose a weapon !");
System.out.println("1. Sword");
System.out.println("2. Spear");
System.out.println("3. Sickle");
System.out.print(">> ");
int choiceWeapon = scanner.nextInt();
switch(choiceWeapon){
case 1 -> {
weapon = weapon1;
player.equipWeapon(weapon);
}
case 2 -> {
weapon = weapon2;
player.equipWeapon(weapon);
}
case 3 -> {
weapon = weapon3;
player.equipWeapon(weapon);
}
default -> {
System.out.println("Invalid choice");
choseWeapon();
}
}
}

private void choseArmor(){


Armor armor1 = new Armor("Steel Armor", 10);
Armor armor2 = new Armor("Iron Armor", 10);
System.out.println("\nPlease chose a Armor!");
System.out.println("1. Steel Armor");
System.out.println("2. Iron Armor");
System.out.print(">> ");
int choiceArmor = scanner.nextInt();
switch(choiceArmor){
case 1 -> {
armor = armor1;
player.equipArmor(armor);
}
case 2 -> {
armor = armor2;
player.equipArmor(armor);
}
default -> {
System.out.println("Invalid choice");
choseArmor();
}
}
}
}

MonsterCharacter.java

package com.main;
class Monster extends Character{
public Monster(String name){
super(name, 90, 20);
}

@Override
public void attack(Character opponent){
System.out.println(getName() + " viciously attacks " +
opponent.getName());
super.attack(opponent);
}

@Override
public void takeDamage(int damage){
System.out.println(getName() + " roars in pain!");
super.takeDamage(damage);
}
}

PlayerCharacter.java
package com.main;
class Player extends Character{
private int healAmount;
Weapon weapon;
Armor armor;

public Player(String name){


super(name, 100, 10);
this.healAmount = 15;
}

public void setHealAmount(int healAmount){


this.healAmount = healAmount;
}

public int getHealAmount(){


return healAmount;
}
public void heal(){
System.out.println(getName() + " heal him self. ");
setHealth(getHealth() + getHealAmount());
if(getHealth() > 100){
setHealth(100);
}
System.out.println("Current Healt: " + getHealth() + "\n");
}

void equipWeapon(Weapon weapon){


this.weapon = weapon;
this.setAttackPower(weapon.getDamage() + 10);
}

void equipArmor(Armor armor){


this.armor = armor;
}

void display(){
System.out.println("\n+++++++++++++++++++++++++++");
System.out.println("Name: " + getName());
if(getHealth() <= 0){
System.out.println("(Player dead!!!)");
}else{
System.out.println("HP: " + getHealth());
weapon.display();
armor.display();
System.out.println("Total attack: " + getAttackPower());
}
System.out.println("+++++++++++++++++++++++++++");
}

@Override
public void attack(Character opponent){
System.out.println(getName() + " performs a powerful
strike on " + opponent.getName());
super.attack(opponent);
}

@Override
public void takeDamage(int damage){
if(armor != null){
int reduceDamage = damage - armor.getDefencePower();
if(reduceDamage < 0){
reduceDamage = 0;
}
System.out.println(getName() + "'s armor reduces the
damage " + armor.getDefencePower());
super.takeDamage(reduceDamage);
}else{
super.takeDamage(damage);
}
}
}

Weapon.java
package com.main;
class Weapon{
private String name;
private int damage;

Weapon(String name, int damage){


this.name = name;
this.damage = damage;
}

public String getName(){


return name;
}

public void setName(String name){


this.name = name;
}

public int getDamage(){


return damage;
}

public void setDamage(int damage){


this.damage = damage;
}

void display(){
System.out.println("Weapon: " + getName() + ", Damage: "
+ getDamage());
}

App.java

import com.auth.view.LR_View;

public class App {


@SuppressWarnings("static-access")
public static void main(String[] args){

LR_View initialDisplay = new LR_View();


initialDisplay.view();
// Register.register();

// Game game = new Game();


// game.start();
// System.out.println("===== code by: Kelompok 1 TID 23=====");
}
}

You might also like