OOP Concepts Practice Project in Java

 A simple Game using Java OOP Concepts


1. "Tools" Abstract class and Parent class to class Player

abstract class Tools {
    abstract void punch(int point);
    abstract void kick(int point);
    abstract void shoot(int point);
    abstract void getPunch(int healths);
    abstract void getKick(int healths);
    abstract void getShoot(int healths);
}

2. "Player" Parent class to classes American, African, Asian & child class to class Tools

public class Player extends Tools{
    private String name;
    private int points;
    private int health;

    public Player(String name, int points, int health){
        this.name = name;
        this.points = points;
        this.health = health;
    }

    int getPoints(){
        return points;
    }
    int getHealth(){
        return health;
    }

    void punch(int point){
        points = points + point;
    }

    void kick(int point){
        points = points + point;
    }

    void shoot(int point){
        points = points + point;
    }

    void getPunch(int healths){
        health = health -healths;
    }

    void getKick(int healths){
        health = health -healths;
    }

    void getShoot(int healths){
        health = health -healths;
    }
}

3. "American" Child class to class Player

import java.util.Scanner;
public class American extends Player{

    Scanner scan = new Scanner(System.in);

    public American(String name, int points, int health) {
        super(name, points, health);
    }
   
    void play(){
        System.out.println("Select an option : ");
        int option = scan.nextInt();
        if(option == 1){
            punch(10);
            System.out.println("You earned 10 points!!");

        }else if(option == 2){
            kick(20);
            System.out.println("You earned 20 points!!");

        }else if(option == 3){
            shoot(30);
            System.out.println("You earned 30 points!!");
           
        }else if(option == -1){
            getPunch(20);
            System.out.println("You lost 20 points!!");

        }else if(option == -2){
            getKick(10);
            System.out.println("You lost 10 points!!");

        }else if(option == -3){
            getShoot(20);
            System.out.println("You lost 20 points!!");
           
        }else{
            System.out.println("Please select a correct option!");
        }
       
    }

}

4. "African" Child class to class Player

import java.util.Scanner;
public class African extends Player{

    Scanner scan = new Scanner(System.in);

    public African(String name, int points, int health) {
        super(name, points, health);
    }
   
    void play(){
        System.out.println("Select an option : ");
        int option = scan.nextInt();
        if(option == 1){
            punch(30);
            System.out.println("You earned 30 points!!");

        }else if(option == 2){
            kick(20);
            System.out.println("You earned 20 points!!");

        }else if(option == 3){
            shoot(10);
            System.out.println("You earned 10 points!!");
           
        }else if(option == -1){
            getPunch(10);
            System.out.println("You lost 10 points!!");

        }else if(option == -2){
            getKick(10);
            System.out.println("You lost 10 points!!");

        }else if(option == -3){
            getShoot(30);
            System.out.println("You lost 30 points!!");
           
        }else{
            System.out.println("Please select a correct option!");
        }
    }
}

5. "Asian" Child class to class Player

import java.util.Scanner;
public class Asian extends Player{

    Scanner scan = new Scanner(System.in);

    public Asian(String name, int points, int health) {
        super(name, points, health);
    }

    void play(){
        System.out.println("Select an option : ");
        int option = scan.nextInt();
        if(option == 1){
            punch(20);
            System.out.println("You earned 20 points!!");

        }else if(option == 2){
            kick(10);
            System.out.println("You earned 10 points!!");

        }else if(option == 3){
            shoot(20);
            System.out.println("You earned 20 points!!");
           
        }else if(option == -1){
            getPunch(20);
            System.out.println("You lost 20 points!!");

        }else if(option == -2){
            getKick(20);
            System.out.println("You earned 20 points!!");

        }else if(option == -3){
            getShoot(10);
            System.out.println("You earned 10 points!!");
           
        }else{
            System.out.println("Please select a correct option!");
        }
    }  
}

6. "Main" The class which has main method

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter your name :");
        String name = scan.nextLine();

        System.out.println("=========================");
        System.out.println("Hello " + name);
        System.out.println("Welcome to the Game!");
        System.out.println("=========================");

        System.out.println("Which Player you want to select : \n1. American\n2. African\n3. Asian");
        int choice = scan.nextInt();
       
        System.out.println("Attack & Damage Options");
        System.out.println("1. Punch\n2. Kick\n3. Shoot\n-1. Get Punched\n-2. Get Kicked\n-3 Get Shot");
        System.out.println("If you earn 200 points, you win\nIf your health is zero, you lost");
       
        if(choice == 1){
            American p1 = new American(name, 100, 80);                
            System.out.println("=========================\nYou total points are " + p1.getPoints());
            System.out.println("You healt points are " + p1.getHealth() +"\n=========================");
            while(true){
                p1.play();
                if(p1.getPoints() == 200){
                    System.out.println("Congratulatios !!!\n You won the Game !!!\nYou have all taken 200 points.");
                    System.out.println("========================\n  Thank you");
                    break;
                }else if(p1.getHealth() == 0){
                    System.out.println("Sorryyyyyyy !!!\n You Lost the Game !!!\nYou have no points to play more.");
                    System.out.println("========================\n  Thank you");
                    break;
                }
                System.out.println("You total points are " + p1.getPoints());
                System.out.println("You health points are " + p1.getHealth());
            }
           
        }else if(choice == 2){
            African p1 = new African(name, 100, 120);
            System.out.println("=========================\nYou total points are " + p1.getPoints());
            System.out.println("You healt points are " + p1.getHealth() +"\n=========================");
            while(true){
                p1.play();
                if(p1.getPoints() == 200){
                    System.out.println("Congratulatios !!!\n You won the Game !!!\nYou have all taken 200 points.");
                    System.out.println("========================\n  Thank you");
                    break;
                }else if(p1.getHealth() == 0){
                    System.out.println("Sorryyyyyyy !!!\n You Lost the Game !!!\nYou have no points to play more.");
                    System.out.println("========================\n  Thank you");
                    break;
                }
                System.out.println("You total points are " + p1.getHealth());
                System.out.println("You health points are " + p1.getHealth());
            }
        }else if(choice == 3){
            Asian p1 = new Asian(name, 100, 100);
            System.out.println("=========================\nYou total points are " + p1.getPoints());
            System.out.println("You healt points are " + p1.getHealth() +"\n=========================");
            while(true){
                p1.play();
                if(p1.getPoints() == 200){
                    System.out.println("Congratulatios !!!\n You won the Game !!!\nYou have all taken 200 points.");
                    System.out.println("========================\n  Thank you");
                    break;
                }else if(p1.getHealth() == 0){
                    System.out.println("Sorryyyyyyy !!!\n You Lost the Game !!!\nYou have no points to play more.");
                    System.out.println("========================\n  Thank you");
                    break;
                }
                System.out.println("You total points are " + p1.getPoints());
                System.out.println("You health points are " + p1.getHealth());
            }
        }else{
            System.out.println("Wrong Choice\n======================\nThank you!!!");
        }
    }
}

Thank you

Comments

Popular posts from this blog

Search results in MySQL database using PHP

Introduction to React Native Navigation