8 Replies - 351 Views - Last Post: 19 June 2013 - 09:18 AM Rate Topic: ***-- 2 Votes

#1 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Order Salespeople By Total Sales

Posted 19 June 2013 - 08:43 AM

I have this class I am suppose to write program for. It is almost done but I now have to compare who's annual sales was greater this one or that one. I am using an array list and now I am confused how to write that code. After I input the information from the keyboard well the code looks like this.
import java.util.Scanner;
import java.text.DecimalFormat; //Needed for Decimal formatting
import java.util.ArrayList;
public class SalesProgram
{
 
    public static void main(String[] args)
    {
       
        // declaring variables
        double annualSales; // this variable  holds the user's input
        String name;// Have to get a name variable
        Scanner input = new Scanner(System.in); // input from keyboard
        ArrayList<AddTotal> seller = new ArrayList<>();//Create new ArrayList
        /*     
         * creating a loop so i can add or create a salesAssociate object
         * to my AddTotal class because I know there should be more than one
         * person because i have to compare the two or more people's sales
         */
        do
       {
          //Create an new object from AddTotal
           AddTotal salesAssociate = new AddTotal();
            
           //Set salesperson's name
           System.out.println("Enter sales associates name and presss enter"+
                   " or type exit to end.");
           name = input.nextLine();
           if(name.equalsIgnoreCase("exit"))
               break;
           salesAssociate.setName(name);
            // set annual sales
           try{
           System.out.println("Please enter annual sales for the sales associates");
           }catch(Exception e){
             System.out.println("Just the actual ammount in numeric fashion.");
           }
           annualSales = input.nextDouble();
           salesAssociate.setSales(annualSales);
            
     // now where I was confused at earlier
     // add the object to the arraylist objects
        seller.add(salesAssociate);
        }while(true);
 
  //this is to make it look like a thousand or million with commas and decimals
    DecimalFormat currencyFormat = new DecimalFormat("#,##0.00");
    }// end Main

}// end SalesProgram.java



As you can see I am stuck because I am confused on how to write this part. how would I write an if statement simple enough that determines first which one has a larger sales then calculate by how much then finally write all this in.

would I write something similar to this
if(seller.get(whichIndexNumberForSalesAssociate)> seller.get(othersalesAssociate)// this is what really confuses me
// would i use a for loop I just cant figure this part out?



This post has been edited by macosxnerd101: 19 June 2013 - 08:45 AM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Order Salespeople By Total Sales

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 08:47 AM

You've really made zero effort to implement a solution to this. You're describing the logic fine, but we're not going to hold your hand through writing the code. Make an effort to implement something. Play around with it. Until you show some effort, we really can't be of much help.

By the way, you never took my advice from your last thread where I pointed you to an algorithm that does just what you are describing.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,880
  • Joined: 10-May 07

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 08:49 AM

View Postmacosxnerd101, on 19 June 2013 - 11:47 AM, said:

you never took my advice from your last thread

How?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 08:50 AM

You mean by looking up the algorithm and attempting to implement a solution?
Was This Post Helpful? 0
  • +
  • -

#5 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 09:00 AM

View Postmacosxnerd101, on 19 June 2013 - 10:47 AM, said:

You've really made zero effort to implement a solution to this. You're describing the logic fine, but we're not going to hold your hand through writing the code. Make an effort to implement something. Play around with it. Until you show some effort, we really can't be of much help.

By the way, you never took my advice from your last thread where I pointed you to an algorithm that does just what you are describing.

I did do that what I had to do because I needed a minimum of two people was created a easier way to do it sorry I though I added that part to this but this is what I had to do

public class SalesProgram 
{

 

    public static void main(String[] args) 
    {
      
        // declaring variables
        double annualSales; // this variable  holds the user's input 
        String name;// Have to get a name variable
        Scanner input = new Scanner(System.in); // input from keyboard
        ArrayList<AddTotal> seller = new ArrayList<>();//Create new ArrayList
        
        System.out.println("Press enter to continue or type exit to exit when done.");
        // creating iterator so I can compare sales on this and print out correct 
        //Iterator compareSales = seller.iterator();  
        /*
         * creating a loop so i can add or create a salesAssociate object
         * to my AddTotal class because I know there should be more than one
         * person because i have to compare the two or more people's sales
         */
         AddTotal salesAssociate;
        do 
        {
          //Create an new object from AddTotal
           salesAssociate = new AddTotal();
           
           //Set sales associate's name
           System.out.println("Enter sales associates name and presss enter or type exit to end.");
           name = input.nextLine();
           salesAssociate.setName(name);
            // interupts the loop to exit out ;)/>
           if(name.equalsIgnoreCase("exit"))
               break;
           // only sets name if name is not equal to exit
           
           
           // set annual sales
           System.out.println("Please enter annual sales for the sales associates");
           annualSales = input.nextDouble();
           salesAssociate.setSales(annualSales);
          System.out.println(salesAssociate.getTotalSalary()+" commission is: \n " + salesAssociate.getCommission());
           // add the object to the arraylist objects 
           seller.add(salesAssociate);
           
           // this line stops the loop from skipping to set annual sales
           input.nextLine();
        }while(true); // end do while loop
        
  
        int counter =0;
        //this is to make it look like a thousand or million with commas and decimals
        DecimalFormat currencyFormat = new DecimalFormat("#,##0.00");
 
          //returning
          
          salesAssociate = (AddTotal)seller.get(counter);
          double a = salesAssociate.getTotalSalary();
          String sA= salesAssociate.getName();
          double commA = salesAssociate.getCommission();
          salesAssociate = (AddTotal)seller.get(counter+1);
          double b = salesAssociate.getTotalSalary();
          String sB= salesAssociate.getName();
          double commB = salesAssociate.getCommission();
          if(a>B)/>
          {
            double c = a - b;
            System.out.printf("Sales associate %s made $%.2f in commission.%nSales Associate %s made $%.2f more than %s in total sales.%nAdditionally, %s made $%.2f annual gross.",sA,commA,sA,c,sB,sA,a);
            
          }else
          {
              double c = b - a;
             System.out.printf("Sales associate %s made $%.2f in commission.%nSales Associate %s made $%.2f more than %s in total sales.%nAdditionally, %s made $%.2f annual gross.",sB,commB,sB,c,sA,sB,B)/>;
          }
            
    }// end Main
       
}// end SalesProgram.java


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 09:02 AM

It's still really not clear what sort of help you're looking for. It sounds like you want to sort the data? You still haven't attempted to implement sorting with your ArrayList. It just doesn't scale to keep using individual variables like this.
Was This Post Helpful? 0
  • +
  • -

#7 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 09:06 AM

View Postmacosxnerd101, on 19 June 2013 - 11:02 AM, said:

It's still really not clear what sort of help you're looking for. It sounds like you want to sort the data? You still haven't attempted to implement sorting with your ArrayList. It just doesn't scale to keep using individual variables like this.

Basically I was trying to compare what sales person had made a greater sales that annual year and write out who was the victor, but I could not figure that part out. I was doing this for my class but now I just want to know a better way to this for future purposes. :)
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 09:07 AM

So this is just a find-max then. Basically, loop through the ArrayList. Store the first element in a temp variable. If the element at a given index has a greater sale, update the temp variable. The final value in your temp variable is the person with the greatest number of sales.
Was This Post Helpful? 1
  • +
  • -

#9 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Order Salespeople By Total Sales

Posted 19 June 2013 - 09:18 AM

View Postmacosxnerd101, on 19 June 2013 - 11:07 AM, said:

So this is just a find-max then. Basically, loop through the ArrayList. Store the first element in a temp variable. If the element at a given index has a greater sale, update the temp variable. The final value in your temp variable is the person with the greatest number of sales.

Ok I will try this on Saturday for practice thank you very much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1