...

View Full Version : Trying to print contents of object filled array list.



edd21
03-18-2012, 07:27 AM
I am trying to print a sales report with each members name and their percentage of sales and expenses. I should be using a loop that runs the length of the sales list and use next() method to get each salesperson and then my observer methods to return the information about the salesperson. Something isn't right.



import java.util.Scanner;
import java.io.*;
import list.*;

public class TestSalesReport
{
SortedList list = new SortedList();
static String first;
static String last;
static int sales;
static float expenses;
static int salesTotal;
static float expensesTotal;

// Scanner based constructor.
public TestSalesReport(Scanner inFile)
{
SalesPerson person;
String first;
String last;
int sales;
float expenses;

while(inFile.hasNext())
{
last = inFile.next();
first = inFile.nextLine();
sales = inFile.nextInt();
expenses = inFile.nextFloat();
person = new SalesPerson(first, last, sales, expenses);
list.insert(person);
salesTotal = salesTotal + sales;
expensesTotal = expensesTotal + expenses;
salesTotal = salesTotal + sales;

}

}

public String getFirst(){return first;}
public String getLast(){return last;}
public int getSales(){ return sales;}
public float getExpenses(){return expenses;}

public void SalesPersons(String firsts, String lasts)
{
firsts = getFirst();
lasts = getLast();
System.out.println("These are sales persons: \n"+firsts + " " + lasts);
}

// Calculate sales percentages
public void SalesPercent(float salesPer) {
salesPer = getSales()*100/(float)salesTotal;
System.out.print(salesPer);
}

// Calculate expense percentages
public void ExpensePercent(float expensesPer) {
expensesPer = getExpenses()*100/expensesTotal;
System.out.print(expensesPer);
}

/*public void PrintReport(Scanner inFile)
{
for (int count = 0; count < 10; count++)
{
while (inFile.hasNext())
{
System.out.println(getFirst());
}
}
}*/

public static void main(String[] args) throws IOException
{
TestSalesReport salesReport;
Scanner inFile = new Scanner(new FileReader("sales.txt"));
salesReport = new TestSalesReport(inFile);

System.out.println(salesTotal + " " + expensesTotal);


}
}

Fou-Lu
03-18-2012, 08:36 PM
You'll need to fix all the issues with your custom List classes before you can address the problems you have here.
This class likely should have no reference to first, last, sales or expenses variables. These are all contained within individual SalesPerson objects stored within a List.

All this main does is print the totals. But you've calculated the total sales incorrectly by adding it twice during the constructor.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum