How to set salary for object before adding it arraylistHow do I efficiently iterate over each entry in a Java Map?Create ArrayList from arrayHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyIs null check needed before calling instanceof?Convert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?

How to creep the reader out with what seems like a normal person?

Is creating your own "experiment" considered cheating during a physics exam?

Did Henry V’s archers at Agincourt fight with no pants / breeches on because of dysentery?

Given what happens in Endgame, why doesn't Dormammu come back to attack the universe?

Subtleties of choosing the sequence of tenses in Russian

Confused by notation of atomic number Z and mass number A on periodic table of elements

What is the range of this combined function?

Were there two appearances of Stan Lee?

Stark VS Thanos

Reverse the word in a string with the same order in javascript

Need help understanding harmonic series and intervals

Why do Ichisongas hate elephants and hippos?

Weird result in complex limit

In gnome-terminal only 2 out of 3 zoom keys work

Multiple options for Pseudonyms

What's the metal clinking sound at the end of credits in Avengers: Endgame?

Pawn Sacrifice Justification

Python "triplet" dictionary?

What are the spoon bit of a spoon and fork bit of a fork called?

Feels like I am getting dragged in office politics

Are Boeing 737-800’s grounded?

Any examples of headwear for races with animal ears?

What's the polite way to say "I need to urinate"?

In the time of the mishna, were there Jewish cities without courts?



How to set salary for object before adding it arraylist


How do I efficiently iterate over each entry in a Java Map?Create ArrayList from arrayHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyIs null check needed before calling instanceof?Convert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















Im reading data from a text file
which includes information about employees details
as the following:




H ,name ,socialNumber,hours,rate

S,name,social number,salary




and add them to tow lists.



If they are hourly employee:



H.add(new Hourly(name ,number,hour,rate);


Else if they were salaried:



S.add(new salaried(name,number);


The question is: how to add the salary for the employee before adding the object to the list?

hourly = rate*hours

Salaried = salary

I have attribute salary
and mutator method which sets double salary



I also set the salary after adding the object to the list,
but the took the last salary form the text
The first 1500 the second 2000










share|improve this question



















  • 4





    Can't you just calculate that in the constructor of the objects, or create the objects above the add line, and set the salary before adding it in? Show some code as a Minimal, Complete, and Verifiable example and clarify what exactly you need help with.

    – Carcigenicate
    Mar 22 at 19:17












  • It works , obj 1 , 2 then set salary cause I know there is only tow employee What if I don’t now how many salaried employee? How many objects should I create ?

    – Faisal Garoush
    Mar 22 at 19:58












  • I tried to upload a photo of the project but it couldn’t upload

    – Faisal Garoush
    Mar 22 at 20:02











  • All relevant code should be posted here directly as text.

    – Carcigenicate
    Mar 22 at 20:05

















-1















Im reading data from a text file
which includes information about employees details
as the following:




H ,name ,socialNumber,hours,rate

S,name,social number,salary




and add them to tow lists.



If they are hourly employee:



H.add(new Hourly(name ,number,hour,rate);


Else if they were salaried:



S.add(new salaried(name,number);


The question is: how to add the salary for the employee before adding the object to the list?

hourly = rate*hours

Salaried = salary

I have attribute salary
and mutator method which sets double salary



I also set the salary after adding the object to the list,
but the took the last salary form the text
The first 1500 the second 2000










share|improve this question



















  • 4





    Can't you just calculate that in the constructor of the objects, or create the objects above the add line, and set the salary before adding it in? Show some code as a Minimal, Complete, and Verifiable example and clarify what exactly you need help with.

    – Carcigenicate
    Mar 22 at 19:17












  • It works , obj 1 , 2 then set salary cause I know there is only tow employee What if I don’t now how many salaried employee? How many objects should I create ?

    – Faisal Garoush
    Mar 22 at 19:58












  • I tried to upload a photo of the project but it couldn’t upload

    – Faisal Garoush
    Mar 22 at 20:02











  • All relevant code should be posted here directly as text.

    – Carcigenicate
    Mar 22 at 20:05













-1












-1








-1








Im reading data from a text file
which includes information about employees details
as the following:




H ,name ,socialNumber,hours,rate

S,name,social number,salary




and add them to tow lists.



If they are hourly employee:



H.add(new Hourly(name ,number,hour,rate);


Else if they were salaried:



S.add(new salaried(name,number);


The question is: how to add the salary for the employee before adding the object to the list?

hourly = rate*hours

Salaried = salary

I have attribute salary
and mutator method which sets double salary



I also set the salary after adding the object to the list,
but the took the last salary form the text
The first 1500 the second 2000










share|improve this question
















Im reading data from a text file
which includes information about employees details
as the following:




H ,name ,socialNumber,hours,rate

S,name,social number,salary




and add them to tow lists.



If they are hourly employee:



H.add(new Hourly(name ,number,hour,rate);


Else if they were salaried:



S.add(new salaried(name,number);


The question is: how to add the salary for the employee before adding the object to the list?

hourly = rate*hours

Salaried = salary

I have attribute salary
and mutator method which sets double salary



I also set the salary after adding the object to the list,
but the took the last salary form the text
The first 1500 the second 2000







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 19:32









Pavel Smirnov

2,5591818




2,5591818










asked Mar 22 at 19:15









Faisal GaroushFaisal Garoush

1




1







  • 4





    Can't you just calculate that in the constructor of the objects, or create the objects above the add line, and set the salary before adding it in? Show some code as a Minimal, Complete, and Verifiable example and clarify what exactly you need help with.

    – Carcigenicate
    Mar 22 at 19:17












  • It works , obj 1 , 2 then set salary cause I know there is only tow employee What if I don’t now how many salaried employee? How many objects should I create ?

    – Faisal Garoush
    Mar 22 at 19:58












  • I tried to upload a photo of the project but it couldn’t upload

    – Faisal Garoush
    Mar 22 at 20:02











  • All relevant code should be posted here directly as text.

    – Carcigenicate
    Mar 22 at 20:05












  • 4





    Can't you just calculate that in the constructor of the objects, or create the objects above the add line, and set the salary before adding it in? Show some code as a Minimal, Complete, and Verifiable example and clarify what exactly you need help with.

    – Carcigenicate
    Mar 22 at 19:17












  • It works , obj 1 , 2 then set salary cause I know there is only tow employee What if I don’t now how many salaried employee? How many objects should I create ?

    – Faisal Garoush
    Mar 22 at 19:58












  • I tried to upload a photo of the project but it couldn’t upload

    – Faisal Garoush
    Mar 22 at 20:02











  • All relevant code should be posted here directly as text.

    – Carcigenicate
    Mar 22 at 20:05







4




4





Can't you just calculate that in the constructor of the objects, or create the objects above the add line, and set the salary before adding it in? Show some code as a Minimal, Complete, and Verifiable example and clarify what exactly you need help with.

– Carcigenicate
Mar 22 at 19:17






Can't you just calculate that in the constructor of the objects, or create the objects above the add line, and set the salary before adding it in? Show some code as a Minimal, Complete, and Verifiable example and clarify what exactly you need help with.

– Carcigenicate
Mar 22 at 19:17














It works , obj 1 , 2 then set salary cause I know there is only tow employee What if I don’t now how many salaried employee? How many objects should I create ?

– Faisal Garoush
Mar 22 at 19:58






It works , obj 1 , 2 then set salary cause I know there is only tow employee What if I don’t now how many salaried employee? How many objects should I create ?

– Faisal Garoush
Mar 22 at 19:58














I tried to upload a photo of the project but it couldn’t upload

– Faisal Garoush
Mar 22 at 20:02





I tried to upload a photo of the project but it couldn’t upload

– Faisal Garoush
Mar 22 at 20:02













All relevant code should be posted here directly as text.

– Carcigenicate
Mar 22 at 20:05





All relevant code should be posted here directly as text.

– Carcigenicate
Mar 22 at 20:05












2 Answers
2






active

oldest

votes


















0














If you want to set the Salary before adding it to the List, you can define the new Entry before, change the Salary and then adding it:



Salaried s = Salaried(name,number);
s.setSalary(computedSalary);
S.add(s);


I hope that help you,



Regards






share|improve this answer






























    0














    You may consider the builder pattern, which offers a flexible way of constructing objects:



    public class Salaried 
    String name;
    String socialNumber;
    Double salary;

    ...

    public static class Builder
    String name;
    String socialNumber;
    Double salary;

    public Builder(String name, String socialNumber)
    this.name = name;
    this.socialNumber = socialNumber;


    public Builder salary(Double salary)
    this.salary = salary;
    return this;


    public Salaried build()
    Salaried salaried = new Salaried(name, socialNumber);
    salaried.setSalary(salary);
    return salaried;




    S.add(new Salaried.Builder("bob","12345678").salary(40000).build());





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306425%2fhow-to-set-salary-for-object-before-adding-it-arraylist%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you want to set the Salary before adding it to the List, you can define the new Entry before, change the Salary and then adding it:



      Salaried s = Salaried(name,number);
      s.setSalary(computedSalary);
      S.add(s);


      I hope that help you,



      Regards






      share|improve this answer



























        0














        If you want to set the Salary before adding it to the List, you can define the new Entry before, change the Salary and then adding it:



        Salaried s = Salaried(name,number);
        s.setSalary(computedSalary);
        S.add(s);


        I hope that help you,



        Regards






        share|improve this answer

























          0












          0








          0







          If you want to set the Salary before adding it to the List, you can define the new Entry before, change the Salary and then adding it:



          Salaried s = Salaried(name,number);
          s.setSalary(computedSalary);
          S.add(s);


          I hope that help you,



          Regards






          share|improve this answer













          If you want to set the Salary before adding it to the List, you can define the new Entry before, change the Salary and then adding it:



          Salaried s = Salaried(name,number);
          s.setSalary(computedSalary);
          S.add(s);


          I hope that help you,



          Regards







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 19:37









          Alejandro GranadosAlejandro Granados

          1086




          1086























              0














              You may consider the builder pattern, which offers a flexible way of constructing objects:



              public class Salaried 
              String name;
              String socialNumber;
              Double salary;

              ...

              public static class Builder
              String name;
              String socialNumber;
              Double salary;

              public Builder(String name, String socialNumber)
              this.name = name;
              this.socialNumber = socialNumber;


              public Builder salary(Double salary)
              this.salary = salary;
              return this;


              public Salaried build()
              Salaried salaried = new Salaried(name, socialNumber);
              salaried.setSalary(salary);
              return salaried;




              S.add(new Salaried.Builder("bob","12345678").salary(40000).build());





              share|improve this answer



























                0














                You may consider the builder pattern, which offers a flexible way of constructing objects:



                public class Salaried 
                String name;
                String socialNumber;
                Double salary;

                ...

                public static class Builder
                String name;
                String socialNumber;
                Double salary;

                public Builder(String name, String socialNumber)
                this.name = name;
                this.socialNumber = socialNumber;


                public Builder salary(Double salary)
                this.salary = salary;
                return this;


                public Salaried build()
                Salaried salaried = new Salaried(name, socialNumber);
                salaried.setSalary(salary);
                return salaried;




                S.add(new Salaried.Builder("bob","12345678").salary(40000).build());





                share|improve this answer

























                  0












                  0








                  0







                  You may consider the builder pattern, which offers a flexible way of constructing objects:



                  public class Salaried 
                  String name;
                  String socialNumber;
                  Double salary;

                  ...

                  public static class Builder
                  String name;
                  String socialNumber;
                  Double salary;

                  public Builder(String name, String socialNumber)
                  this.name = name;
                  this.socialNumber = socialNumber;


                  public Builder salary(Double salary)
                  this.salary = salary;
                  return this;


                  public Salaried build()
                  Salaried salaried = new Salaried(name, socialNumber);
                  salaried.setSalary(salary);
                  return salaried;




                  S.add(new Salaried.Builder("bob","12345678").salary(40000).build());





                  share|improve this answer













                  You may consider the builder pattern, which offers a flexible way of constructing objects:



                  public class Salaried 
                  String name;
                  String socialNumber;
                  Double salary;

                  ...

                  public static class Builder
                  String name;
                  String socialNumber;
                  Double salary;

                  public Builder(String name, String socialNumber)
                  this.name = name;
                  this.socialNumber = socialNumber;


                  public Builder salary(Double salary)
                  this.salary = salary;
                  return this;


                  public Salaried build()
                  Salaried salaried = new Salaried(name, socialNumber);
                  salaried.setSalary(salary);
                  return salaried;




                  S.add(new Salaried.Builder("bob","12345678").salary(40000).build());






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 19:56









                  Jin KimJin Kim

                  7,121134373




                  7,121134373



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306425%2fhow-to-set-salary-for-object-before-adding-it-arraylist%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript