![]() |
Needs help in editing
Hi guys: Can any one please help me how to i do the changes in my
program according to the following 4 changes. 1) Create a Date type birth data instance field in the Employee class, not a String 2) Enlarge the constructors for the Employee class and subclasses to pass the int month, int day, and int year of the birth date as input by the user to PayrollSystemTest to the subclass constructor, and then to the Employee class constructor 3) Input from the user for the 5 specific employees and then comment out the hardcoding for the 4 employees in the original code 4) Report monthly salary amounts and include the November birthday bonus import java.util.ArrayList; import java.util.Date; import java.util.Scanner; public class Employee { String socialSecurity; String birthDate; public Employee(String socialSecurity, String birthDate){ this.birthDate = birthDate; this.socialSecurity = socialSecurity; } public String getSocialSecurity(){ return socialSecurity; } public String getBirthDate(){ return birthDate; } public String toString(){ return ("Employee: Social security " + socialSecurity + " date of birth " + birthDate); } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter employees one by one"); ArrayList<Employee> ar = new ArrayList<Employee>(); while(true){ System.out.println(" Employee types available:"); System.out.println(""); System.out.println("1-Salaried Employee"); System.out.println("2-Hourly Employee"); System.out.println("3-Commission Employee"); System.out.println("4-Based Salary Commission Employee"); System.out.println(""); System.out.println("Enter employee type (1-4) . Finish list with type 0: "); String typeString = keyboard.next(); // boolean goodInput = true; int type = -1; try{ type = Integer.parseInt(typeString); } catch(Exception ex){ } if(type <0 || type > 4){ System.out.println("Invalid type. Please, try again. "); continue; } if(type == 0)break; System.out.println("Enter social security: "); String social = keyboard.next(); System.out.println("Enter date of birth: "); String birth = keyboard.next(); boolean goodInput = true; switch(type){ case 1: System.out.println("Enter salary: "); String salString = keyboard.next(); double salary = -1.0; try{ salary = Double.parseDouble(salString); } catch(Exception ex){ } if(salary < 0){ System.out.println("Invalid input. Try again"); goodInput = false; break; } SalariedEmployee emp = new SalariedEmployee(social, birth, salary); ar.add(emp); break; case 2: System.out.println("Enter hourly wage: "); String hourlyString = keyboard.next(); System.out.println("Enter hours worked: "); String hoursString = keyboard.next(); double hourlyWage = -1.0; double hoursWorked = -1.0; try{ hourlyWage = Double.parseDouble(hourlyString ); hoursWorked = Double.parseDouble(hoursString ); } catch(Exception ex){ } if(hourlyWage < 0.0 || hoursWorked < 0.0){ System.out.println("Invalid input. Try again"); goodInput = false; break; } HourlyEmployee emp1 = new HourlyEmployee(social, birth, hourlyWage, hoursWorked); ar.add(emp1); break; case 3: System.out.println("Enter commission rate: "); String commissionString = keyboard.next(); System.out.println("Enter gross sales: "); String salesString = keyboard.next(); double commissionRate = -1.0; double grossSales = -1.0; try{ commissionRate = Double.parseDouble(commissionString ); grossSales = Double.parseDouble(salesString ); } catch(Exception ex){ } if(commissionRate < 0.0 || grossSales < 0.0){ System.out.println("Invalid input. Try again"); goodInput = false; break; } CommissionEmployee emp2 = new CommissionEmployee(social, birth, commissionRate, grossSales); ar.add(emp2); break; case 4: System.out.println("Enter commission rate: "); commissionString = keyboard.next(); System.out.println("Enter gross sales: "); salesString = keyboard.next(); System.out.println("Enter salary: "); salString = keyboard.next(); commissionRate = -1.0; grossSales = -1.0; salary = -1.0; try{ commissionRate = Double.parseDouble(commissionString ); grossSales = Double.parseDouble(salesString ); salary = Double.parseDouble(salString ); } catch(Exception ex){ } if(commissionRate < 0.0 || grossSales < 0.0 || salary < 0){ System.out.println("Invalid input. Try again"); goodInput = false; break; } SalaryBasedCommissionEmployee emp3 = new SalaryBasedCommissionEmployee(social, birth, commissionRate, grossSales, salary); ar.add(emp3); } } for (int i = 0; i < ar.size(); i++) { Employee employee = ar.get(i); System.out.println(employee); } } } class SalariedEmployee extends Employee{ double salary; public SalariedEmployee(String socialSecurity, String birthDate, double salary){ super(socialSecurity, birthDate); this.salary = salary; } public double getSalary(){ return salary; } public String toString(){ return ("Salaried Employee: Social security " + socialSecurity + " date of birth " + birthDate + " salary " + salary); } } class HourlyEmployee extends Employee { double hourlyWage; double hoursWorked; public HourlyEmployee(String socialSecurity, String birthDate, double hourlyWage, double hoursWorked){ super(socialSecurity, birthDate); this.hourlyWage = hourlyWage; this.hoursWorked = hoursWorked; } public String toString(){ return ("Hourly Employee: Social security " + socialSecurity + " date of birth " + birthDate + " hourlyWage " + hourlyWage + " hoursWorked " + hoursWorked); } } class CommissionEmployee extends Employee { double grossSales; double commissionRate; public CommissionEmployee(String socialSecurity, String birthDate, double grossSales, double commissionRate){ super(socialSecurity, birthDate); this.grossSales = grossSales; this.commissionRate = commissionRate; } public String toString(){ return ("Commission Employee: Social security " + socialSecurity + " date of birth " + birthDate + " Commission Rate " + commissionRate + " Gross Sales " + grossSales); } } class SalaryBasedCommissionEmployee extends CommissionEmployee { double salary; public SalaryBasedCommissionEmployee(String socialSecurity, String birthDate, double grossSales, double commissionRate, double salary){ super(socialSecurity, birthDate, grossSales, commissionRate); this.salary = salary; } public String toString(){ return ("Salary Based Commission Employee: Social security " + socialSecurity + " date of birth " + birthDate + " Commission Rate " + commissionRate + " Gross Sales " + grossSales + " Salary " + salary); } } |
Re: Needs help in editing
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Le 23/06/2011 20:25, Eric a écrit : > 1) Create a Date type birth data instance field in the Employee class, > not a String org.joda.time.DateTime from Joda-time library > 2) Enlarge the constructors for the Employee class and subclasses to > pass the int month, int day, and int year of the birth date as input > by the user to PayrollSystemTest to the subclass constructor, and then > to the Employee class constructor What the usefulness ? Datetime can be parsed directly from text like 2011-06-24 (ISO format or other) and constructors *must* take Datetime in parameter, not month/day/year. Ultimately, make a DateFactory or use Joda-time API to convert fields to datetime. > 3) Input from the user for the 5 specific employees and then comment > out the hardcoding for the 4 employees in the original code public enum EmployeeType { SALARIED, HOURLY, COMMISSION, SALARY_BASED_COMMISSION } And please : — Learn Java, all those questions are very very basic… — Don't post so many and so ugly craps code… - -- Aeris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOA7+yAAoJEK8zQvxDY4P9aIwH/0V6g8aXD2n6LBIYSQdUg97n +NY/pWEdOdxaKSX1ScQIlxHWBiX0Eg5/3KNaX5Q8VbS8fFAqYMt6UZRGKREBsAN/ r5dWxgYvK7/ZHDInKg3q8mEqWI+P+YpH1nB2sNlW0dHf3JjvRqPg3qWwrYC3g wkc kQtDqZaJugVfAlz+tHm9N9YPyFTHuT0b3ZBUB9BI1PjdfHHzn1 NR0n/FJULSL/SH pwwPM+QPzDwQeH9+sv847J4iIjH9nW4VwFp85yh4J9pV0iDw59/8DgCZvTGnIpJp dwlmIJFMorMqiVB+uQ+ACaAZ3Mpusk3tddI3/S9iaV3dTvFSKq6Lb1E1h3tCQG0= =sQpY -----END PGP SIGNATURE----- |
Re: Needs help in editing
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Le 24/06/2011 00:35, Aéris a écrit : >> 3) Input from the user for the 5 specific employees and then comment >> > out the hardcoding for the 4 employees in the original code > public enum EmployeeType { > SALARIED, HOURLY, COMMISSION, SALARY_BASED_COMMISSION > } I forgot to say you must use an EmployeeFactory to create employee from user input (with reflection for example) to avoid harcoded craps switch case. - -- Aeris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOA8CjAAoJEK8zQvxDY4P9MDkH/2PXWKeE8JJ9kPLlNZoS6pl+ c2352NA4XpWskE8iOFpT3KBmyQqfv8uclXU7ersbke6vVbmPH7 izNWlxgasBJ8eX CeNkknTzdR9m/SdapOpDbGKQgPsoUM6bbZcaU1qJRmMmsEMV6gRDxN6lf0lzoJj U xSXyJLxMQoQ6UFH8STNEQ5yUh6euQAxnAZtKVlzh31Txhvp3hk yzQpS3rLselGjO D4x1SfJUAetmE4zqOY8BAKdjy+SBd2vyPCqA96aTndicVfn73j ADKeKaBS+pbf7g tRdBFID+BszOFu5AEIRYfF0v6YYSALAyAfm7aGbPSW2Ef/E1ireQckoH8NZp3vs= =ZctI -----END PGP SIGNATURE----- |
Re: Needs help in editing
I think we are being asked to someone's school assignment. Better he does it himself. On 11-06-23 02:25 PM, Eric wrote: > Hi guys: Can any one please help me how to i do the changes in my > program according to the following 4 changes. > > 1) Create a Date type birth data instance field in the Employee class, > not a String > > 2) Enlarge the constructors for the Employee class and subclasses to > pass the int month, int day, and int year of the birth date as input > by the user to PayrollSystemTest to the subclass constructor, and then > to the Employee class constructor > > 3) Input from the user for the 5 specific employees and then comment > out the hardcoding for the 4 employees in the original code > > 4) Report monthly salary amounts and include the November birthday > bonus > > import java.util.ArrayList; > import java.util.Date; > import java.util.Scanner; > > public class Employee { > > String socialSecurity; > String birthDate; > > public Employee(String socialSecurity, String birthDate){ > this.birthDate = birthDate; > this.socialSecurity = socialSecurity; > } > > public String getSocialSecurity(){ > return socialSecurity; > } > > public String getBirthDate(){ > return birthDate; > } > > public String toString(){ > return ("Employee: Social security " + socialSecurity > + " date of birth " + birthDate); > } > > public static void main(String[] args) { > > Scanner keyboard = new Scanner(System.in); > System.out.println("Enter employees one by one"); > ArrayList<Employee> ar = new ArrayList<Employee>(); > > while(true){ > System.out.println(" Employee types available:"); > System.out.println(""); > System.out.println("1-Salaried Employee"); > System.out.println("2-Hourly Employee"); > System.out.println("3-Commission Employee"); > System.out.println("4-Based Salary Commission > Employee"); > System.out.println(""); > System.out.println("Enter employee type (1-4) . > Finish list with type 0: "); > > String typeString = keyboard.next(); > // boolean goodInput = true; > int type = -1; > try{ > type = Integer.parseInt(typeString); > } catch(Exception ex){ > > } > > if(type<0 || type> 4){ > System.out.println("Invalid type. Please, try > again. "); > continue; > } > > if(type == 0)break; > > System.out.println("Enter social security: "); > String social = keyboard.next(); > System.out.println("Enter date of birth: "); > String birth = keyboard.next(); > > boolean goodInput = true; > switch(type){ > case 1: > System.out.println("Enter salary: "); > String salString = keyboard.next(); > double salary = -1.0; > try{ > salary = > Double.parseDouble(salString); > } catch(Exception ex){ > > } > if(salary< 0){ > System.out.println("Invalid input. Try > again"); > goodInput = false; > break; > } > SalariedEmployee emp = new > SalariedEmployee(social, birth, salary); > ar.add(emp); > break; > > case 2: > System.out.println("Enter hourly wage: > "); > String hourlyString = keyboard.next(); > System.out.println("Enter hours worked: > "); > String hoursString = > keyboard.next(); > double hourlyWage = -1.0; > double hoursWorked = -1.0; > try{ > hourlyWage = > Double.parseDouble(hourlyString ); > hoursWorked = > Double.parseDouble(hoursString ); > } catch(Exception ex){ > > } > > if(hourlyWage< 0.0 || hoursWorked< 0.0){ > System.out.println("Invalid input. Try > again"); > goodInput = false; > break; > } > HourlyEmployee emp1 = new > HourlyEmployee(social, birth, hourlyWage, hoursWorked); > ar.add(emp1); > break; > > case 3: > System.out.println("Enter > commission rate: "); > String commissionString = > keyboard.next(); > System.out.println("Enter gross sales: "); > String salesString = > keyboard.next(); > double commissionRate = -1.0; > double grossSales = -1.0; > try{ > commissionRate = > Double.parseDouble(commissionString ); > grossSales = > Double.parseDouble(salesString ); > } catch(Exception ex){ > > } > > if(commissionRate< 0.0 || grossSales< > 0.0){ > System.out.println("Invalid input. Try > again"); > goodInput = false; > break; > } > CommissionEmployee emp2 = new > CommissionEmployee(social, birth, commissionRate, grossSales); > ar.add(emp2); > break; > > case 4: > System.out.println("Enter > commission rate: "); > commissionString = keyboard.next(); > System.out.println("Enter gross sales: "); > salesString = > keyboard.next(); > System.out.println("Enter salary: "); > salString = keyboard.next(); > commissionRate = -1.0; > grossSales = -1.0; > salary = -1.0; > try{ > commissionRate = > Double.parseDouble(commissionString ); > grossSales = > Double.parseDouble(salesString ); > salary = > Double.parseDouble(salString ); > } catch(Exception ex){ > > } > if(commissionRate< 0.0 || grossSales > < 0.0 || salary< 0){ > System.out.println("Invalid input. Try > again"); > goodInput = false; > break; > } > > SalaryBasedCommissionEmployee emp3 = new > SalaryBasedCommissionEmployee(social, birth, commissionRate, > grossSales, salary); > ar.add(emp3); > } > > > > > > } > > for (int i = 0; i< ar.size(); i++) { > Employee employee = ar.get(i); > System.out.println(employee); > } > > > } > > > } > > class SalariedEmployee extends Employee{ > double salary; > > public SalariedEmployee(String socialSecurity, String > birthDate, double salary){ > super(socialSecurity, birthDate); > this.salary = salary; > > } > > public double getSalary(){ > return salary; > } > > > public String toString(){ > return ("Salaried Employee: Social security " + > socialSecurity + " date of birth " + birthDate + " salary " + > salary); > } > > } > > class HourlyEmployee extends Employee { > double hourlyWage; > double hoursWorked; > > public HourlyEmployee(String socialSecurity, String > birthDate, double hourlyWage, double hoursWorked){ > super(socialSecurity, birthDate); > this.hourlyWage = hourlyWage; > this.hoursWorked = hoursWorked; > > } > > public String toString(){ > return ("Hourly Employee: Social security " + > socialSecurity + " date of birth " + birthDate + " hourlyWage > " + hourlyWage + " hoursWorked " + hoursWorked); > } > > > } > > class CommissionEmployee extends Employee { > double grossSales; > double commissionRate; > > > public CommissionEmployee(String socialSecurity, String > birthDate, double grossSales, double commissionRate){ > super(socialSecurity, birthDate); > this.grossSales = grossSales; > this.commissionRate = commissionRate; > > } > > public String toString(){ > return ("Commission Employee: Social security " + > socialSecurity + " date of birth " + birthDate + " Commission > Rate " + commissionRate + " Gross Sales " + grossSales); > } > > > } > > class SalaryBasedCommissionEmployee extends CommissionEmployee > { > double salary; > > > > public SalaryBasedCommissionEmployee(String > socialSecurity, String birthDate, double grossSales, double > commissionRate, double salary){ > super(socialSecurity, birthDate, grossSales, > commissionRate); > this.salary = salary; > > > } > > public String toString(){ > return ("Salary Based Commission Employee: Social > security " + socialSecurity + " date of birth " + birthDate + > " Commission Rate " + commissionRate > + " Gross Sales " + grossSales + " Salary " + > salary); > } > > > } |
Re: Needs help in editing
On Jun 23, 3:39*pm, Aéris <ae...@imirhil.fr> wrote:
> I forgot to say you must use an EmployeeFactory to create employee from > user input (with reflection for example) to avoid harcoded craps switch > case. Silly advice to give a newbie. Actually, silly advice to give anyone. There's no "must use" in factory methods or classes. Reflection is an elephant gun for shooting fleas; simple polymorphism suffices in most cases. -- Lwq |
Re: Needs help in editing
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Le 24/06/2011 17:42, lewbloch a écrit : > Reflection is an elephant gun for > shooting fleas; simple polymorphism suffices in most cases. I totally aggree. But with this (craps) code and because constructors with different prototype, reflection is unavoidable? - -- Aeris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOBOVFAAoJEK8zQvxDY4P9Kd8H/17/M093d87q+yvPxZF3AhJa JqNGv0IJKzBpLMi0wZpPnIu0zy8yN3G0CXpUIXz0SDN3xjvqmV QwFASVmCzHQorh c2wq9X0ZkXs0kxXLRdvzLp3wO6X0T7WsaT6r++W2CFEnougScs cwiZ0EyWLE21ow 1yeZ8PYnsX/UztIjNLv30Ls6TwhCgkqI2jo2yxU7HsSTNh4m+yxCeu14/+92jIAL qVb8iSfgv5ja2xDTEwtLl+GX89BW0JCTqrEk3w7aj/XSZTrdTmqQ9OdmgUeTXwfZ aJlOk05OVJXhnR/K/QSH45ICbD/Y7i+k6om4FcsAOmVMMsNU72tFsr/VPPCfaK8= =E0hj -----END PGP SIGNATURE----- |
Re: Needs help in editing
Aéris wrote:
> lewbloch a écrit : >> Reflection is an elephant gun for >> shooting fleas; simple polymorphism suffices in most cases. > I totally aggree. > > But with this (craps) code and because constructors with different > prototype, reflection is unavoidable? > Reflection is mostly avoidable. A little light use of 'Class#newInstance()' with package-private builders called by a factory method isn't very risky and avoids the typical mad craziness of looking up 'Method' or 'Constructor' instances. If you're going down that latter route, leave programming to those better equipped for it. If you think heavy use of reflection will fix crappy code, boy are you ever wrong. **** piled on top of **** only smells worse. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg |
Re: Needs help in editing
William Colls wrote:
> I think we are being asked to someone's school assignment. Better he does it > himself. > PLEASE do not top-post. The OP showed us what he's starting with and asked for _help_, not to "" his assignment. (I assume your missing verb is "do".) So let's give him some help, if that's OK with you, O Snarky One? > On 11-06-23 02:25 PM, Eric wrote: >> Hi guys: Can any one please help me how to i [sic] do the changes in my >> program according to the following 4 changes. >> >> 1) Create a Date type birth data instance field in the Employee class, >> not a String Do you know how to declare a variable to be of a certain type? It's done like this: SomeType variable; Do you know where the 'Date' type is in the standard Java API? <http://download.oracle.com/javase/6/docs/api/java/util/package-frame.html> Do you know how to declare instance members? <http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html> >> 2) Enlarge the constructors for the Employee class and subclasses to >> pass the int month, int day, and int year of the birth date as input >> by the user to PayrollSystemTest to the subclass constructor, and then >> to the Employee class constructor Do you know how to pass arguments to a constructor? It's very similar to how to pass arguments to a method. <http://www.oracle.com/technetwork/java/index-jsp-135888.html> <http://www.oracle.com/technetwork/java/prog-140388.html#const> <http://download.oracle.com/javase/tutorial/java/javaOO/constructors.html> <http://download.oracle.com/javase/tutorial/java/javaOO/methods.html> >> 3) Input from the user for the 5 specific employees and then comment >> out the hardcoding for the 4 employees in the original code Do you have your class notes on how to get input? >> 4) Report monthly salary amounts and include the November birthday >> bonus Do you have your class notes on how to present output? <http://download.oracle.com/javase/tutorial/essential/io/index.html> -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg |
Re: Needs help in editing
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Le 26/06/2011 19:30, Lew a écrit : > > Reflection is mostly avoidable. A little light use of > 'Class#newInstance()' with package-private builders called by a factory > method isn't very risky and avoids the typical mad craziness of looking > up 'Method' or 'Constructor' instances. If you're going down that > latter route, leave programming to those better equipped for it. This is what I say… On clean code, Class#newInstance + setter avoid reflection. But on the craps given code, empty constructor is not available, so Class#newInstance is not usable in this case… - -- Aeris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOB40kAAoJEK8zQvxDY4P9tDcIAMPn2uiUEq evh3Ev26yrh7JK SKZsCxfzEtlXmiTjFUItF2XQVZDWChiL+UGpjyrCQhHnyeeL35 hRiWWmdfJyxTEw IPx5VJ44+7Jk2WAmdGh4fsHDYuYETXKKkfqJnZYZFQPDIElfA6 rOBLJgLD+e9Kvj PuALhiKa8mqss483NLBVTBYB1303Ro5LoXpkljKl3tIGhI5h3G ES8A1QxZJEXHha wdrDjtoaAOKa9OfClhZarnEEx07QwxmXE0rAp/OPsUPwtpUgpYgSEB5EopQsNhcA C53q+1/B2DNFR7Q2miQrM7uajd4e0C46Fm3obxUSmfzhz6QZm/fhqaIxCgICfrM= =CiIk -----END PGP SIGNATURE----- |
Re: Needs help in editing
Aéris wrote:
> Lew a écrit : >> Reflection is mostly avoidable. A little light use of >> 'Class#newInstance()' with package-private builders called by a factory >> method isn't very risky and avoids the typical mad craziness of looking >> up 'Method' or 'Constructor' instances. If you're going down that >> latter route, leave programming to those better equipped for it. > > This is what I say… > On clean code, Class#newInstance + setter avoid reflection. > But on the craps given code, empty constructor is not available, so > Class#newInstance is not usable in this case… So you suggest lowering the river instead of raising the bridge? Again, adding **** to a pile of **** just makes it smell worse. Clean up the **** and add clean stuff, i.e., fix the broken design and refactor the code. Duh. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg |
| All times are GMT. The time now is 09:53 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.