Let's practice oop. Write some code in dart for an employee with fields age, name and…
- User
Let's practice oop. Write some code in dart for an employee with fields age, name and job in our cleaning company. Fill a list of employees with some employees
- Assistant
class Employee { int age; String name; String job;
Employee(int age, String name, String job) { this.age = age; this.name = name; this.job = job; }
void printEmployee() { print("age years old and works as a $job."); } }
void main() { List employees = [ Employee(20, "John", "Cleaner"), Employee(28, "Mary", "Supervisor"), Employee(35, "Bob", "Janitor"), Employee(22, "Alice", "Sanitation Engineer"), ];
for (Employee employee in employees) { employee.printEmployee(); } }
- User
Now let's calculate their salary. It is a method taking hours worked. Be aware job is better to be enum and there should be a map job to salary multiple. Take saraly multiples from some statistics
- Assistant
enum Job {cleaner, supervisor, janitor, sanitationEngineer}
const Map<Job, double> jobSalaryMultiples = { Job.cleaner: 8.5, Job.supervisor: 12.5, Job.janitor: 7.5, Job.sanitationEngineer: 10.5, };
class Employee { int age; String name; Job job;
Employee(int age, String name, Job job) { this.age = age; this.name = name; this.job = job; }
double calculateSalary(int hoursWorked){ return hoursWorked * jobSalaryMultiples[this.job]; }
void printEmployee() { print("age years old and works as a ${job.toString().split('.').last}."); } }
void main() { List employees = [ Employee(20, "John", Job.cleaner), Employee(28, "Mary", Job.supervisor), Employee(35, "Bob", Job.janitor), Employee(22, "Alice", Job.sanitationEngineer), ];
for (Employee employee in employees) { employee.printEmployee(); print('Salary: ${employee.calculateSalary(160)}'); // assuming 160 working hours per month } }
- User
Be aware the multiple is applied to minimal wage in us
- Assistant
Okay, I see. Let me update the code to reflect that the salary multiple is applied to the federal minimum wage in the US, which as of 2021 is $7.25 per hour.
enum Job {cleaner, supervisor, janitor, sanitationEngineer}
const Map<Job, double> jobSalaryMultiples = { Job.cleaner: 1.5, Job.supervisor: 2.5, Job.janitor: 1.2, Job.sanitationEngineer: 2.0, };
class Employee { int age; String name; Job job;
Employee(int age, String name, Job job) { this.age = age; this.name = name; this.job = job; }
double calculateSalary(int hoursWorked){ double minimumWage = 7.25; // federal minimum wage in US return hoursWorked * minimumWage * jobSalaryMultiples[this.job]; }
void printEmployee() { print("age years old and works as a ${job.toString().split('.').last}."); } }
void main() { List employees = [ Employee(20, "John", Job.cleaner), Employee(28, "Mary", Job.supervisor), Employee(35, "Bob", Job.janitor), Employee(22, "Alice", Job.sanitationEngineer), ];
for (Employee employee in employees) { employee.printEmployee(); print('Salary: ${employee.calculateSalary(160)}'); // assuming 160 working hours per month } }
- User
Let's now add an enum with the most popular genders for the purpose of addressing them correctly in business msil
- Assistant
Sure, here's an example of how to add an enum with the most popular genders for addressing employees correctly in business email:
enum Gender { male, female, nonBinary, preferNotToSay }
class Employee { int age; String name; Job job; Gender gender;
Employee(int age, String name, Job job, Gender gender) { this.age = age; this.name = name; this.job = job; this.gender = gender; }
double calculateSalary(int hoursWorked){ double minimumWage = 7.25; // federal minimum wage in US return hoursWorked * minimumWage * jobSalaryMultiples[this.job]; }
void printEmployee() { String salutation; switch (this.gender) { case Gender.male: salutation = 'Mr. '; break; case Gender.female: salutation = 'Ms. '; break; case Gender.nonBinary: salutation = ''; break; case Gender.preferNotToSay: salutation = ''; break; } print("name is {job.toString().split('.').last}."); } }
void main() { List employees = [ Employee(20, "John", Job.cleaner, Gender.male), Employee(28, "Mary", Job.supervisor, Gender.female), Employee(35, "Bob", Job.janitor, Gender.male), Employee(22, "Alex", Job.sanitationEngineer, Gender.nonBinary), Employee(45, "Jamie", Job.sanitationEngineer, Gender.preferNotToSay), ];
for (Employee employee in employees) { employee.printEmployee(); print('Salary: ${employee.calculateSalary(160)}'); // assuming 160 working hours per month } }
- User
Now let's modify our salary calculator with the new field added
- Assistant
Sure, here's an updated example of the code with the changes made to reflect the gender field:
enum Gender { male, female, nonBinary, preferNotToSay }
class Employee { int age; String name; Job job; Gender gender;
Employee(int age, String name, Job job, Gender gender) { this.age = age; this.name = name; this.job = job; this.gender = gender; }
double calculateSalary(int hoursWorked){ double minimumWage = 7.25; // federal minimum wage in US return hoursWorked * minimumWage * jobSalaryMultiples[this.job]; }
void printEmployee() { String salutation; String possessivePronoun; switch (this.gender) { case Gender.male: salutation = 'Mr. '; possessivePronoun = 'his'; break; case Gender.female: salutation = 'Ms. '; possessivePronoun = 'her'; break; case Gender.nonBinary: salutation = ''; possessivePronoun = 'their'; break; case Gender.preferNotToSay: salutation = ''; possessivePronoun = ''; break; } print("name is {gender.toString().split('.').last}, and works as a {salutation}Their salary is {possessivePronoun} ${jobSalaryMultiples[this.job]}x the federal minimum wage."); } }
void main() { List employees = [ Employee(20, "John", Job.cleaner, Gender.male), Employee(28, "Mary", Job.supervisor, Gender.female), Employee(35, "Bob", Job.janitor, Gender.male), Employee(22, "Alex", Job.sanitationEngineer, Gender.nonBinary), Employee(45, "Jamie", Job.sanitationEngineer, Gender.preferNotToSay), ];
for (Employee employee in employees) { employee.printEmployee(); } }