package COM.xinit.demon.co.uk.3tier.database;
// The StockRec class
// Keeps a single record instance of a stock object
//
// These are created from a dynamic query against the DB
public class StockRec {
private String symbol;
private float price;
// StockRec constructor - create a instance of a stock
// from the queried data
public StockRec (String symbol, float price) {
this.symbol = symbol;
this.price = price;
}
// StockRec constructor - create a instance of a stock
// with no data
public StockRec () {
symbol = "";
price = 0.0f;
}
/**
StockRec constructor that creates a StockRec form a single string
@param recStr the complete record in the toString format
*/
public StockRec(String recStr) {
String symbol = null;
Float price = null;
String s1 = recStr.substring(0, 7); // Extract the symbol part
String s2 = recStr.substring(8); // Take the remainder
price = new Float(s2.trim());
this.symbol = s1.trim();
this.price = price.floatValue();
}
// Methods to return the private values of this object
public float getPrice () {
return price;
}
public String getSymbol () {
return symbol;
}
public void setPrice (float newPrice) {
price = newPrice;
}
public void printPrice () {
System.out.print(price);
}
public void printStock () {
System.out.print(symbol);
}
public void print()
{
System.out.println("symbol [" + symbol + "]");
System.out.println("price [" + price + "]");
}
/**
Gets a title string for columns.
*/
public static String getTitle() {
// format is "12345678 9999.999"
return "Symbol Price ";
}
/**
Gets the StockRec in a string to match format of getTitle
Format is "12345678 9999.999"
*/
public String toString() {
String part1 = null; // Holds first half of string,
// including trailing spaces up to possible start of price
String part2 = null; // Holds second half of string,
// including any leading spaces to pad price to correct width
String s2 = null;
String s3 = null;
int i=0;
int j=0;
// We want symbol at the beginning
// Followed by Price starting at 11 position (index 10)
part1 = symbol;
part1.trim();
// Pad first half to correct length, Crude but it works!
for (i=part1.length(); i< 11; i++) {
part1 = part1 + " ";
}
s2 = (new Float(price)).toString();
s2.trim();
// Get index of decimal point in i
for (i=0;i<4;i++) {
if (s2.charAt(i) == '.')
break;
}
s3 = "";
// Pad with leading zeros
for (j=0;j 8) {
part2 = s3.substring(0, 8);
}
else { // pad with zeros
part2 = s3;
for (j=part2.length(); j<8; j++) {
part2 = part2 + "0";
}
}
return part1 + part2;
}
}

Last Updated