Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-18-2012, 06:27 AM   PM User | #1
edd21
New to the CF scene

 
Join Date: Mar 2012
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
edd21 is an unknown quantity at this point
Trying to print contents of object filled array list.

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.

Code:
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);
		
		
	}
}
edd21 is offline   Reply With Quote
Old 03-18-2012, 07:36 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:40 AM.


Advertisement
Log in to turn off these ads.