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
               Integer intArray[] = new Integer[reportSales.size()];
               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
               Float floatArray[] = new Float[reportExpenses.size()];
               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(" ");
         System.out.println(D+"         "+getSalesTotal()+"          "+getExpenseTotal()+A);
         System.out.print("|      "+getName()+"      ");
   // 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);
6 Years
Discussion Span
Last Post by quuba

I haven't studied the code, but it looks like you have an array of names, slaes etc in the scanner constructor, but the getName, and the print don't loop thru that, they just access a single string?
I would have expected to see a SalesPerson class, each instance representing a single person, and a SealeReport class with an ArrayList of SalesPersons. YOu seem to have tried to both in one class, with inevutable confusion.
ps I can see no inner classes here


Here is not present inner class! Why inner?
You need class Person with fields: name, sales, expenses
with proper constructor and get...() methods.
List<Person> personList = new ArrayList<Person>();
1. operate with it to succesive store readed i formed new instances of person data,
2. at end - make all needed calculations

This article 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.