Advertisement
Guest User

Untitled

a guest
Mar 31st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. package model.account;
  2.  
  3. /**
  4.  * Implementation of Account Interface.
  5.  *
  6.  */
  7. public final class ABCAccountImpl implements Account {
  8.  
  9.     private final String username;
  10.     private String nickname;
  11.     private String password;
  12.     private int bestScore;
  13.     private Settings settings;
  14.  
  15.     private ABCAccountImpl(final String username, final String nickname, final String password, final int bestScore, final Settings settings) {
  16.         if (username == null || password == null) {
  17.             throw new IllegalArgumentException();
  18.         }
  19.         this.username = username;
  20.         this.nickname = nickname;
  21.         this.password = password;
  22.         this.bestScore = bestScore;
  23.         this.settings = settings;
  24.     }
  25.  
  26.     /**
  27.      * Create a simple Account.
  28.      * @param username the account username
  29.      * @param password the account password
  30.      * @return the Account
  31.      */
  32.     public static Account createSimpleAccount(final String username, final String password) {
  33.         return new ABCAccountImpl(username, username, password, 0, Settings.DEFAULT);
  34.     }
  35.  
  36.     /**
  37.      * Create an Account with nickname.
  38.      * @param username the account username
  39.      * @param password the account password
  40.      * @param nickname the account nickname
  41.      * @return the Account
  42.      */
  43.     public static Account createAccountWithNickname(final String username, final String password, final String nickname) {
  44.         if (nickname == null) {
  45.             throw new IllegalArgumentException();
  46.         }
  47.         if (nickname.equals("")) {
  48.             return new ABCAccountImpl(username, username, password, 0, Settings.DEFAULT);
  49.         } else {
  50.             return new ABCAccountImpl(username, nickname, password, 0, Settings.DEFAULT);
  51.         }
  52.     }
  53.  
  54.     /**
  55.      * Create a complete Account.
  56.      * @param username the account username
  57.      * @param password the account password
  58.      * @param nickname the account nickname
  59.      * @param bestScore the account topScores
  60.      * @param settings the account settings
  61.      * @return the Account
  62.      */
  63.     public static Account createCompleteAccount(final String username, final String password, final String nickname, final int bestScore, final Settings settings) {
  64.         if (nickname == null || bestScore < 0) {
  65.             throw new IllegalArgumentException();
  66.         }
  67.         return new ABCAccountImpl(username, nickname, password, bestScore, settings);
  68.     }
  69.  
  70.     /**
  71.      * {@inheritDoc}
  72.      */
  73.     public String getUsername() {
  74.         return this.username;
  75.     }
  76.  
  77.     /**
  78.      * {@inheritDoc}
  79.      */
  80.     public String getNickname() {
  81.         return this.nickname;
  82.     }
  83.  
  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     public String getPassword() {
  88.         return this.password;
  89.     }
  90.  
  91.     /**
  92.      * {@inheritDoc}
  93.      */
  94.     public int getBestScore() {
  95.         return this.bestScore;
  96.     }
  97.  
  98.     /**
  99.      * {@inheritDoc}
  100.      */
  101.     public Settings getSettings() {
  102.         return settings;
  103.     }
  104.  
  105.     /**
  106.      * {@inheritDoc}
  107.      */
  108.     public void setNickname(final String nickname) {
  109.         if (nickname == null) {
  110.             throw new IllegalArgumentException();
  111.         }
  112.         this.nickname = nickname;
  113.     }
  114.  
  115.     /**
  116.      * {@inheritDoc}
  117.      */
  118.     public void setPassword(final String password) {
  119.         if (password == null) {
  120.             throw new IllegalArgumentException();
  121.         }
  122.         this.password = password;
  123.     }
  124.  
  125.     /**
  126.      * {@inheritDoc}
  127.      */
  128.     @Override
  129.     public void setBestScore(final int bestScore) {
  130.         if (bestScore < 0 || bestScore < this.bestScore) {
  131.             throw new IllegalArgumentException();
  132.         }
  133.         this.bestScore = bestScore;
  134.     }
  135.  
  136.     /**
  137.      * {@inheritDoc}
  138.      */
  139.     @Override
  140.     public void setSettings(final Settings settings) {
  141.         if (settings == null) {
  142.             throw new IllegalArgumentException();
  143.         }
  144.         this.settings = settings;
  145.     }
  146.     /**
  147.      * @see java.lang.Object#hashCode()
  148.      */
  149.     @Override
  150.     public int hashCode() {
  151.         final int prime = 31;
  152.         int result = 1;
  153.         result = prime * result + ((username == null) ? 0 : username.hashCode());
  154.         return result;
  155.     }
  156.  
  157.     /**
  158.      * @see java.lang.Object#equals(java.lang.Object)
  159.      */
  160.     @Override
  161.     public boolean equals(final Object obj) {
  162.         if (this == obj) {
  163.             return true;
  164.         }
  165.         if (obj == null) {
  166.             return false;
  167.         }
  168.         if (!(obj instanceof ABCAccountImpl)) {
  169.             return false;
  170.         }
  171.         final ABCAccountImpl other = (ABCAccountImpl) obj;
  172.         if (username == null) {
  173.             if (other.username != null) {
  174.                 return false;
  175.             }
  176.         } else if (!username.equals(other.username)) {
  177.             return false;
  178.         }
  179.         return true;
  180.     }
  181.  
  182.     /**
  183.      * @see java.lang.Object#toString()
  184.      */
  185.     @Override
  186.     public String toString() {
  187.         return "AccountImpl [username=" + username + ", nickname=" + nickname + ", password=" + password + "]";
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement