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 7 of 7

Thread: How can I do this in Java Please??

  1. #1
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How can I do this in Java Please??

    I have made a program in Java that is designed to take user input in the form of a yearly sales total, and use it to calculate their yearly commission bonus, and then return this value to the user. I have it all in one main class now, but I would very much be interested in how to take the bottom half of my program code, and transfer it to a different external class in a different file....
    Here is my code:
    package annual_pay;
    import java.util.*;
    public class  {@SuppressWarnings("empty-statement")
        public static void main(String[] args)
        {   double T = 50000; //set value for base salary                
            double Amt; //declare variable for final pay
            int x = 1;
         do{
          try{ //validate user input
            Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");//request user input
            System.out.println("Please enter valid dollar value:");
            double yearly = percent.nextDouble(); //store user input
               if (yearly >= 0 && yearly <= 500000){ //if over $500,000.00 in sales
               if (yearly >80000){ //if 80% sales goal was not reached
            double YrPrcnt = yearly * .05; //value used for compensation set next
            Amt = T + YrPrcnt * 1.25; //set Amt to pay plus commissions
            System.out.print("The Annual Payrate for employee is: $");
            System.out.println(Amt);}
               else {
                 System.out.print("The Annual Payrate for employee is: $"); 
                 System.out.println(T);}}
                  else {
               System.err.print("Sales too high, ");
               System.err.println("please see Human Resources.");}
               x=2; break;
          }
            catch(Exception e){ //if user input throws exception
                System.out.println("Try again please");}}while(x==1);
     
           System.out.println("-------------------------------------------------");
           System.out.println ("Salesperson regular wages = $50,000");
           // 80% of sales target is computed
           // Target = 100000 * 80%.
           System.out.println("Sales Target is:");
           System.out.println ("$100,000 * 80% = $80,000");
           // Return 5% commission of sales target
           // commission = 5% * $100,000;
            System.out.println ("100,000 * 5% = $5000");
           // Return commission with acceleration factor
           // Commission = 5% + 1.25 acceleration factor;
            System.out.println ("5% + 1.25 = 6.25%");
            // Return potential.
            // Sales with 5% interest increment at 1.25 acceleration
            // factor until annual sales reach 50% of salesperson annaul sales
            // total sales = 50% + 6.25%;
            System.out.println ("50% + 6.25% = 56.25% ");        
            // Calculate potential compensation = 100,000 / 56.25%;
            System.out.println ("$100,000 / 56.25% = $56,250");
            // Calculate potential compensation by sales;
            // Return potential payrates with commissions;
            System.out.println("Potential compensations based on sales:");
            System.out.println("-------------------------------------");
            System.out.println ("$100,000 * 5% = $5,000");
            System.out.println ("$100,000 + $5,000 = Total Sales $105,000");
            System.out.println ("$105,000 / 56.25% = $56,562.50");
            System.out.println ("$110,000 / 56.25% = $61,875");
            System.out.println ("$115,000 / 56.25% = $64,687.50");
            System.out.println ("$120,000 / 56.25% = $67,500");
            System.out.println ("$125,000 / 56.25% = $70,312.50");
            System.out.println ("$130,000 / 56.25% = $73,125");
            System.out.println ("$135,000 / 56.25% = $75,937.50");
            System.out.println ("$140,000 / 56.25% = $78,750");
            System.out.println ("$145,000 / 56.25% = $81,562.50");
            System.out.println ("$150,000 / 56.25% = $84,375");}}
    //end program....
    and I would like to take this code block:
      // 80% of sales target is computed
           // Target = 100000 * 80%.
           System.out.println("Sales Target is:");
           System.out.println ("$100,000 * 80% = $80,000");
           // Return 5% commission of sales target
           // commission = 5% * $100,000;
            System.out.println ("100,000 * 5% = $5000");
           // Return commission with acceleration factor
           // Commission = 5% + 1.25 acceleration factor;
            System.out.println ("5% + 1.25 = 6.25%");
            // Return potential.
            // Sales with 5% interest increment at 1.25 acceleration
            // factor until annual sales reach 50% of salesperson annaul sales
            // total sales = 50% + 6.25%;
            System.out.println ("50% + 6.25% = 56.25% ");        
            // Calculate potential compensation = 100,000 / 56.25%;
            System.out.println ("$100,000 / 56.25% = $56,250");
            // Calculate potential compensation by sales;
            // Return potential payrates with commissions;
            System.out.println("Potential compensations based on sales:");
            System.out.println("-------------------------------------");
            System.out.println ("$100,000 * 5% = $5,000");
            System.out.println ("$100,000 + $5,000 = Total Sales $105,000");
            System.out.println ("$105,000 / 56.25% = $56,562.50");
            System.out.println ("$110,000 / 56.25% = $61,875");
            System.out.println ("$115,000 / 56.25% = $64,687.50");
            System.out.println ("$120,000 / 56.25% = $67,500");
            System.out.println ("$125,000 / 56.25% = $70,312.50");
            System.out.println ("$130,000 / 56.25% = $73,125");
            System.out.println ("$135,000 / 56.25% = $75,937.50");
            System.out.println ("$140,000 / 56.25% = $78,750");
            System.out.println ("$145,000 / 56.25% = $81,562.50");
            System.out.println ("$150,000 / 56.25% = $84,375");}}
    //end program....
    and create a second, external class within the same package to run this. This is the list of potential compensation amounts, based on yearly sales totals, and I would like for it to be in a different program file.
    If anyone could tell me the best way to move this code to a different file, and still be able to call it at the end of my initial program, I would be eternally grateful!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I do this in Java Please??

    Make a list of the functions that the code does. Then take a part of the code that does a one of those function and move it to its own method.
    I don't know why you want to create a separate class. Its possible for one class to have more than one method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: How can I do this in Java Please??

    Quote Originally Posted by Norm View Post
    I don't know why you want to create a separate class. Its possible for one class to have more than one method.
    I need the files to be in two separate instances , so that I can change the list when changes in policy dictate, but the initial user interface part of the program will be secured, and inaccessible to me once implemented. Having separate methods within that first program would just be something else that was locked away if I needed to revise it later....
    However, I will take the remainder of your advice , and see what I can come up with, thanks for that!!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I do this in Java Please??

    If you can split the existing method into multiple methods in one class, then it should be an easy step to move some of those methods to another class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    zlloyd1 (November 21st, 2012)

  6. #5
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: How can I do this in Java Please??

    Quote Originally Posted by Norm View Post
    If you can split the existing method into multiple methods in one class, then it should be an easy step to move some of those methods to another class.
    Thanks, I will do that and re-post if I run into problems!!

  7. #6
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Red face Re: How can I do this in Java Please??

    Quote Originally Posted by Norm View Post
    If you can split the existing method into multiple methods in one class, then it should be an easy step to move some of those methods to another class.
    Alright, I divided it into two separate classes within my program, but now it doesn't run correctly??
    Here is what I have now:
    package zackfinal;
    import java.util.*;
    public class ZackFinal {@SuppressWarnings("empty-statement")
        public static void main(String[] args)
        {   double T = 50000; //set value for base salary                
            double Amt; //declare variable for final pay
            int x = 1;
         do{
          try{ //validate user input
            Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");//request user input
            System.out.println("Please enter valid dollar value:");
            double yearly = percent.nextDouble(); //store user input
               if (yearly >= 0 && yearly <= 500000){ //if over $500,000.00 in sales
               if (yearly >80000){ //if 80% sales goal was not reached
            double YrPrcnt = yearly * .05; //value used for compensation set next
            Amt = T + YrPrcnt * 1.25; //set Amt to pay plus commissions
            System.out.print("The Annual Payrate for employee is: $");
            System.out.println(Amt);}
               else {
                 System.out.print("The Annual Payrate for employee is: $"); 
                 System.out.println(T);}}
                  else {
               System.err.print("Sales too high, ");
               System.err.println("please see Human Resources.");}
               x=2; break;
          }
            catch(Exception e){ //if user input throws exception
                System.out.println("Try again please");}}while(x==1);}
     
     
     
    class Chart extends ZackFinal{
        public void chart(String[] args){
           System.out.println("-------------------------------------------------");
           System.out.println ("Salesperson regular wages = $50,000");
           // 80% of sales target is computed
           // Target = 100000 * 80%.
           System.out.println("Sales Target is:");
           System.out.println ("$100,000 * 80% = $80,000");
           // Return 5% commission of sales target
           // commission = 5% * $100,000;
            System.out.println ("100,000 * 5% = $5000");
           // Return commission with acceleration factor
           // Commission = 5% + 1.25 acceleration factor;
            System.out.println ("5% + 1.25 = 6.25%");
            // Return potential.
            // Sales with 5% interest increment at 1.25 acceleration
            // factor until annual sales reach 50% of salesperson annaul sales
            // total sales = 50% + 6.25%;
            System.out.println ("50% + 6.25% = 56.25% ");        
            // Calculate potential compensation = 100,000 / 56.25%;
            System.out.println ("$100,000 / 56.25% = $56,250");
            // Calculate potential compensation by sales;
            // Return potential payrates with commissions;
            System.out.println("Potential compensations based on sales:");
            System.out.println("-------------------------------------");
            System.out.println ("$100,000 * 5% = $5,000");
            System.out.println ("$100,000 + $5,000 = Total Sales $105,000");
            System.out.println ("$105,000 / 56.25% = $56,562.50");
            System.out.println ("$110,000 / 56.25% = $61,875");
            System.out.println ("$115,000 / 56.25% = $64,687.50");
            System.out.println ("$120,000 / 56.25% = $67,500");
            System.out.println ("$125,000 / 56.25% = $70,312.50");
            System.out.println ("$130,000 / 56.25% = $73,125");
            System.out.println ("$135,000 / 56.25% = $75,937.50");
            System.out.println ("$140,000 / 56.25% = $78,750");
            System.out.println ("$145,000 / 56.25% = $81,562.50");}}}
    //end program....
    but for some reason, nothing after the beginning of class Chart does anything when I run it.... I mean it runs and compiles without errors, but the chart no longer displays to the user. I swear, I am about fed up with Java from messing with this, and would be eternally grateful if you could tell me what is wrong here, PLEASE!!

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I do this in Java Please??

    the chart no longer displays
    Where does your code create an instance of the Chart class and call any of its methods?
    Methods are only executed if they are called.

    You need to create an instance of the Chart class and call its method if you want that method to execute.


    The formatting of your code does not follow standards.
    Each } should be on a separate line with indentation that shows the nesting of the logic.

    There should never be more than one } on a line.
    If you don't understand my answer, don't ignore it, ask a question.

Tags for this Thread