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

                      How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                      155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                      Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?