Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- class hero {
- protected String nama;
- protected String id;
- protected int hp;
- protected int atk;
- protected int lv;
- protected int sm;
- protected int bm;
- hero(){
- }
- public void lvUp(){
- }
- public String getNama() {
- return nama;
- }
- public void setNama(String nama) {
- this.nama = nama;
- }
- public int getHp() {
- return hp;
- }
- public void setHp(int hp) {
- this.hp = hp;
- }
- public int getAtk() {
- return atk;
- }
- public void setAtk(int atk) {
- this.atk = atk;
- }
- public int getLv() {
- return lv;
- }
- public void setLv(int lv) {
- this.lv = lv;
- }
- public int getSm() {
- return sm;
- }
- public void setSm(int sm) {
- this.sm = sm;
- }
- public int getBm() {
- return bm;
- }
- public void setBm(int bm) {
- this.bm = bm;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- }
- class swordman extends hero{
- swordman(String nama,String id){
- this.id=id;
- this.nama=nama;
- hp=100;
- atk=10;
- lv=1;
- sm=1;
- bm=0;
- }
- public void lvUp(){
- lv++;
- hp+=100;
- atk+=10;
- setSm(getSm() + 1);
- }
- }
- class archer extends hero{
- archer(String nama,String id){
- this.id=id;
- this.nama=nama;
- hp=100;
- atk=10;
- lv=1;
- sm=0;
- bm=1;
- }
- public void lvUp(){
- lv++;
- hp+=100;
- atk+=10;
- setBm(getBm() + 1);
- }
- }
- public class TugasAOOP {
- static Scanner x=new Scanner(System.in);
- static ArrayList<hero> hero=new ArrayList<hero>();
- static hero b;
- static void enter(){
- for (int i = 0; i < 25; i++) {
- System.out.println(" ");
- }
- }
- public static void main(String[] args) {
- menu();
- }
- public static void regis(){
- enter();
- String input,input2;
- int flag=0;
- do{
- flag=0;
- System.out.print("Masukan ID : ");
- input=x.nextLine();
- for (int j = 2; j < input.length(); j++) {
- if (Character.isDigit(input.charAt(j))) {
- flag++;
- }
- }
- }while(input.length()!=5||flag!=3);
- System.out.print("Masukan Nama : ");
- input2=x.nextLine();
- do{
- flag=3;
- System.out.print("Pilih Hero [Swordman/Archer] : ");
- input=x.nextLine();
- if(input.equalsIgnoreCase("swordman")){
- b=new swordman(input,input2);
- flag=1;
- hero.add(b);
- }
- else if(input.equalsIgnoreCase("archer")){
- b=new archer(input,input2);
- flag=2;
- hero.add(b);
- }
- }while(flag==3);
- System.out.println("Pendaftaran Sukses ");
- x.nextLine();
- }
- public static void view(){
- System.out.printf("|%s|%6s|%10s|\n","No.","ID","Nama");
- for (int i = 0; i < hero.size(); i++) {
- System.out.printf("|%d.|%6s|%10s|\n",i+1,hero.get(i).getId(),hero.get(i).getNama());
- }
- }
- public static void subMenu(int a){
- int flag=0;
- int p=0;
- do{
- enter();
- System.out.println("Menu");
- System.out.println("=====================");
- System.out.println("1. Training");
- System.out.println("2. View Stat");
- System.out.println("3. Back");
- do{
- System.out.print("Masukan Input : ");
- try{
- flag=1;
- p=x.nextInt();
- x.nextLine();
- }
- catch(Exception e){
- flag=0;
- System.out.println("Input Salah");
- x.nextLine();
- }
- }while(flag==0);
- switch(p){
- case 1:
- enter();
- hero.get(a).lvUp();
- System.out.println("Sukses Level Up");
- x.nextLine();
- break;
- case 2:
- enter();
- System.out.println("ID : "+hero.get(a).getId());
- System.out.println("Nama : "+hero.get(a).getNama());
- System.out.println("Level : "+hero.get(a).getLv());
- System.out.println("Attack : "+hero.get(a).getAtk());
- System.out.println("HP : "+hero.get(a).getHp());
- System.out.println("SwordMastery : "+hero.get(a).getSm());
- System.out.println("BowMastery : "+hero.get(a).getBm());
- x.nextLine();
- break;
- case 3:
- break;
- default:
- subMenu(a);
- break;
- }
- }while(p!=3);
- }
- public static void delete(int a){
- System.out.println("Deleting Data . . . ");
- System.out.println("Press <enter> to continue");
- hero.remove(a);
- x.nextLine();
- }
- public static void menu(){
- int a=0;
- int flag=0;
- do{
- enter();
- System.out.println("Menu");
- System.out.println("=====================");
- System.out.println("1. Registrasi");
- System.out.println("2. Load");
- System.out.println("3. Delete ID");
- System.out.println("4. Exit");
- do{
- System.out.print("Masukan Input : ");
- try{
- flag=1;
- a=x.nextInt();
- x.nextLine();
- }
- catch(Exception e){
- flag=0;
- System.out.println("Input Salah");
- x.nextLine();
- }
- }while(flag==0);
- switch(a){
- case 1:
- regis();
- break;
- case 2:
- enter();
- if(hero.isEmpty()){
- System.out.println("Data Kosong");
- x.nextLine();
- }
- else {
- view();
- do{
- System.out.print("Masukan No ID : ");
- try{
- flag=1;
- a=x.nextInt();
- x.nextLine();
- }
- catch(Exception e){
- flag=0;
- System.out.println("Input Salah");
- x.nextLine();
- }
- }while(flag==0||a>hero.size()||a<1);
- a--;
- subMenu(a);
- }
- break;
- case 3:
- enter();
- if(hero.isEmpty()){
- System.out.println("Data Kosong");
- x.nextLine();
- }
- else {
- view();
- do{
- System.out.print("Masukan No ID : ");
- try{
- flag=1;
- a=x.nextInt();
- x.nextLine();
- }
- catch(Exception e){
- flag=0;
- System.out.println("Input Salah");
- x.nextLine();
- }
- }while(flag==0||a>hero.size()||a<1);
- a--;
- delete(a);
- }
- break;
- case 4:
- break;
- default:
- menu();
- break;
- }
- }while(a!=4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement