Advertisement
akevintg

Hero Assignment AOOP

Nov 2nd, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.53 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. class hero {
  5.     protected String nama;
  6.     protected String id;
  7.     protected int hp;
  8.     protected int atk;
  9.     protected int lv;
  10.     protected int sm;
  11.     protected int bm;
  12.    
  13.     hero(){
  14.     }
  15.    
  16.     public void lvUp(){
  17.        
  18.     }
  19.    
  20.     public String getNama() {
  21.         return nama;
  22.     }
  23.  
  24.     public void setNama(String nama) {
  25.         this.nama = nama;
  26.     }
  27.  
  28.     public int getHp() {
  29.         return hp;
  30.     }
  31.  
  32.     public void setHp(int hp) {
  33.         this.hp = hp;
  34.     }
  35.  
  36.     public int getAtk() {
  37.         return atk;
  38.     }
  39.  
  40.     public void setAtk(int atk) {
  41.         this.atk = atk;
  42.     }
  43.  
  44.     public int getLv() {
  45.         return lv;
  46.     }
  47.  
  48.     public void setLv(int lv) {
  49.         this.lv = lv;
  50.     }
  51.  
  52.     public int getSm() {
  53.         return sm;
  54.     }
  55.  
  56.     public void setSm(int sm) {
  57.         this.sm = sm;
  58.     }
  59.    
  60.     public int getBm() {
  61.         return bm;
  62.     }
  63.  
  64.     public void setBm(int bm) {
  65.         this.bm = bm;
  66.     }
  67.    
  68.     public String getId() {
  69.         return id;
  70.     }
  71.  
  72.     public void setId(String id) {
  73.         this.id = id;
  74.     }
  75.    
  76. }
  77.  
  78.  class swordman extends hero{
  79.     swordman(String nama,String id){
  80.         this.id=id;
  81.         this.nama=nama;
  82.         hp=100;
  83.         atk=10;
  84.         lv=1;
  85.         sm=1;
  86.         bm=0;
  87.     }
  88.    
  89.     public void lvUp(){
  90.         lv++;
  91.         hp+=100;
  92.         atk+=10;
  93.         setSm(getSm() + 1);
  94.     }
  95.    
  96. }
  97.  
  98. class archer extends hero{
  99.     archer(String nama,String id){
  100.         this.id=id;
  101.         this.nama=nama;
  102.         hp=100;
  103.         atk=10;
  104.         lv=1;
  105.         sm=0;
  106.         bm=1;
  107.     }
  108.    
  109.     public void lvUp(){
  110.         lv++;
  111.         hp+=100;
  112.         atk+=10;
  113.         setBm(getBm() + 1);
  114.     }
  115. }
  116.  
  117. public class TugasAOOP {
  118.  
  119.     static Scanner x=new Scanner(System.in);
  120.     static ArrayList<hero> hero=new ArrayList<hero>();
  121.     static hero b;
  122.    
  123.     static void enter(){
  124.         for (int i = 0; i < 25; i++) {
  125.             System.out.println(" ");
  126.         }
  127.     }
  128.    
  129.     public static void main(String[] args) {
  130.         menu();
  131.     }
  132.    
  133.     public static void regis(){
  134.         enter();
  135.         String input,input2;
  136.         int flag=0;
  137.         do{
  138.             flag=0;
  139.             System.out.print("Masukan ID : ");
  140.             input=x.nextLine();
  141.             for (int j = 2; j < input.length(); j++) {
  142.                 if (Character.isDigit(input.charAt(j))) {
  143.                     flag++;
  144.                 }
  145.             }
  146.         }while(input.length()!=5||flag!=3);
  147.         System.out.print("Masukan Nama : ");
  148.         input2=x.nextLine();
  149.         do{
  150.             flag=3;
  151.             System.out.print("Pilih Hero [Swordman/Archer] : ");
  152.             input=x.nextLine();
  153.             if(input.equalsIgnoreCase("swordman")){
  154.                 b=new swordman(input,input2);
  155.                 flag=1;
  156.                 hero.add(b);
  157.             }
  158.             else if(input.equalsIgnoreCase("archer")){
  159.                 b=new archer(input,input2);
  160.                 flag=2;
  161.                 hero.add(b);
  162.             }
  163.         }while(flag==3);
  164.         System.out.println("Pendaftaran Sukses  ");
  165.         x.nextLine();
  166.     }
  167.    
  168.     public static void view(){
  169.         System.out.printf("|%s|%6s|%10s|\n","No.","ID","Nama");
  170.         for (int i = 0; i < hero.size(); i++) {
  171.             System.out.printf("|%d.|%6s|%10s|\n",i+1,hero.get(i).getId(),hero.get(i).getNama());
  172.         }
  173.     }
  174.    
  175.     public static void subMenu(int a){
  176.         int flag=0;
  177.         int p=0;
  178.         do{
  179.             enter();
  180.             System.out.println("Menu");
  181.             System.out.println("=====================");
  182.             System.out.println("1. Training");
  183.             System.out.println("2. View Stat");
  184.             System.out.println("3. Back");
  185.             do{
  186.                 System.out.print("Masukan Input : ");
  187.                 try{
  188.                     flag=1;
  189.                     p=x.nextInt();
  190.                     x.nextLine();
  191.                 }
  192.                 catch(Exception e){
  193.                     flag=0;
  194.                     System.out.println("Input Salah");
  195.                     x.nextLine();
  196.                 }
  197.             }while(flag==0);
  198.             switch(p){
  199.                 case 1:
  200.                     enter();
  201.                     hero.get(a).lvUp();
  202.                     System.out.println("Sukses Level Up");
  203.                     x.nextLine();
  204.                     break;
  205.                 case 2:
  206.                     enter();
  207.                     System.out.println("ID : "+hero.get(a).getId());
  208.                     System.out.println("Nama : "+hero.get(a).getNama());
  209.                     System.out.println("Level : "+hero.get(a).getLv());
  210.                     System.out.println("Attack : "+hero.get(a).getAtk());
  211.                     System.out.println("HP : "+hero.get(a).getHp());
  212.                     System.out.println("SwordMastery : "+hero.get(a).getSm());
  213.                     System.out.println("BowMastery : "+hero.get(a).getBm());
  214.                     x.nextLine();
  215.                     break;
  216.                 case 3:
  217.                     break;
  218.                 default:
  219.                     subMenu(a);
  220.                     break;
  221.             }
  222.         }while(p!=3);
  223.     }
  224.    
  225.     public static void delete(int a){
  226.         System.out.println("Deleting Data . . . ");
  227.         System.out.println("Press <enter> to continue");
  228.         hero.remove(a);
  229.         x.nextLine();
  230.     }
  231.    
  232.     public static void menu(){
  233.         int a=0;
  234.         int flag=0;
  235.         do{
  236.             enter();
  237.             System.out.println("Menu");
  238.             System.out.println("=====================");
  239.             System.out.println("1. Registrasi");
  240.             System.out.println("2. Load");
  241.             System.out.println("3. Delete ID");
  242.             System.out.println("4. Exit");
  243.             do{
  244.                 System.out.print("Masukan Input : ");
  245.                 try{
  246.                     flag=1;
  247.                     a=x.nextInt();
  248.                     x.nextLine();
  249.                 }
  250.                 catch(Exception e){
  251.                     flag=0;
  252.                     System.out.println("Input Salah");
  253.                     x.nextLine();
  254.                 }
  255.             }while(flag==0);
  256.             switch(a){
  257.                 case 1:
  258.                     regis();
  259.                     break;
  260.                 case 2:
  261.                     enter();
  262.                     if(hero.isEmpty()){
  263.                         System.out.println("Data Kosong");
  264.                         x.nextLine();
  265.                     }
  266.                     else {
  267.                         view();
  268.                         do{
  269.                             System.out.print("Masukan No ID : ");
  270.                             try{
  271.                                 flag=1;
  272.                                 a=x.nextInt();
  273.                                 x.nextLine();
  274.                             }
  275.                             catch(Exception e){
  276.                                 flag=0;
  277.                                 System.out.println("Input Salah");
  278.                                 x.nextLine();
  279.                             }
  280.                         }while(flag==0||a>hero.size()||a<1);
  281.                         a--;
  282.                         subMenu(a);
  283.                     }
  284.                     break;
  285.                 case 3:
  286.                     enter();
  287.                     if(hero.isEmpty()){
  288.                         System.out.println("Data Kosong");
  289.                         x.nextLine();
  290.                     }
  291.                     else {
  292.                         view();
  293.                         do{
  294.                             System.out.print("Masukan No ID : ");
  295.                             try{
  296.                                 flag=1;
  297.                                 a=x.nextInt();
  298.                                 x.nextLine();
  299.                             }
  300.                             catch(Exception e){
  301.                                 flag=0;
  302.                                 System.out.println("Input Salah");
  303.                                 x.nextLine();
  304.                             }
  305.                         }while(flag==0||a>hero.size()||a<1);
  306.                         a--;
  307.                         delete(a);
  308.                     }
  309.                     break;
  310.                 case 4:
  311.                     break;
  312.                 default:
  313.                     menu();
  314.                     break;
  315.             }
  316.         }while(a!=4);
  317.     }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement