Guest User

Untitled

a guest
Feb 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. package model;
  2.  
  3. /**
  4.  * Data model for a Stock that can be bought or sold in the StockTicker game.
  5.  *
  6.  * This is a really simple model, with no data validation or business rules.
  7.  *
  8.  * @author Jim Parry
  9.  *
  10.  */
  11. public class Stock {
  12.  
  13.     /** trading symbol for this stock; up to four letters, capitalized */
  14.     private String symbol;
  15.     /** full name of this stock */
  16.     private String name;
  17.     /** trading value for this stock (buy or sell); range 5-195 */
  18.     private int value;
  19.  
  20.     //-------------------------------------------------------------
  21.     //      C o n s t r u c t o r s
  22.     //-------------------------------------------------------------
  23.     /**
  24.      * Default constructor.
  25.      * Initialize all fields.
  26.      */
  27.     public Stock() {
  28.         this.symbol = "?";
  29.         this.name = "?";
  30.         this.value = 100;
  31.     }
  32.  
  33.     /**
  34.      * Convenience constructor; initialize all fields as specified.
  35.      *
  36.      * @param symbol    Trading symbol for the new stock
  37.      * @param name      Full name of the new stock
  38.      * @param value     Starting share value for the new stock
  39.      */
  40.     public Stock(String symbol, String name, int value) {
  41.         this.symbol = symbol;
  42.         this.name = name;
  43.         this.value = value;
  44.     }
  45.  
  46.     //-------------------------------------------------------------
  47.     //      A c c e s s o r s
  48.     //-------------------------------------------------------------
  49.     /**
  50.      * Return the trading symbol of this stock.
  51.      * @return the symbol
  52.      */
  53.     public String getSymbol() {
  54.         return symbol;
  55.     }
  56.  
  57.     /**
  58.      * Return the full name of this stock.
  59.      * @return the name
  60.      */
  61.     public String getName() {
  62.         return name;
  63.     }
  64.  
  65.     /**
  66.      * Return the per share value of this stock.
  67.      * @return the value
  68.      */
  69.     public int getValue() {
  70.         return value;
  71.     }
  72.  
  73.     //-------------------------------------------------------------
  74.     //      M u t a t o r s
  75.     //-------------------------------------------------------------
  76.     /**
  77.      * Change the trading symbol of this stock.
  78.      *
  79.      * Business rules: has to be uppercase, not null or empty,
  80.      * letters only, and no more than 4 letters long
  81.      * Ignore bad requests.
  82.      *
  83.      * @param symbol the symbol to set
  84.      */
  85.     public void setSymbol(String symbol) {
  86.         // not null or empty
  87.         if ((symbol == null) || (symbol.length() == 0)) {
  88.             return;
  89.         }
  90.         // matches all numbers and all uppercase
  91.         if (!symbol.matches("^[A-Z]+$")) {
  92.             return;
  93.         }
  94.         //check for length greater than 4
  95.         if (symbol.length() > 4) {
  96.             return;
  97.         }
  98.  
  99.         this.symbol = symbol;
  100.  
  101.     }
  102.  
  103.     /**
  104.      * Change the full name of this stock.
  105.      *
  106.      * Business rules: cannot be null or empty, and no longer than 30 characters.
  107.      * Ignore bad requests.
  108.      *
  109.      * @param name the name to set
  110.      */
  111.     public void setName(String name) {
  112.         //check for null or empty
  113.         if ((name == null) || (name.length() == 0)) {
  114.             return;
  115.         }
  116.         //check for length greater than 30
  117.         if (name.length() > 30) {
  118.             return;
  119.         }
  120.  
  121.         this.name = name;
  122.     }
  123.  
  124.     /**
  125.      * Change the per share value of this stock.
  126.      *
  127.      * Business rules: must be in the range 5-195, and a multiple of 5
  128.      * Ignore bad requests.
  129.      *
  130.      * @param value the value to set
  131.      */
  132.     public void setValue(int value) {
  133.         //check for values inbetween 5 and 195
  134.         if ((value < 5) || (value > 195)) {
  135.             return;
  136.         }
  137.         //check for multiples of 5 only (using modulus)
  138.         if (value % 5 != 0) {
  139.             return;
  140.         }
  141.         this.value = value;
  142.     }
  143.  
  144.     //-------------------------------------------------------------
  145.     //      U t i l i t y   m e t h o d s
  146.     //-------------------------------------------------------------
  147.     /**
  148.      * Return a text representation for this stock.
  149.      * @return Text representation
  150.      */
  151.     public String toString() {
  152.         return this.symbol + ": " + this.name + " (" + this.value + ")";
  153.     }
  154.  
  155.     /**
  156.      * Equality testing, based on the stock symbol only
  157.      */
  158.     public boolean equals(Object x) {
  159.         Stock them = (Stock) x;
  160.         return this.getSymbol().equals(them.getSymbol());
  161.     }
  162. }
Add Comment
Please, Sign In to add comment