Illustrating Hash Function Operations
MapEntry.java
/*
*
* Program Description: An implementation that fulfills the requirements
* of assignment #4, CS310 Data Structures class.
*
*/
package cs310parsons;
/**
* @author Martha Parsons
* @date 10/01/2019
* @version 02
*
*
*/
public class MapEntry {
private String key;
private StockTradeNode newNode;
public MapEntry(StockTrade obj) {
newNode = new StockTradeNode(obj);
key = obj.getStockSymbol();
}
public int hashCode() {
return key.hashCode();
}
public String getKey() {
return key;
}
public StockTradeNode getNode() {
return newNode;
}
}
StockTrade.java
/*
*
* Program Description: An implementation that fulfills the requirements
* of assignment #4, CS310 Data Structures class.
*
*/
package cs310parsons;
/**
* @author Martha Parsons
* @date 10/01/2019
* @version 04
* @description - A local stock brokerage company is in need of a Java
* programmer to assist them with some of their needs. Various
* brokers in the office take care of multiple stocks in the
* broker�s portfolio.
*/
public class StockTrade {
private String stockSymbol;
private String symbol;
private double pricePerShare;
private int wholeShares;
private String brokerLicense;
private boolean taxable;
/**
* @param stockTradeArr
*
*/
public StockTrade(String[] stockTradeArr) {
setStockTradeAttributes(stockTradeArr);
}
/**
* @return
*/
public String getStockSymbol() {
return stockSymbol;
}
/**
* @return
*/
public Double getPricePerShare() {
return pricePerShare;
}
/**
* @return
*/
public int getWholeShares() {
return wholeShares;
}
/**
* @return
*/
public String getBrokerLicense() {
return brokerLicense;
}
/**
* @param stockSymbol
*/
/**
* @param stockSymbol - Stock symbol (Unique 3 or 4-char string, all
* uppercased)
* @param pricePerShare - Stock price per share (double)
* @param wholeShares - Stock number of shares (int) NOTE: Only whole shares
* can be traded for this program
* @param brokerLicense - Broker�s license number (to match license number in
* Broker)
* @param taxable - Whether trade is taxable (Boolean)
*/
public void setStockTradeAttributes(String[] arr) {
this.stockSymbol = arr[2];
this.symbol = stockSymbol;
this.pricePerShare = Double.parseDouble(arr[3]);
this.wholeShares = Integer.parseInt(arr[4]);
this.brokerLicense = (arr[5]);
this.taxable = "Y".equals(arr[6]);
}
/**
* @return
*/
public boolean isTaxable() {
return taxable;
}
@Override
public int hashCode() {
int hash = symbol.hashCode();
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StockTrade other = (StockTrade) obj;
if (brokerLicense == null) {
if (other.brokerLicense != null)
return false;
} else if (!brokerLicense.equals(other.brokerLicense))
return false;
if (Double.doubleToLongBits(pricePerShare) != Double.doubleToLongBits(other.pricePerShare))
return false;
if (stockSymbol == null) {
if (other.stockSymbol != null)
return false;
} else if (!stockSymbol.equals(other.stockSymbol))
return false;
if (taxable != other.taxable)
return false;
if (wholeShares != other.wholeShares)
return false;
return true;
}
/**
*
*/
@Override
public String toString() {
return "StockTrade {stockSymbol=" + stockSymbol + ", pricePerShare=" + pricePerShare + ", wholeShares="
+ wholeShares + ", brokerLicense=" + brokerLicense + ", taxable=" + taxable + "}";
}
/**
* @param stockSymbol
* @return
*/
public boolean isValidStockSymbol(String stockSymbol) {
int stockSymbolLength = stockSymbol.length();
if (stockSymbolLength <3 || stockSymbolLength >4) { // is 3 or 4 characters long
return false;
}
return true;
}
/**
* @param d
* @return
*/
public boolean isValidPrice(double d) {
if (pricePerShare >1000 && pricePerShare <0) {
return false; // Check the stock price is not over $1000.00 per share
}
return true;
}
/**
* @param i
* @return
*/
public boolean isValidWholeShares(int i) {
if (wholeShares >100000 && wholeShares <0) {
return false; // Check the number of shares is not over 100,000
}
return true;
}
StockTradeLogImpl.java
/*
*
* Program Description: An implementation that fulfills the requirements
* of assignment #4, CS310 Data Structures class.
*
*/
package cs310parsons;
/**
* @author Martha Parsons
* @date 10/01/2019
* @version 02
*
*
*/
public class StockTradeLogImpl {
private int MAX_SIZE;
private MapEntry[] StockTradeHashMap;
private MapEntry insp;
/**
* constructor
*/
public StockTradeLogImpl() {
// This means that each StockTrade object that maps to the same index (via the hashcode) will be stored in a linked list at that index.
// The array size for this implementation will be 17 (another prime number).
MAX_SIZE = 17;
StockTradeHashMap = new MapEntry[MAX_SIZE];
for (int i = 0; i < StockTradeHashMap.length; i++){
StockTradeHashMap[i] = null;
}
}
public void add(StockTrade obj) {
MapEntry newEntry = new MapEntry(obj);
int hashCode = newEntry.hashCode();
hashCode = hashCode % MAX_SIZE;
MapEntry currEntry;
if (StockTradeHashMap[hashCode] == null){
StockTradeHashMap[hashCode] = newEntry;
}
else{
currEntry = StockTradeHashMap[hashCode];
newEntry.getNode().setNextNode(currEntry);
StockTradeHashMap[hashCode] = newEntry;
}
}
public StockTrade find(String stocksymbol){
boolean found = false;
boolean done = false;
int ascii = 0;
int hashCode;
StockTrade sought = null;
for (int i = 0; i < stocksymbol.length(); i++){
ascii = (int) stocksymbol.charAt(i);
ascii += ascii;
}
hashCode = ascii % MAX_SIZE;
if (StockTradeHashMap[hashCode] == null){
return null;
}
StockTradeNode node = StockTradeHashMap[hashCode].getNode();
while (!found && !done) {
if (node.getStockTrade().getStockSymbol().equalsIgnoreCase(stocksymbol)){
found = true;
sought = node.getStockTrade();
}
else {
node = node.getNextNode().getNode();
done = node == null;
}
}
return sought;
}
/**
* Print the entire current hashMap in the terminal
*/
public void displayHash(){
String key;
boolean end;
System.out.println("\nStockTrade HashTable: ");
for (int i = 0; i < StockTradeHashMap.length; i++){
end = false;
insp = StockTradeHashMap[i];
if (insp == null){
System.out.println(" Index " + i + " is empty");
}
else{
key = insp.getKey();
System.out.print(" Index " + i + " contains StockTrades: ");
while (!end){
if (insp.getNode().getNextNode() == null){
System.out.print(key + "\n");
end = true;
}
else{
System.out.print(key + " ");
insp = insp.getNode().getNextNode();
key = insp.getKey();
}
}
}
}
}
}