I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set stock name * @param name */ public void setName(String name) { this.name = name; } /** * * @return symbol */ public String getSymbol() { return symbol; } /** * set symbol of stock * @param symbol */ public void setSymbol(String symbol) { this.symbol = symbol; } /** * * @return stock's price */ public double getPrice() { return price; } /** * set price of stock with protection * against negative value * @param price */ public void setPrice(double price) { //If price is negative value if(price<0) this.price = 0; else this.price = price; } /** * * @return String representation of Stock object */ @Override public String toString() { return "Stock[" + "name='" + name + '\'' + ", symbol='" + symbol + '\'' + ", price=" + price + ']'; } }
import java.io.*; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Scanner; public class HW6 { public static void main(String[] args) { try { //Create an array list of stocks ArrayList<Stock> stocks = new ArrayList<>(); //Prompt user for input file Scanner input = new Scanner(System.in); System.out.print("Enter input file: "); String inputFileName = input.nextLine(); //open file to read File myFile = new File(inputFileName); Scanner inputFile = new Scanner(myFile); //read all data from file while (inputFile.hasNextLine()) { String[] tokens = inputFile.nextLine().trim().split(","); //Create stock object Stock stock = new Stock(tokens[0],tokens[1],Double.parseDouble(tokens[2])); //Add stock to stock list stocks.add(stock); } //Close the file stream inputFile.close(); //Prompt user for output file System.out.print("Enter output file name: "); String outputFileName = input.nextLine(); //Write data to file as well as console PrintWriter writer = new PrintWriter(new FileWriter(outputFileName)); int index1 = getHighestStockIndex(stocks); int index2 = getLowestStockIndex(stocks); DecimalFormat format = new DecimalFormat("#.00"); System.out.println("Sum of Stock Price: $"+format.format(getSum(stocks))); System.out.println("Average of Stock Price: $"+format.format(getAverage(stocks))); System.out.println("Highest Price Stock's name: "+stocks.get(index1).getName()+" and its price: $"+format.format(stocks.get(index1).getPrice())); System.out.println("Lowest Price Stock's name: "+stocks.get(index2).getName()+" and its price: $"+format.format(stocks.get(index2).getPrice())); writer.println("Sum of Stock Price: $"+format.format(getSum(stocks))); writer.println("Average of Stock Price: $"+format.format(getAverage(stocks))); writer.println("Highest Price Stock's name: "+stocks.get(index1).getName()+" and its price: $"+format.format(stocks.get(index1).getPrice())); writer.println("Lowest Price Stock's name: "+stocks.get(index2).getName()+" and its price: $"+format.format(stocks.get(index2).getPrice())); //close output stream writer.close(); } catch (IOException ex) { System.out.println(ex.getMessage()); } } /** * * @param stocks * @return sum of stocks price */ public static double getSum(ArrayList<Stock> stocks) { double sum =0; for (Stock s:stocks) { sum += s.getPrice(); } return sum; } /** * * @param stocks * @return average of stock price */ public static double getAverage(ArrayList<Stock>stocks) { return getSum(stocks)/stocks.size(); } /** * * @param stocks * @return highest stock index */ public static int getHighestStockIndex(ArrayList<Stock>stocks) { int index = 0; double price = stocks.get(0).getPrice(); for (int i = 0; i < stocks.size(); i++) { if(price<stocks.get(i).getPrice()) { price = stocks.get(i).getPrice(); index=i; } } return index; } /** * * @param stocks * @return lowest stock index */ public static int getLowestStockIndex(ArrayList<Stock>stocks) { int index = 0; double price = stocks.get(0).getPrice(); for (int i = 0; i < stocks.size(); i++) { if(price>stocks.get(i).getPrice()) { price = stocks.get(i).getPrice(); index=i; } } return index; } }
No, this code is perfectly working and is as simple as it could be..
The first class i.e Stock has three private variable name,symbol and price.
As all the members are private getter and setters are provided to access the elements and tostring method to print the attributes.
A three parameter constructor to initialize values to attributes during object creation.
In the second class, i.e HW6
An arraylist of stock is created.
An input file is used as data to run the program. In the input file , for every stock the data is sperated by ',' . i.e name,symbol,price.
The file is read line by line and is spllited in parts using "," as token.
All the datas are loaded in the arraylist and average, sum, highest and lowest stock is written to an output file and also to the program console.
For calculating sum,average, highest and lowest static functions are provided for each.
Get Answers For Free
Most questions answered within 1 hours.