Reputation: 43
For my class we have to put stock information into the elements of an array and display the percentage of change for each stock with its symbol. I have it working to where it will print the first object five times verses the five objects once like its suppose to be.
Here's my code (The instructions are in the comments):
import java.util.Scanner;
public class StockTest{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
/*
Make sureto complete the Stock class before you do the following items.
*/
/* Step 1:
* Instantiate two Stock objects with arguments of your choice(stock symbol and name).
* Using two set methods set the previousClosingPrice and curentPrice of two Stocks.
* Display the percentage changed from previousClosingPrice to curentPrice of both stocks.
*/
System.out.println("Input stock symbol");
String symbol = input.next();
System.out.println("Input stock name");
String name = input.next();
Stock stock = new Stock(symbol, name);
System.out.println("Input previous price");
Stock.setPreviousClosingPrice(input.nextDouble());
System.out.println("Input current price");
Stock.setCurrentPrice(input.nextDouble());
System.out.println("the change percentage of the stock is " + Stock.getChangePercent() + "%");
/*
* Step 2: Declare an array of 5 Stock objects. (Next three steps should be done in a loop)
* Ask the user to input stock symbol, name, previousClosingPrice and curentPrice.
* Initialize an array element with new Stock object using symbol and name that user input.
* Using two set methods set the previousClosingPrice and curentPrice of each Stock element
*/
Stock[] stockArray = new Stock[5];
for(int i = 0; i < stockArray.length; i++){
System.out.println("Please input stock symbol, name, previous price, and current price");
String stockSymbol = input.next();
String stockName = input.next();
stockArray[i] = new Stock(symbol, name);
Stock.setPreviousClosingPrice(input.nextDouble());
Stock.setCurrentPrice(input.nextDouble());
}
/*
* Step 4: (this step should be done in a loop)
* Display the percentage changed from previousClosingPrice to curentPrice of all stocks with their symbol.
*/
for (int i = 0; i < stockArray.length; i++){
System.out.println("Percentage changed of " + symbol + " " + Stock.getChangePercent());
}
} }
Upvotes: 0
Views: 161
Reputation: 2155
I could see few problems with your code :
Creating and storing Stock object in array directly as done in below line will have overhead as you also need to update remaining properties for the same object.
stockArray[i] = new Stock(symbol, name);
Using static methods to update object properties doesn't make sense.
Stock.setPreviousClosingPrice(input.nextDouble());
Stock.setCurrentPrice(input.nextDouble());
Again Scanner methods need to be used carefully.
I tried correcting code as per comments:
StockTest class:
import java.util.Scanner;
public class StockTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*
* Make sureto complete the Stock class before you do the following
* items.
*/
/*
* Step 1: Instantiate two Stock objects with arguments of your
* choice(stock symbol and name). Using two set methods set the
* previousClosingPrice and curentPrice of two Stocks. Display the
* percentage changed from previousClosingPrice to curentPrice of both
* stocks.
*/
System.out.println("Input stock symbol");
String symbol = input.nextLine();
System.out.println("Input stock name");
String name = input.nextLine();
Stock stock1 = new Stock(symbol, name);
System.out.println("Input previous price");
stock1.setPreviousClosingPrice(Double.parseDouble(input.nextLine()));
System.out.println("Input current price");
stock1.setCurrentPrice(Double.parseDouble(input.nextLine()));
System.out.println("Input stock symbol");
symbol = input.nextLine();
System.out.println("Input stock name");
name = input.nextLine();
Stock stock2 = new Stock(symbol, name);
System.out.println("Input previous price");
stock2.setPreviousClosingPrice(Double.parseDouble(input.nextLine()));
System.out.println("Input current price");
stock2.setCurrentPrice(Double.parseDouble(input.nextLine()));
System.out.println("the change percentage of the stock is "
+ stock1.getChangePercent() + "%");
System.out.println("the change percentage of the stock is "
+ stock2.getChangePercent() + "%");
/*
* Step 2: Declare an array of 5 Stock objects. (Next three steps should
* be done in a loop) Ask the user to input stock symbol, name,
* previousClosingPrice and curentPrice. Initialize an array element
* with new Stock object using symbol and name that user input. Using
* two set methods set the previousClosingPrice and curentPrice of each
* Stock element
*/
Stock[] stockArray = new Stock[2];
for (int i = 0; i < stockArray.length; i++) {
System.out
.println("Please input stock symbol, name, previous price, and current price");
symbol = input.nextLine();
name = input.nextLine();
Stock stock = new Stock(symbol, name);
stock.setPreviousClosingPrice(Double.parseDouble(input.nextLine()));
stock.setCurrentPrice(Double.parseDouble(input.nextLine()));
stockArray[i] = stock;
}
/*
* Step 4: (this step should be done in a loop) Display the percentage
* changed from previousClosingPrice to curentPrice of all stocks with
* their symbol.
*/
for (Stock stock : stockArray) {
System.out.println("Percentage changed of " + stock.getSymbol()
+ " " + stock.getChangePercent() + "%");
}
}
}
Stock class
public class Stock {
private String name;
private String symbol;
private double previousClosingPrice;
private double currentPrice;
public Stock(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
public String getName() {
return this.name;
}
public String getSymbol() {
return this.symbol;
}
public void setCurrentPrice(double currentPrice) {
this.currentPrice = currentPrice;
}
public void setPreviousClosingPrice(double previousClosingPrice) {
this.previousClosingPrice = previousClosingPrice;
}
public double getChangePercent() {
return (this.currentPrice / this.previousClosingPrice - 1) * 100;
}
}
Upvotes: 0
Reputation: 393771
You are using the wrong variables for initializing the Stock instances.
Change
String stockSymbol = input.next();
String stockName = input.next();
stockArray[i] = new Stock(symbol,name);
to
String stockSymbol = input.next();
String stockName = input.next();
stockArray[i] = new Stock(stockSymbol,stockName);
It's not clear what's the purpose of the first Stock
instance you create (Stock stock = new Stock(symbol, name);
), since you don't do anything with it after initializing it.
Upvotes: 2