Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Help with my program

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with my program

    Hi All,

    I am trying to write a program that basically determines the max amount of units sold and displays it in a dialog box. Also, the program will display the winning sales associate out of 10. I have wrote the program successfully so far and I can see the maximum number of units sold displayed in the dialog box, but I cannot seem to link the winning sales person.

    Here is the code I have typed so far.

    //Program to find maximum of three number


    import javax.swing.JOptionPane;
    import java.util.Scanner;
    public class Sales
    {
    public static void main (String[] args)
    {

    //Declare variables
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
    int large;
    int winner;

    //Create scanner object to receive user input
    Scanner input = new Scanner(System.in);

    //Prompt the user for input numbers
    System.out.print("Enter units sold by sales person # 1 : ");
    num1 = input.nextInt();

    System.out.print("Enter units sold by sales person # 2 : ");
    num2 = input.nextInt();

    System.out.print("Enter units sold by sales person # 3 : ");
    num3 = input.nextInt();

    System.out.print("Enter units sold by sales person # 4 : ");
    num4 = input.nextInt();

    System.out.print("Enter units sold by sales person # 5 : ");
    num5 = input.nextInt();

    System.out.print("Enter units sold by sales person # 6 : ");
    num6 = input.nextInt();

    System.out.print("Enter units sold by sales person # 7 : ");
    num7 = input.nextInt();

    System.out.print("Enter units sold by sales person # 8 : ");
    num8 = input.nextInt();

    System.out.print("Enter units sold by sales person # 9 : ");
    num9 = input.nextInt();

    System.out.print("Enter units sold by sales person # 10 : ");
    num10 = input.nextInt();



    //Initialize large number
    large = num1;
    winner = num1;

    //Compare other numbers with large and reassign

    if (num2 > large)
    large = num2; //assign num2 to large

    if (num2 > winner)
    winner = num2;

    if (num3 > large)
    large = num3; //assign num3 to large

    if (num3 > winner)
    winner = num3;

    if (num4 > large)
    large = num4; //assign num4 to large

    if (num4 > winner)
    winner = num4;

    if (num5 > large)
    large = num5; //assign num5 to large

    if (num5 > winner)
    winner = num5;

    if (num6 > large)
    large = num6; //assign num6 to large

    if (num6 > winner)
    winner = num6;

    if (num7 > large)
    large = num7; //assign num7 to large

    if (num7 > winner)
    winner = num7;

    if (num8 > large)
    large = num8; //assign num8 to large

    if (num8 > winner)
    winner = num8;

    if (num9 > large)
    large = num9; //assign num9 to large

    if (num9 > winner)
    winner = num9;

    if (num10 > large)
    large = num10; //assign num10 to large

    if (num10 > winner)
    winner = num10;


    //Extra line

    System.out.println();

    //Print the largest number

    //System.out.printf("Maximum units sold: %d \n", large);


    String message = "";

    message = message + String.format("Maximum units sold: %d \n", large);

    message = message + String.format("Winning sales person: %d", winner);

    JOptionPane.showMessageDialog (null, message);

    }//End main method

    }//End class


  2. #2
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Help with my program

    What do you want to show as the winner. Right now you are copying the same value of highest sales to the winner as well, and that is working fine.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with my program

    Well when you're assigning winner, you're assigning the amount the person sold, rather than which person sold it.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Help with my program

    Hi,

    as per my understanding of what you are trying to do, does sample output below fit your need?

    eg :

    Enter units sold by sales person # 1 : 5
    Enter units sold by sales person # 2 : 1

    Maximum units sold: 5
    Winning sales person: 1

    maybe you could use a POJO? (Plain Old Java Object)

    class salesVO{
     
    	int sales;
    	int salePerson;
    	public int getSales() {
    		return sales;
    	}
    	public void setSales(int sales) {
    		this.sales = sales;
    	}
    	public int getSalePerson() {
    		return salePerson;
    	}
    	public void setSalePerson(int salePerson) {
    		this.salePerson = salePerson;
    	}
     
     
     
    }

    basically you make an array of salesVO and use it to relate the sales person to the number of greatest sales

    eg :

    		//Prompt the user for input numbers
     
    		salesVO[] sVO = new salesVO[10]; 
     
    		// maybe instead of repeating this block of code via hard coding
    		// use for loop?
    		System.out.print("Enter units sold by sales person # 1 : ");
    		num1 = input.nextInt();
    		sVO[1].setSales(input.nextInt());
    		sVO[1].setSalePerson(1);

    other options that i can think of are maps, and 2d Arrays.


    regards

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with my program

    Quote Originally Posted by destitute.developer View Post

    		//Prompt the user for input numbers
     
    		salesVO[] sVO = new salesVO[10]; 
     
    		// maybe instead of repeating this block of code via hard coding
    		// use for loop?
    		System.out.print("Enter units sold by sales person # 1 : ");
    		num1 = input.nextInt();
    		sVO[1].setSales(input.nextInt());
    		sVO[1].setSalePerson(1);
    You would want to put the sale person starting at position 0 instead of 1

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Help with my program

    a yes sorry, my bad thank you for pointing it out
    carelessness will definitely be the end of me

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with my program

    So it's solved then?

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my program

    Quote Originally Posted by destitute.developer View Post
    Hi,

    as per my understanding of what you are trying to do, does sample output below fit your need?

    eg :

    Enter units sold by sales person # 1 : 5
    Enter units sold by sales person # 2 : 1

    Maximum units sold: 5
    Winning sales person: 1

    maybe you could use a POJO? (Plain Old Java Object)

    class salesVO{
     
    	int sales;
    	int salePerson;
    	public int getSales() {
    		return sales;
    	}
    	public void setSales(int sales) {
    		this.sales = sales;
    	}
    	public int getSalePerson() {
    		return salePerson;
    	}
    	public void setSalePerson(int salePerson) {
    		this.salePerson = salePerson;
    	}
     
     
     
    }

    basically you make an array of salesVO and use it to relate the sales person to the number of greatest sales

    eg :

    		//Prompt the user for input numbers
     
    		salesVO[] sVO = new salesVO[10]; 
     
    		// maybe instead of repeating this block of code via hard coding
    		// use for loop?
    		System.out.print("Enter units sold by sales person # 1 : ");
    		num1 = input.nextInt();
    		sVO[1].setSales(input.nextInt());
    		sVO[1].setSalePerson(1);

    other options that i can think of are maps, and 2d Arrays.


    regards





    Hello,

    Yes you are correct. But I have not learned about this in my class so I cannot use it. Are there any other basic java concepts that can achieve this? This is for a HW assignment I am trying to complete.

    --- Update ---

    How would I assign which person sold it?

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Help with my program

    Hi

    how about arrays?

    eg :
    		int[][] x= new int[10][2];
     
    		x[0][0]  = 100; // sales
    		x[0][1] = 1; //agent	
     
                    x[1][0] = 20;
                    x[1][1] = 2;
    ...till 10 inputs are acquired.

    actually, how far did your class covered? just to have an idea on what your looking for

    regards

  10. #10
    Junior Member
    Join Date
    Feb 2014
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my program

    Quote Originally Posted by destitute.developer View Post
    Hi

    how about arrays?

    eg :
    		int[][] x= new int[10][2];
     
    		x[0][0]  = 100; // sales
    		x[0][1] = 1; //agent	
     
                    x[1][0] = 20;
                    x[1][1] = 2;
    ...till 10 inputs are acquired.

    actually, how far did your class covered? just to have an idea on what your looking for

    regards


    We have not done arrays yet. So far I have changed the code and maybe this will help.

    //Program to find maximum of three number


    import javax.swing.JOptionPane;
    import java.util.Scanner;
    public class Sales
    {
    public static void main (String[] args)
    {

    //Declare variables
    int large = 0;
    int num;
    int counter = 0;
    int winner = 0;

    //Create scanner object to receive user input
    Scanner input = new Scanner(System.in);

    while (counter <10)
    {

    System.out.print("Enter units sold by sales person # %d " +counter);
    num = input.nextInt();

    if (num > large)
    {

    large = num;
    winner = counter;

    }

    counter++;

    }


    String message = "";

    message = message + String.format("Maximum units sold: %d \n", large);

    message = message + String.format("Winning sales person: %d", counter);

    JOptionPane.showMessageDialog (null, message);

    }//End main method

    }//End class

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Help with my program

    you have actually solved it already

    though
    message = message + String.format("Winning sales person: %d", counter);

    this line will always display the max value of 'counter'.

    try displaying 'winner'

    regards

  12. The Following User Says Thank You to destitute.developer For This Useful Post:

    drumforzildjian (February 10th, 2014)

  13. #12
    Junior Member
    Join Date
    Feb 2014
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking Re: Help with my program

    Quote Originally Posted by destitute.developer View Post
    you have actually solved it already

    though
    message = message + String.format("Winning sales person: %d", counter);

    this line will always display the max value of 'counter'.

    try displaying 'winner'

    regards
    HOORAY!!!!!!
    It works! This is awesome. Thank you so so so much! I will have a 100 on my HW.

Similar Threads

  1. Replies: 1
    Last Post: February 6th, 2014, 01:11 PM
  2. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  3. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM