Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package viceCity.models.guns;
  2.  
  3. import static viceCity.common.ExceptionMessages.*;
  4.  
  5. public abstract class BaseGun implements Gun {
  6.     private static final int BULLETS_MIN = 0;
  7.     private static final int TOTAL_BULLETS_MIN = 0;
  8.  
  9.     private String name;
  10.     private int bulletsPerBarrel;
  11.     private int totalBullets;
  12.  
  13.     protected BaseGun(String name, int bullets, int totalBullets) {
  14.         this.setName(name);
  15.         this.setBulletsPerBarrel(bullets);
  16.         this.setTotalBullets(totalBullets);
  17.     }
  18.  
  19.     @Override
  20.     public String getName() {
  21.         return this.name;
  22.     }
  23.  
  24.     @Override
  25.     public int getBulletsPerBarrel() {
  26.         return this.bulletsPerBarrel;
  27.     }
  28.  
  29.     @Override
  30.     public boolean canFire() {
  31.         return this.getBulletsPerBarrel() > BULLETS_MIN || this.getTotalBullets() > TOTAL_BULLETS_MIN;
  32.     }
  33.  
  34.     @Override
  35.     public int getTotalBullets() {
  36.         return this.totalBullets;
  37.     }
  38.  
  39.     private void setName(String name) {
  40.         if (name == null || name.trim().isEmpty()) {
  41.             throw new NullPointerException(NAME_NULL);
  42.         }
  43.  
  44.         this.name = name;
  45.     }
  46.  
  47.     protected void setBulletsPerBarrel(int bulletsPerBarrel) {
  48.         if (bulletsPerBarrel < BULLETS_MIN) {
  49.             throw new IllegalArgumentException(BULLETS_LESS_THAN_ZERO);
  50.         }
  51.  
  52.         this.bulletsPerBarrel = bulletsPerBarrel;
  53.     }
  54.  
  55.     protected void setTotalBullets(int totalBullets) {
  56.         if (totalBullets < TOTAL_BULLETS_MIN) {
  57.             throw new IllegalArgumentException(TOTAL_BULLETS_LESS_THAN_ZERO);
  58.         }
  59.  
  60.         this.totalBullets = totalBullets;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement