I would like to sort a list (from a text file "sales.txt")that has text, int and float values. The problem I have is, my output only produces the last set of values. I would like to produce a table with as the final output. Please help me.
public class Sales {
static String name; // Used for reading salesperson's name
static int sales; // Used for reading sales
static int sumSales; // Total sales
static float expenses; // Used for reading expenses
static float sumExpenses; // Total expenses
static float salesPer; // Sales as a percentage of total sales
static float expensesPer; // Expenses as a percentage of total expenses
// Variables used to create output table
static String A = "\n|--------------------------------------------------------------|";
static String B = "\n| SALES REPORT |";
static String C = "\n| NAME | Total Sales | Total Expenses |";
static String D = "| |";
// Scanner based constructor
public Sales(Scanner inFile) {
while (inFile.hasNext()) { // Loops for text values
name = inFile.nextLine(); // Takes text string value
while (inFile.hasNextInt()) { // Loops for int/float values
sales = inFile.nextInt(); // Takes int value
expenses = inFile.nextFloat(); // Takes float value
sumSales += sales; // Sum all sales values
sumExpenses += expenses; // Sum all expenses values
salesPer = (sales*100)/(float)sumSales; // Calculates percentage of total sales
expensesPer = (expenses*100)/sumExpenses; // Calculates percentage of total expenses
}
}
}
// Returns formatted output for printing
public String toString() {
return A + B + A + C + '\n' + D + " " + "$" + sumSales + " | "
+ "$" + sumExpenses + " |" + A + "\n| " + name + " | %" + salesPer
+ " | %" + expensesPer + " |" + A;
}
// Begin execution here - the driver
public static void main(String[] args) throws IOException {
Scanner inFile = new Scanner(new FileReader("sales.txt")); // Set scanner to read file
Sales output = new Sales(inFile); // Renews sales class to print toString results
// Associated with the sales text file
System.out.println('\n' + output.toString());
}
}