简体   繁体   中英

My Array is only printing out last element from file, I want it to print out all

I have linked a gist to the stockData txt file and what my program displays when being run. Also to not I am not worried about totalChangepercent returning 0.0% as I haven't finished but I didn't go any further without solving this.

Yes this is a homework Assignment but I did two similar assignments like this with no issue so I am not sure what else to do.

Programming Running:

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

stockData.txt:

GPRO  
GoPro, Inc.
89.93
89.8773
SBUX
Starbucks
75.26
75.76
JCP
JC Penney
8.18
7.72
AMZN
Amazon
323.71
319.94
AE
Adams Resources and Energy
44.71
44.69
CEP
Constellation Energy Partners
3.38
3.35
KO
Coca-Cola
43.66
44.44
MCD
McDonald's
92.81
93.53
TSLA
Tesla Motors
259.28
264.57
AAPL
Apple Inc
100.80
102.30

public class Stock
{
private static String symbol;
private static String stockName;
private static double previousClosingPrice, currentPrice;
private static double totalChangePercent;
public Stock ()
{
    symbol = " ";
    stockName = " ";
    previousClosingPrice = 0.0;
    currentPrice = 0.0;
} //end default

public Stock(String symbol, String stockName, double previousClosingPrice, double currentPrice)
{
    this.symbol = symbol;
    this.stockName = stockName;
    this.previousClosingPrice = previousClosingPrice;
    this.currentPrice = currentPrice;
} //end overloaded

//setters

public void setSymbol(String symbol)
{
    symbol = symbol;
}  //end setSymbol();

public void setStock(String stockName)
{
    stockName = stockName;
}  //end setStock();

public void setPreviousclosingPrice(double previousClosingPrice)
{
    previousClosingPrice = previousClosingPrice;
}  //end setPreviousclosingPrice();

public void setCurrentPrice(double currentPrice)
{
    currentPrice = currentPrice;
}  //end setCurrentPrice();

//getters

public String getSymbol()
{
    return symbol;
} //end getSymbol();

public String getStockName()
{
    return stockName;
} //getStock();

public double getPreviousClosingPrice()
{
    return previousClosingPrice;
} //end getPreviousClosingPrice()

public double getCurrentPrice()
{
    return currentPrice;
} //end getCurrentPrice()

public static double getChangePercent()
{
    totalChangePercent = ((currentPrice - previousClosingPrice) / previousClosingPrice);

    return totalChangePercent;
} //end getChangePercent();

public boolean equals(Stock bool)
{
    if(symbol.equals(bool.getSymbol()) && stockName.equals(bool.getStockName()) && previousClosingPrice == bool.getPreviousClosingPrice() && currentPrice == bool.getCurrentPrice())
      {
        return true;
      } else
      {
        return false;
      }
}

public String toString()
{
    String str = " ";
    str += "\nSymbol: " + symbol + "\nStock: " + stockName;
    str += "\nPrevious Closing Price: $" + previousClosingPrice + "\nCurrent Price: $" + currentPrice;
    str += "\nTotal Change Percent: " + totalChangePercent + "%";

    return str;
}
}




import java.util.*;
import java.io.*;
import java.text.*;

public class StockDriver
{
public static void main(String args[]) throws IOException
{
    Stock [] stockData = new Stock [10];

    setStockData(stockData);
    displayStockData(stockData);
    displayImprovedStocks(stockData);
} //end main

public static void setStockData(Stock [] stockData) throws IOException
{

    File fileIn = new File("stockdata.txt");
    Scanner scan = new Scanner(fileIn);

    if(!fileIn.exists())
    {
        System.out.println("No file found!");
        System.exit(0);
    } //end if -- ends check

    String symbol, stockName;
    double previousClosingPrice, currentPrice;
    int i = 0;

    while(scan.hasNext()&& i < stockData.length)
    {
        symbol = scan.nextLine();
        stockName = scan.nextLine();
        String postPriceStr = scan.nextLine();
        String currentPriceStr = scan.nextLine();
        previousClosingPrice = Double.parseDouble(postPriceStr);
        currentPrice = Double.parseDouble(currentPriceStr);
        stockData[i] = new Stock(symbol, stockName, previousClosingPrice, currentPrice);
        i++;
    } //end while
} //end setStockData()

public static void displayImprovedStocks(Stock [] stockData)
{
    DecimalFormat decFor = new DecimalFormat("#0.00");
    double totalImproved = 0.0;
    double postImproved = 0.0;
    double doMath = 0.0;

    for (int i = 0; i < stockData.length; i++)
    {
        postImproved += stockData[i].getPreviousClosingPrice();
        if (postImproved >= stockData[i].getCurrentPrice())
        {
            doMath = postImproved;
        } else
        {
            System.out.println(" ");
        } //end

        totalImproved = doMath;
        System.out.println("\nImproved Stocks: " + decFor.format(totalImproved));
    }


}
public static void displayStockData(Stock [] stockData)
{
    for(int i = 0; i <= stockData.length; i++)
    {
        System.out.println(stockData[i]);
    }
}
}

All your class variables are static - meaning they are shared across all the instances of the class. Instead try having them like so:

public class Stock
{
private String symbol;
private String stockName;
private double previousClosingPrice, currentPrice;
private double totalChangePercent;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM