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-17-2012, 04:04 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
Exclamation Wrong output from salesReport, array not filling properly.

I need to print out the percentages of total sales and total expenses for each salesperson. For some reason I am only getting the last salespersons name and percentages. Also before my list starts, it outputs all names and the last persons percentages.

Code:
   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
               reportSales.add(intgr);   
               Integer intArray[] = new Integer[reportSales.size()];
               reportSales.toArray(intArray);
               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
               reportExpenses.add(expn);
               Float floatArray[] = new Float[reportExpenses.size()];
               reportExpenses.toArray(floatArray);
               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(" ");
         //SalesTotals(0);
         //ExpenseTotals(0);
         //SalesPercent(0);
         //ExpensePercent(0);
         System.out.println(A+B+A+C);
         System.out.println(D+"         "+getSalesTotal()+"          "+getExpenseTotal()+A);
         System.out.print("|      "+getName()+"      ");
         SalesPercent(0);
         ExpensePercent(0);
      }
   	
   // 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);
      
         out.print();
      }
   }
The text file (sales.txt) shows:

Smith Kevin
80475 3966.27
Medina Norelis
71040 5677.21
Bailey Van
28305 11276.65
Keepes Karen
58830 7388.15
Keepes Ron
64935 6532.68
Carey Harry
34410 10810.03
Hairy Mata
40515 9954.56
Hurry Mata
52725 8243.62
Hurry Caine
46620 9099.09
Petz Edz
77145 4821.74
edd21 is offline   Reply With Quote
Old 03-17-2012, 06:42 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,742
Thanks: 4
Thanked 2,465 Times in 2,434 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
The ultimate problem here is that there is no collection of the names or totals. All you have is the last item in the list.
The constructor for creating the totals is mostly correct, you do need to chomp off the trailing linefeeds from the scanner though. When you do non-string > non-string, etc, then try to retrieve string, the results is that of the linefeeds left by the scanner when fetching the numbers. A simple call to nextLine() after you are complete with the numbers removes that excess line feed.

Personally, I'd write a new class to contain each seller and their totals. Then all you need is a collection of that type, and you can draw the totals (either statically from that new class summing up a collection, or from the reporting class which iterates the existing ones). Then its easy to construct too, with something like this:
PHP Code:
public SalesReport(Scanner in)
{
    while (
in != null && in.hasNext())
    {
        
this.sellersCollection.add(new SellerInformation(in.nextLine(), in.nextDouble(), in.nextDouble());
        
in.nextLine();
    }

The other pro to this is you can now use a Comparator to sort the sellersCollection.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
edd21 (03-17-2012)
Old 03-18-2012, 11:00 PM   PM User | #3
shakemelikeapig
New Coder

 
Join Date: Dec 2009
Posts: 63
Thanks: 0
Thanked 2 Times in 2 Posts
shakemelikeapig is an unknown quantity at this point
have you tried using asp cos i herd that php is to complicated
shakemelikeapig is offline   Reply With Quote
Old 03-18-2012, 11:11 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,742
Thanks: 4
Thanked 2,465 Times in 2,434 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
Quote:
Originally Posted by shakemelikeapig View Post
have you tried using asp cos i herd that php is to complicated
PHP's not complicated, but I fail to see any relevance to the question. This is Java, not PHP or C#.
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:51 PM.


Advertisement
Log in to turn off these ads.