Hi, I need some help with some method in my classes. I started on some if not all of them. I just cant seem to find what im doing wrong here. Any help would be greatly excepted. Thanks

Here is code:

[B][LEFT]import[/LEFT]
[/B]

[LEFT]java.util.Scanner;[/LEFT]

 
 
 

[B][LEFT]public[/LEFT]
[/B]

[LEFT][B]class[/B] Assignment3[/LEFT]

 
[LEFT]{
[B]public[/B] [B]static[/B] [B]void[/B] main(String args[])
{
Scanner scan = [B]new[/B] Scanner(System.[I]in[/I]);[/LEFT]
 
[LEFT]System.[I]out[/I].println("Enter the name of the stock:");
String name = scan.nextLine();[/LEFT]
 
[LEFT]System.[I]out[/I].println("Enter the symbol of the stock:");
String symbol = scan.next();[/LEFT]
 
[LEFT]Stock stock = [B]new[/B] Stock(name, symbol);[/LEFT]
 
[LEFT]System.[I]out[/I].println("Enter the quantity of stock purchased (-1 to quit):"); 
[B]int[/B] quantity = scan.nextInt();[/LEFT]
 
[LEFT][B]while[/B] (quantity > 0)
{
System.[I]out[/I].println("Enter the price of the stock purchased:"); 
[B]double[/B] price = scan.nextDouble();[/LEFT]
 
[LEFT]Purchase purchase = [B]new[/B] Purchase(quantity, price);
stock.addPurchase(purchase);[/LEFT]
 
[LEFT]System.[I]out[/I].println("Enter the quantity of stock purchased (-1 to quit):"); 
quantity = scan.nextInt();
}[/LEFT]
 
[LEFT]System.[I]out[/I].println("Enter the current stock price:"); 
[B]double[/B] currentPrice = scan.nextDouble();
stock.printReport(currentPrice);
}
}
==========================================[/LEFT]
 

[B][LEFT]import[/LEFT]
[/B]

[LEFT]java.math.BigDecimal;[/LEFT]

 
 
 
 

[B][LEFT]public[/LEFT]
[/B]

[LEFT][B]class[/B] Purchase [/LEFT]

 
[LEFT]{
[B]private[/B] BigDecimal m_quantity;
[B]private[/B] BigDecimal m_price;[/LEFT]
 
[LEFT][B]public[/B] Purchase([B]int[/B] quantity, [B]double[/B] price)
{
quantity = 0;
price = 0;
}[/LEFT]
 
[LEFT][B]public[/B] [B]int[/B] getQuantity()
{
[B]int[/B] quantity = m_quantity.intValue();
[B]return[/B] quantity;[/LEFT]
 
[LEFT]}[/LEFT]
 
[LEFT][B]public[/B] [B]double[/B] getPrice()
{
[B]double[/B] price = m_price.doubleValue();
[B]return[/B] price;
}[/LEFT]
 
[LEFT][B]public[/B] [B]double[/B] getCost()
{
BigDecimal c = m_quantity.multiply(m_price);
[B]double[/B] cost = c.doubleValue();
[B]return[/B] cost;
}[/LEFT]
 
 

[LEFT]/* public double getCostAtPrice()[/LEFT]

 
[LEFT]{ 
BigDecimal c2 = m_quantity.multiply(******);
double cost2 = c2.doubleValue();[/LEFT]
 
[LEFT]return cost2;
}[/LEFT]
 
[LEFT]public double getGainAtPrice(double price)
{ 
BigDecimal c = m_quantity.multiply(m_price);
BigDecimal c2 = m_quantity.multiply(******);
double gain = c.doubleValue()- c2.doubleValue();
return gain;
} 
*/[/LEFT]
}
=========================================
 

[B][LEFT]import[/LEFT]
[/B]

[LEFT]java.util.ArrayList;[/LEFT]

 
 
 

[B][LEFT]public[/LEFT]
[/B]

[LEFT][B]class[/B] Stock[/LEFT]

 
[LEFT]{
[B]private[/B] String m_name = [B]null[/B];
[B]private[/B] String m_symbol = [B]null[/B];
[B]private[/B] ArrayList<Purchase> m_purchases; [/LEFT]
 
[LEFT][B]public[/B] Stock(String name, String symbol)
{
m_name = name;
m_symbol = symbol;
m_purchases = [B]new[/B] ArrayList<Purchase>();
}
[B]public[/B] String getName()
{
[B]return[/B] m_name;
}
[B]public[/B] String getSymbol()
{
[B]return[/B] m_symbol;
}[/LEFT]
 
[LEFT][B]public[/B] [B]void[/B] addPurchase(Purchase p)
{
m_purchases.add(p);
}
[B]public[/B] [B]double[/B] calculateGain([B]double[/B] price)
{ 
[B]for[/B] (Purchase p : m_purchases)
{ 
price = price + price;[/LEFT]
 
[LEFT]}
[B]return[/B] price;
}
[B]public[/B] [B]void[/B] printReport([B]double[/B] price)
{[/LEFT]
 
[LEFT]System.[I]out[/I].println("");
System.[I]out[/I].printf("-----------------------------------------------------%n");
System.[I]out[/I].printf("Report for %s (%s) @$%s/share %n",m_name, m_symbol,price );
System.[I]out[/I].printf("-----------------------------------------------------%n");
System.[I]out[/I].printf("%-15s%-15s%-15s%-15s%n" ,"Qty","Price","Cost","Gain");[/LEFT]
 
[LEFT]System.[I]out[/I].printf("-----------------------------------------------------%n");
System.[I]out[/I].printf("%-45s%-15s%n" ,"Total Gain:","$",calculateGain(price));
} [/LEFT]
}

Im not worry about the output. I know how to do that part but the methods is what i need help on....again thanks

Do what part? What method is wrong? What's it suppose to do and what is it doing instead? Do just say "here's my code, fix it".

The methods I'm talking about are getCostAtPrice, getGainAtPrice and calculateGain. These are the methods that are giving me trouble.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.