misa is muy, muy Java impaired (my jar jar impersonation)
Hello everybody, I'm relatively a newbie when it comes to programming
with Java. I was wondering if somebody wouldn't mind looking over the
code I have written so far for my Java class and see if it looks
ok. It is supposed to do the following:
• The user will be prompted for the current price for the stock being processed.
• The program will then read a series of investments in from a file, “investment.txt”:
◦ The investments will all be for the same stock (all IBM or Microsoft …)
◦ The investments will have different purchase dates and purchase prices.
◦ The investments will be ordered
such that the oldest stock (one purchased first) is on the first line
in the file.
◦ The file will have the following format (ticker, purchase price, date, num shares):
IBM,6.50,5/3/1987,100
IBM,7.50,12/1/1987,100
IBM,9.00,3/14/1990,100
IBM,12.00,4/2/1996,100
IBM,23.45,8/26/1999,100
IBM,18.00,1/5/2001,100
• The data from the file and the
current price entered by the user will be used to create new Investment
objects.
• The investments will be stored
in a data structure that obeys the FIFO protocol (a Queue)
I believe I have everything taken care but would greatly appreciate any suggestions to improve the code.
import java.io.*;
import java.util.GregorianCalendar;
import javax.swing.*;
public class InvestmentFromFile
{
public static void main(String argv[])
{
int i=0, day, month, year, nShare;
float pPrice;
String ticker, temp="";
Investment[] investment= new Investment[100];
float cPrice =
Float.parseFloat(JOptionPane.showInputDi alog("Input the Current Price
for the Stock being read from the file"));
try
{
BufferedReader in = new BufferedReader(new FileReader("investment.txt"));
while((temp = in.readLine()) != null) //last line will be null, which will exit the loop
{
String[] investPieces=
temp.split(","); //each investPieces element holds the 4 pieces
ticker= investPieces[0];
pPrice= Float.parseFloat(investPieces[1]);
nShare= Integer.parseInt(investPieces[3]);
//splitting the date part
String[] datePieces= investPieces[2].split("/");
month= Integer.parseInt(datePieces[0]);
day= Integer.parseInt(datePieces[1]);
year= Integer.parseInt(datePieces[2]);
investment[i]= new
Investment(ticker, cPrice, year, month, day, pPrice, nShare);
System.out.println(investment[i]);
System.out.println(" Capital Gain: " +
investment[i].calculateGain());
System.out.println(" Tax on Gain: " +
investment[i].calculateTax());
System.out.println(" Net Gain: " +
investment[i].calculateNetGain());
i++;
}
}
catch(FileNotFoundException e) {System.exit(0);}
catch(IOException e) {System.exit(0);}
}
}
/** class to store a Stock investment */
class Investment
{
private Stock stockOwned;
private GregorianCalendar purchaseDate;
private float purchasePrice;
private int numOfShares;
private static final float SHORTTERM = 0.28F;
private static final float LONGTERM = 0.20F;
/** Default Investment constructor **/
public Investment()
{
stockOwned = new Stock();
stockOwned.setStock("",0);
purchaseDate=new GregorianCalendar();
purchasePrice=0;
numOfShares=0;
}
/** Investment constructor takes a Stock,
year, month, date, purchase price & number of shares**/
public Investment(Stock s, int year, int month, int day, float pPrice, int num)
{ makeInvestment(s, year,month, day, pPrice, num); }
/** Investment constructor takes a ticker symbol, current price
year, month, date, purchase price & number of shares**/
public Investment(String ticker, float cPrice, int year, int month, int day, float pPrice,int num)
{
stockOwned = new Stock();
stockOwned.setStock(ticker,cPrice);
makeInvestment(stockOwned, year, month, day, pPrice, num);
}
/** Investment method to set all attributes takes a Stock,
year, month, date, purchase price & number of shares**/
public void makeInvestment(Stock s, int year, int month, int day, float pPrice, int num)
{
stockOwned = s;
purchaseDate=new GregorianCalendar(year, (month-1), day);
purchasePrice = pPrice;
numOfShares=num;
}
/** @return the ticker symbol associated with the stockOwned **/
public String getTicker()
{ return stockOwned.getStockTicker(); }
/** @return the purchasDate as a String **/
public String getDateString()
{
int year = purchaseDate.get(GregorianCalendar.YEAR) ;
int mon = purchaseDate.get(GregorianCalendar.MONTH) + 1;
int day = purchaseDate.get(GregorianCalendar.DATE) ;
return mon + "/" + day +"/" + year;
}
/** @return the purchaseDate as a GregorianCalendar **/
public GregorianCalendar getPurchaseDate()
{ return purchaseDate; }
/** @param float: the new price for the stockOwned **/
public void setCurrentPrice(float Price)
{ stockOwned.setStockPrice(Price); }
/** @return the number of shares for the Investment **/
public int getShares()
{ return numOfShares; }
/** @return the gross capital gain for the investment assuming sold today **/
public float calculateGain()
{
return numOfShares * (stockOwned.getStockPrice() - purchasePrice);
}
/** @return the tax on the investment gain assuming sold today **/
public float calculateTax()
{
float tax = 0;
GregorianCalendar lastYear = new GregorianCalendar();
lastYear.add(GregorianCalendar.YEAR, -1);
if(purchaseDate.before(lastYear))
tax = LONGTERM * calculateGain();
else
tax = SHORTTERM * calculateGain();
return tax;
}
/** @return the net capital gain for the investment assuming sold today **/
public float calculateNetGain()
{
return calculateGain() - calculateTax();
}
/** @return the Investment String **/
public String toString()
{
return
stockOwned.getStockTicker() + "," + purchasePrice +","+
getDateString()+","+numOfShares;
}
}
/** A simple Stock class **/
class Stock
{
private String stockTicker;
private float stockPrice;
/** sets the stock ticker symbol and current price */
public void setStock(String ts, float price)
{
stockTicker = ts;
stockPrice = price;
}
/** sets the current stock price */
public void setStockPrice(float price)
{ stockPrice = price; }
/** @return the current stock price */
public float getStockPrice()
{ return stockPrice; }
/** @return the stock ticker symbol */
public String getStockTicker()
{ return stockTicker; }
}
with Java. I was wondering if somebody wouldn't mind looking over the
code I have written so far for my Java class and see if it looks
ok. It is supposed to do the following:
• The user will be prompted for the current price for the stock being processed.
• The program will then read a series of investments in from a file, “investment.txt”:
◦ The investments will all be for the same stock (all IBM or Microsoft …)
◦ The investments will have different purchase dates and purchase prices.
◦ The investments will be ordered
such that the oldest stock (one purchased first) is on the first line
in the file.
◦ The file will have the following format (ticker, purchase price, date, num shares):
IBM,6.50,5/3/1987,100
IBM,7.50,12/1/1987,100
IBM,9.00,3/14/1990,100
IBM,12.00,4/2/1996,100
IBM,23.45,8/26/1999,100
IBM,18.00,1/5/2001,100
• The data from the file and the
current price entered by the user will be used to create new Investment
objects.
• The investments will be stored
in a data structure that obeys the FIFO protocol (a Queue)
I believe I have everything taken care but would greatly appreciate any suggestions to improve the code.
import java.io.*;
import java.util.GregorianCalendar;
import javax.swing.*;
public class InvestmentFromFile
{
public static void main(String argv[])
{
int i=0, day, month, year, nShare;
float pPrice;
String ticker, temp="";
Investment[] investment= new Investment[100];
float cPrice =
Float.parseFloat(JOptionPane.showInputDi
for the Stock being read from the file"));
try
{
BufferedReader in = new BufferedReader(new FileReader("investment.txt"));
while((temp = in.readLine()) != null) //last line will be null, which will exit the loop
{
String[] investPieces=
temp.split(","); //each investPieces element holds the 4 pieces
ticker= investPieces[0];
pPrice= Float.parseFloat(investPieces[1]);
nShare= Integer.parseInt(investPieces[3]);
//splitting the date part
String[] datePieces= investPieces[2].split("/");
month= Integer.parseInt(datePieces[0]);
day= Integer.parseInt(datePieces[1]);
year= Integer.parseInt(datePieces[2]);
investment[i]= new
Investment(ticker, cPrice, year, month, day, pPrice, nShare);
System.out.println(investment[i]);
System.out.println(" Capital Gain: " +
investment[i].calculateGain());
System.out.println(" Tax on Gain: " +
investment[i].calculateTax());
System.out.println(" Net Gain: " +
investment[i].calculateNetGain());
i++;
}
}
catch(FileNotFoundException e) {System.exit(0);}
catch(IOException e) {System.exit(0);}
}
}
/** class to store a Stock investment */
class Investment
{
private Stock stockOwned;
private GregorianCalendar purchaseDate;
private float purchasePrice;
private int numOfShares;
private static final float SHORTTERM = 0.28F;
private static final float LONGTERM = 0.20F;
/** Default Investment constructor **/
public Investment()
{
stockOwned = new Stock();
stockOwned.setStock("",0);
purchaseDate=new GregorianCalendar();
purchasePrice=0;
numOfShares=0;
}
/** Investment constructor takes a Stock,
year, month, date, purchase price & number of shares**/
public Investment(Stock s, int year, int month, int day, float pPrice, int num)
{ makeInvestment(s, year,month, day, pPrice, num); }
/** Investment constructor takes a ticker symbol, current price
year, month, date, purchase price & number of shares**/
public Investment(String ticker, float cPrice, int year, int month, int day, float pPrice,int num)
{
stockOwned = new Stock();
stockOwned.setStock(ticker,cPrice);
makeInvestment(stockOwned, year, month, day, pPrice, num);
}
/** Investment method to set all attributes takes a Stock,
year, month, date, purchase price & number of shares**/
public void makeInvestment(Stock s, int year, int month, int day, float pPrice, int num)
{
stockOwned = s;
purchaseDate=new GregorianCalendar(year, (month-1), day);
purchasePrice = pPrice;
numOfShares=num;
}
/** @return the ticker symbol associated with the stockOwned **/
public String getTicker()
{ return stockOwned.getStockTicker(); }
/** @return the purchasDate as a String **/
public String getDateString()
{
int year = purchaseDate.get(GregorianCalendar.YEAR)
int mon = purchaseDate.get(GregorianCalendar.MONTH)
int day = purchaseDate.get(GregorianCalendar.DATE)
return mon + "/" + day +"/" + year;
}
/** @return the purchaseDate as a GregorianCalendar **/
public GregorianCalendar getPurchaseDate()
{ return purchaseDate; }
/** @param float: the new price for the stockOwned **/
public void setCurrentPrice(float Price)
{ stockOwned.setStockPrice(Price); }
/** @return the number of shares for the Investment **/
public int getShares()
{ return numOfShares; }
/** @return the gross capital gain for the investment assuming sold today **/
public float calculateGain()
{
return numOfShares * (stockOwned.getStockPrice() - purchasePrice);
}
/** @return the tax on the investment gain assuming sold today **/
public float calculateTax()
{
float tax = 0;
GregorianCalendar lastYear = new GregorianCalendar();
lastYear.add(GregorianCalendar.YEAR, -1);
if(purchaseDate.before(lastYear))
tax = LONGTERM * calculateGain();
else
tax = SHORTTERM * calculateGain();
return tax;
}
/** @return the net capital gain for the investment assuming sold today **/
public float calculateNetGain()
{
return calculateGain() - calculateTax();
}
/** @return the Investment String **/
public String toString()
{
return
stockOwned.getStockTicker() + "," + purchasePrice +","+
getDateString()+","+numOfShares;
}
}
/** A simple Stock class **/
class Stock
{
private String stockTicker;
private float stockPrice;
/** sets the stock ticker symbol and current price */
public void setStock(String ts, float price)
{
stockTicker = ts;
stockPrice = price;
}
/** sets the current stock price */
public void setStockPrice(float price)
{ stockPrice = price; }
/** @return the current stock price */
public float getStockPrice()
{ return stockPrice; }
/** @return the stock ticker symbol */
public String getStockTicker()
{ return stockTicker; }
}
