0

I need to sort a text file that has text, whole integers and float integers. The code I have thus far is able to take the data and perform required calculations. Next I need help with sorting by last name and outputting data in a table. I was successful in getting just one line and desperately need help outputting a table.

The text file/list has the following sample data:
Adams Brian (representing name)
2374 345.57 (representing sales-whole Int and expenses-float Int respectively)
Kinsley James
11238 799.7
Brown Kim
23566 1117.11
.....and so on.

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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 salesPer;    // Sales as a percentage of total sales
static float expenses;    // Used for reading expenses
static float sumExpenses; // Total expenses
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 line value
while (inFile.hasNextInt()) {  // Loops for int/float values
sales = inFile.nextInt();      // Takes int value
expenses = inFile.nextFloat(); // Takes float value
sumSales += sales;             // Sums up all sales values
sumExpenses += expenses;       // Sums up all expenses
}
}
}
// Calculates percentage of total sales
public float getSalesPer() {
salesPer = (sales*100)/(float)sumSales;
return salesPer;
}
// Calculates percentage of total expenses
public float getExpensesPer() {
expensesPer = (expenses*100)/sumExpenses;
return expensesPer;
}
// Returns formatted output table with sales summary data for printing
public String toString() {
return A + B + A + C + '\n' + D + "       " + "$" + sumSales + "      |      "
+ "$" + sumExpenses + "     |" + A + "\n| " + name + "        |       " + getSalesPer()
+  "%      |       " + getExpensesPer() + "%       |" + A;
}

// 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"));
Sales results = new Sales(inFile);
// Print formated output table with sales summary data
System.out.println('\n' + results.toString());
}
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by cale.macdonald
0

You could start off by creating a class called Employee. Employee will have first name, last name, sales and expenses as possible attributes. Read through the file and create a new Employee object then put the employee object into some type of list. Have you employee class implement the Comparable interface. Write your compare to as you need. Then call Collections.sort on your list (with all the employee objects in it)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.