Hola joseph
Cuando necesites hacer una consulta, pon un título descriptivo. Ayuda la necesitamos todos, de momento le hemos puesto un título genérico.
Te envío la clase con modificaciones para poder leer el archivo. He eliminado el código que no se utilizaba.
import java.text.DecimalFormat; // needed for DecimalFormat class
import java.io.*; // needed for file I/O classes
public class Program4
{
public static void main(String[] args) throws IOException
{
// print my name to the screen
System.out.println("Jose A. Pimentel");
System.out.println();
String fileName; // to hold the file name
String lineComp; // to hold the name of company
double compNum; // to hold the number of shares
double purchPrice; // to hold the purchase price per shares
double commPurch; // to hold the purchase commission rate
double salesPrice; // to hold the sales price per share
double commSales; // to hold the sales commission
fileName = "C:\\Pruebas\\StockData.txt";
FileReader inputFile = new FileReader(fileName);
BufferedReader buferArchivo = new BufferedReader(inputFile);
String linea = buferArchivo.readLine(); // lee la proxima linea
while(linea != null) // minetras haya lineas en el archivo
{
if (linea.isEmpty()) { // salta las lineas vacias
linea = buferArchivo.readLine();
continue;
}
// assign every document data to a different variable
String separador[] = linea.split(":");
lineComp = separador[0];
compNum = Double.parseDouble(separador[1]);
purchPrice = Double.parseDouble(separador[2]);
commPurch = Double.parseDouble(separador[3]);
salesPrice = Double.parseDouble(separador[4]);
commSales = Double.parseDouble(separador[5]);
checkValidity(lineComp, compNum, purchPrice, commPurch, salesPrice, commSales);
linea = buferArchivo.readLine();
}
buferArchivo.close();
}
public static void checkValidity(String company, double numOfShares, double purchPrice,
double commissionP, double salesPrice, double commissionS)
{
DecimalFormat twoDecimals = new DecimalFormat("0.000");
calcProfit(numOfShares, purchPrice, commissionP, salesPrice, commissionS);
if (numOfShares <= 0)
{
System.out.print(company);
System.out.println(" Number of shares invalid: " + twoDecimals.format(numOfShares));
}
if (purchPrice < 0)
{
System.out.print(company);
System.out.println(" Purchase price invalid: " + twoDecimals.format(purchPrice));
}
if (salesPrice < 0)
{
System.out.print(company);
System.out.println(" Sales price invalid: " + twoDecimals.format(salesPrice));
}
if(commissionP < 0 || commissionP > 0.20)
{
if(commissionS < 0 || commissionS > 0.20)
{
System.out.print(company);
System.out.println(" Purchase commission percent invalid: " + twoDecimals.format(commissionP));
System.out.print(company);
System.out.println(" Sales commission percent invalid: " + twoDecimals.format(commissionS));
}
}
else
{
}
}
public static double calcProfit(double numShares, double priceP, double commP, double priceS, double commS)
{
double commAmountP = 0;
double commAmountS = 0;
double totalP = 0;
double totalS = 0;
double totalAmount = 0;
commAmountP = numShares * priceP * commP;
commAmountS = numShares * priceS * commS;
totalS = numShares * priceS - commAmountS;
totalP = numShares * priceP + commAmountP;
totalAmount = totalS - totalP;
return totalAmount;
}
}
Envío txt formateado para leer con el programa. Hay que colocar el archivo en la ruta designada.
Mi compañia:1200:1300:1400:150:-99
Tu compañia:1000:800:600:-500:33
Su compañia:1:2:3:4:5
A mi entender parece que sabes lo que quieres conseguir, pero no sabes como perdírselo a Java.
Siendo así, deberías hacer las consultas necesarias en la documentación de Java o seguir uno de nuestros cursos para poder construir tus programas. Tal como te recomienda el compañero Jorge.
Un apunte: Si colocas todo el código en el main, cuando tu aplicación crezca, será difícil de controlar.
Saludos