Created
October 13, 2018 16:56
-
-
Save vakho10/7f9e09eaafcde0c4bf41276b0adee5ce to your computer and use it in GitHub Desktop.
Java code example about enums and inner classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ge.tsu.lab4; | |
public class Employee { | |
String fullName; | |
int age; | |
double salary; | |
Gender gender; | |
Job job; | |
public Employee(String fullName, int age, double salary, Gender gender, Job job) { | |
this.fullName = fullName; | |
this.age = age; | |
this.salary = salary; | |
this.gender = gender; | |
this.job = job; | |
} | |
class Address { | |
String fullName; | |
public Address(String fullName) { | |
this.fullName = fullName; | |
} | |
@Override | |
public String toString() { | |
return "Address{" + | |
"fullName='" + fullName + '\'' + | |
'}'; | |
} | |
} | |
boolean isMySalaryAboveAverage() { | |
return job.averageSalary < salary; | |
} | |
@Override | |
public String toString() { | |
return "Employee{" + | |
"fullName='" + fullName + '\'' + | |
", age=" + age + | |
", salary=" + salary + | |
", gender=" + gender + | |
", job=" + job + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ge.tsu.lab4; | |
public enum Gender { | |
MALE, FEMALE | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ge.tsu.lab4; | |
public enum Job { | |
PROGRAMMER(800.00), WRITER(100.00), DIRECTOR(1_000.00), ACTOR(1_000_000.00); | |
double averageSalary; | |
Job(double averageSalary) { | |
this.averageSalary = averageSalary; | |
} | |
@Override | |
public String toString() { | |
return name() + '(' + averageSalary + ')'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ge.tsu.lab4; | |
public class Main { | |
public static void main(String[] args) { | |
Employee[] employees = { | |
new Employee("Emp1", 18, 1_000, Gender.MALE, Job.WRITER), | |
new Employee("Emp2", 18, 1000, Gender.FEMALE, Job.PROGRAMMER), | |
new Employee("Emp3", 18, 800, Gender.MALE, Job.ACTOR), | |
}; | |
Employee.Address[] addresses = new Employee.Address[employees.length]; | |
int i = 0; | |
for (Employee employee : employees) { | |
addresses[i++] = employee.new Address("address_" + i); | |
} | |
for (int j = 0; j < employees.length; j++) { | |
System.out.print(employees[j] + "; "); | |
System.out.print(employees[j].isMySalaryAboveAverage() + "; "); | |
System.out.println(addresses[j]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment