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;
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
add a comment |
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
4
Can't you just calculate that in the constructor of the objects, or create the objects above theadd
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
add a comment |
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
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
java
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 theadd
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
add a comment |
4
Can't you just calculate that in the constructor of the objects, or create the objects above theadd
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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());
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Mar 22 at 19:37
Alejandro GranadosAlejandro Granados
1086
1086
add a comment |
add a comment |
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());
add a comment |
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());
add a comment |
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());
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());
answered Mar 22 at 19:56
Jin KimJin Kim
7,121134373
7,121134373
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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