Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- /**
- * Data model for a Stock that can be bought or sold in the StockTicker game.
- *
- * This is a really simple model, with no data validation or business rules.
- *
- * @author Jim Parry
- *
- */
- public class Stock {
- /** trading symbol for this stock; up to four letters, capitalized */
- private String symbol;
- /** full name of this stock */
- private String name;
- /** trading value for this stock (buy or sell); range 5-195 */
- private int value;
- //-------------------------------------------------------------
- // C o n s t r u c t o r s
- //-------------------------------------------------------------
- /**
- * Default constructor.
- * Initialize all fields.
- */
- public Stock() {
- this.symbol = "?";
- this.name = "?";
- this.value = 100;
- }
- /**
- * Convenience constructor; initialize all fields as specified.
- *
- * @param symbol Trading symbol for the new stock
- * @param name Full name of the new stock
- * @param value Starting share value for the new stock
- */
- public Stock(String symbol, String name, int value) {
- this.symbol = symbol;
- this.name = name;
- this.value = value;
- }
- //-------------------------------------------------------------
- // A c c e s s o r s
- //-------------------------------------------------------------
- /**
- * Return the trading symbol of this stock.
- * @return the symbol
- */
- public String getSymbol() {
- return symbol;
- }
- /**
- * Return the full name of this stock.
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * Return the per share value of this stock.
- * @return the value
- */
- public int getValue() {
- return value;
- }
- //-------------------------------------------------------------
- // M u t a t o r s
- //-------------------------------------------------------------
- /**
- * Change the trading symbol of this stock.
- *
- * Business rules: has to be uppercase, not null or empty,
- * letters only, and no more than 4 letters long
- * Ignore bad requests.
- *
- * @param symbol the symbol to set
- */
- public void setSymbol(String symbol) {
- // not null or empty
- if ((symbol == null) || (symbol.length() == 0)) {
- return;
- }
- // matches all numbers and all uppercase
- if (!symbol.matches("^[A-Z]+$")) {
- return;
- }
- //check for length greater than 4
- if (symbol.length() > 4) {
- return;
- }
- this.symbol = symbol;
- }
- /**
- * Change the full name of this stock.
- *
- * Business rules: cannot be null or empty, and no longer than 30 characters.
- * Ignore bad requests.
- *
- * @param name the name to set
- */
- public void setName(String name) {
- //check for null or empty
- if ((name == null) || (name.length() == 0)) {
- return;
- }
- //check for length greater than 30
- if (name.length() > 30) {
- return;
- }
- this.name = name;
- }
- /**
- * Change the per share value of this stock.
- *
- * Business rules: must be in the range 5-195, and a multiple of 5
- * Ignore bad requests.
- *
- * @param value the value to set
- */
- public void setValue(int value) {
- //check for values inbetween 5 and 195
- if ((value < 5) || (value > 195)) {
- return;
- }
- //check for multiples of 5 only (using modulus)
- if (value % 5 != 0) {
- return;
- }
- this.value = value;
- }
- //-------------------------------------------------------------
- // U t i l i t y m e t h o d s
- //-------------------------------------------------------------
- /**
- * Return a text representation for this stock.
- * @return Text representation
- */
- public String toString() {
- return this.symbol + ": " + this.name + " (" + this.value + ")";
- }
- /**
- * Equality testing, based on the stock symbol only
- */
- public boolean equals(Object x) {
- Stock them = (Stock) x;
- return this.getSymbol().equals(them.getSymbol());
- }
- }
Add Comment
Please, Sign In to add comment