Hi, Bonjava,
I looked at your code. There are a few problems there.
1. Assignment3 class is OK.
2. Purchase class.
If you use getters and setters for proerties, the proterty name should match the getters and setters. If you getter is called "getQuantity" then the property name must be "quantity".
In the constructor with arguments you have:
public Purchase(int quantity, double price)
{
quantity = 0;
price = 0;
}
All it does is setting the passed arguments to zeros. This should
set the preprty of a specific instance of Purchase class to passed
values like:
this.quantity = quantity;
this.price = price;
If it confuses you, change the names of the parameters.
Data types.
I believe simple primitive types are sufficient here - double for quantity and int for price.
The changed Purchase class will look like this:
public class Purchase {
private double quantity;
private double price;
public Purchase(int quantity, double price)
{
this.quantity = quantity;
this.price = price;
}
public double getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public double getCost()
{
double cost = getQuantity() * getPrice();
return cost;
}
}
3. Stock class.
I believe what you want is to print the values for price, cost and
the value, returned from calculateGain() function (I am not sure
what you wanted to achieve though).
What you need to do is to iterate through created Purchase classes
and get the values form each one. This is the modified version of
Stock class:
import java.util.ArrayList;
public class Stock {
private String m_name = null;
private String m_symbol = null;
private ArrayList<Purchase> m_purchases;
public Stock(String name, String symbol)
{
m_name = name;
m_symbol = symbol;
m_purchases = new ArrayList<Purchase>();
}
public String getName() {
return m_name;
}
public String getSymbol() {
return m_symbol;
}
public void addPurchase(Purchase p) {
m_purchases.add(p);
}
public double calculateGain(double price) {
for (Purchase p : m_purchases)
{
price = price + price;
}
return price;
}
public void printReport(double price)
{
java.util.Iterator it = m_purchases.iterator();
System.out.println("");
System.out.printf("-----------------------------------------------------%n");
System.out.printf("Report for %s (%s) @$%s/share %n",m_name, m_symbol,price );
System.out.printf("-----------------------------------------------------%n");
System.out.printf("%-15s%-15s%-15s%-15s%n" ,"Qty","Price","Cost","Gain");
System.out.printf("-----------------------------------------------------%n");
while (it.hasNext()) {
Purchase pr = (Purchase)it.next();
System.out.printf("%-15s%-15s%-15s%-15s%n" ,"Qty",pr.getPrice(),pr.getCost(),calculateGain(price));
}
}
}
I hope this helps.