With this code segment, I must be able to output text file data with names of persons along with int values after performing calculations. The program isolates the names, int values and float values and should output the percentage of each value as a functions of total... example out is...
Total Sales 100,000
Total Expense 25,000.0
Smith John 6.5% 13.2%
Brown Kevin 5.8% 11.3%...and so on
Current the output prints one line of data only. In other words, only the last values on the list is printed onscreen. Please help.
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class SalesReport {
String name; // Used for reading salesperson's name
int sales; // Used for reading sales values
float expenses; // Used for reading expenses values
int salesTotal;
float expenseTotal;
// Class constants
static String A = "\n|--------------------------------------------------------------|";
static String B = "\n| SALES REPORT |";
static String C = "\n| SALESPERSON | Total Sales | Total Expenses |";
static String D = "| |";
// Direct constructor
public SalesReport(String newName, int newSales, float newExpenses,
int newSalesTotal, float newExpenseTotal) {
name = newName;
sales = newSales;
expenses = newExpenses;
salesTotal = newSalesTotal;
expenseTotal = newExpenseTotal;
}
// Default constructor
public SalesReport() {
name = " ";
sales = 0;
expenses = 0;
salesTotal = 0;
expenseTotal = 0;
}
// Scanner based constructor
public SalesReport(Scanner inFile) {
String word = " ";
List<String> reportNames = new ArrayList<String>();
// Gather names
while (inFile.hasNext()) { // Loops for text only values
word = inFile.nextLine(); // Collects all name values
reportNames.add(word); // places file names into array
String nameArray[] = new String[reportNames.size()];
reportNames.toArray(nameArray); // assign names to array positions
for (int i = 0; i < nameArray.length; i++)
name = nameArray[i];
// Gather sales and expenses
int intgr = 0;
float expn = 0;
List<Integer> reportSales = new ArrayList<Integer>();
while (inFile.hasNextInt()) { // Loops for integer values
intgr = inFile.nextInt(); // Collects all sales integer values
reportSales.add(intgr);
Integer intArray[] = new Integer[reportSales.size()];
reportSales.toArray(intArray);
for (int j = 0; j < intArray.length; j++)
sales = intArray[j];
salesTotal += sales;
List<Float> reportExpenses = new ArrayList<Float>();
expn = inFile.nextFloat(); // Collects all expenses float values
reportExpenses.add(expn);
Float floatArray[] = new Float[reportExpenses.size()];
reportExpenses.toArray(floatArray);
for (int k = 0; k < floatArray.length; k++)
expenses = floatArray[k];
expenseTotal += expenses;
}
System.out.println(name+" ");
}
System.out.println(salesTotal+" "+expenseTotal+'\n');
}
// Observer methods
public String getName() {
return name; }
public int getSales() {
return sales; }
public float getExpenses() {
return expenses; }
public int getSalesTotal() {
return salesTotal; }
public float getExpenseTotal() {
return expenseTotal; }
// Isolate salespersons
public void SalesPersons(String names) {
names = getName();
System.out.println("These are sales persons: \n"+names);
}
// Sum all sales amounts
public void SalesTotals(int sumSales) {
sumSales = getSalesTotal();
System.out.println("This is sales total: \n"+sumSales);
}
// Sum all expense amounts
public void ExpenseTotals(float sumExpenses) {
sumExpenses = getExpenseTotal();
System.out.println("This is expenses total: \n"+sumExpenses);
}
// Calculate sales percentages
public void SalesPercent(float salesPer) {
salesPer = getSales()*100/(float)getSalesTotal();
System.out.print(" "+salesPer+"% ");
}
// Calculate expense percentages
public void ExpensePercent(float expensesPer) {
expensesPer = getExpenses()*100/getExpenseTotal();
System.out.print(" "+expensesPer+"% |"+A);
}
// Print method
public void print() {
//SalesPersons(" ");
//SalesTotals(0);
//ExpenseTotals(0);
//SalesPercent(0);
//ExpensePercent(0);
System.out.println(A+B+A+C);
System.out.println(D+" "+getSalesTotal()+" "+getExpenseTotal()+A);
System.out.print("| "+getName()+" ");
SalesPercent(0);
ExpensePercent(0);
}
// Begin execution here - the driver
public static void main(String[] args) throws IOException {
// Sets scanner to read file
Scanner inFile = new Scanner(new FileReader("sales.txt"));
SalesReport out = new SalesReport(inFile);
out.print();
}
}