Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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);
    		
    		
    	}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •