Advertisement
Guest User

Untitled

a guest
Feb 5th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Person {
  4.  
  5. // Attributes
  6. static private int id;
  7. private String firstName;
  8. private String lastName;
  9.  
  10. static int lastId = 1000;
  11.  
  12. public Person() {
  13. firstName = "";
  14. lastName = "";
  15. id = ++lastId;
  16. }
  17.  
  18. // Constructor
  19. public Person(String firstName, String lastName) {
  20. this.firstName = firstName;
  21. this.lastName = lastName;
  22. id = ++lastId;
  23. }
  24.  
  25. // Getters
  26. public int getId() {
  27. return id;
  28. }
  29.  
  30. public String getFirstName() {
  31. return firstName;
  32. }
  33.  
  34. public String getLastName() {
  35. return lastName;
  36. }
  37.  
  38. public String getFullName() {
  39. return firstName + " " + lastName;
  40. }
  41.  
  42. // Setters
  43.  
  44. public void setFirstName(String firstName) {
  45. this.firstName = firstName;
  46. }
  47.  
  48. public void setLastName(String lastName) {
  49. this.lastName = lastName;
  50. }
  51.  
  52. static void PersonTester(){ //tester method
  53. Person firstPerson = new Person();
  54. firstPerson.setFirstName("Dina");
  55. firstPerson.setLastName("Khatri");
  56. System.out.println(firstPerson.getFullName());
  57. System.out.println(firstPerson.getId() + "\n");
  58.  
  59. Person secondPerson = new Person("Margarita", "Pepperoni");
  60. System.out.println(secondPerson.getFullName());
  61. System.out.println(secondPerson.getId() + "\n");
  62.  
  63. Person thirdPerson = new Person("Carmen", "Sandiego");
  64. System.out.println(thirdPerson.getFullName());
  65. System.out.println(thirdPerson.getId() + "\n");
  66. }
  67.  
  68.  
  69.  
  70. public static void main(String[] args) {
  71. PersonTester();
  72. AddressBook<Person> book = new AddressBook<>();
  73. book.add(new Student("Will",'A'));
  74. book.add(new FulltimeEmplyee(23000));
  75. book.printBook();
  76. }
  77. }
  78.  
  79. class Student extends Person {
  80. ArrayList<Tuple> classes = new ArrayList<Tuple>();
  81. class Tuple<C, G> {
  82. public final C course;
  83. public final G grade;
  84. public Tuple(C course, G grade) {
  85. this.course = course;
  86. this.grade = grade;
  87. }
  88. public String toString() {
  89. return "Course: " + this.course.toString() + ", Grade: " + this.grade.toString();
  90. }
  91. }
  92. public Student(String course, char grade) {
  93. classes.add(new Tuple<String, Character>(course, grade));
  94. }
  95. }
  96.  
  97. class Employee extends Person {
  98. String department;
  99. public void totalHours(HourlyEmployee person) {
  100. //
  101. }
  102. public void totalHours(FulltimeEmplyee person) {
  103. //
  104. }
  105. public void avgHours(HourlyEmployee person) {
  106. //
  107. }
  108. public void avgHours(FulltimeEmplyee person) {
  109. //
  110. }
  111. public void totalWages(HourlyEmployee person) {
  112. //
  113. }
  114. public void totalWages(FulltimeEmplyee person) {
  115. //
  116. }
  117. }
  118. class HourlyEmployee extends Employee {
  119. int hourlyRate;
  120. int hoursPerWeek;
  121.  
  122. public HourlyEmployee(int hourlyRate, int hoursPerWeek) {
  123. this.hourlyRate = hourlyRate;
  124. this.hoursPerWeek = hoursPerWeek;
  125. }
  126. }
  127.  
  128. class FulltimeEmplyee extends Employee {
  129. int salary;
  130. //40 hour workweek
  131. public FulltimeEmplyee(int salary) {
  132. this.salary = salary;
  133. }
  134.  
  135. }
  136.  
  137.  
  138. import java.util.ArrayList;
  139. import java.util.Arrays;
  140.  
  141. public class AddressBook<E extends Person> {
  142. public ArrayList<E> person = new ArrayList<E>();
  143.  
  144. // public AddressBook(E person) {
  145. // this.person.add(person);
  146. // System.out.println("New addressbook, use methods to change it");
  147. // }
  148. public AddressBook() {
  149. System.out.println("New addressbook, use methods to change it");
  150. }
  151.  
  152. public void add(E person) {
  153. if(!this.person.contains(person)) {
  154. this.person.add(person);
  155. } else {
  156. System.out.println("No duplicates");
  157. }
  158. }
  159.  
  160. public void delete(E person) {
  161. if(!this.person.contains(person)) {
  162. System.out.println("Person not found");
  163. } else {
  164. this.person.remove(person);
  165. }
  166. }
  167.  
  168. public void search(E person) {
  169. if(this.person.contains(person)) {
  170. System.out.println("Person found at index of " + this.person.indexOf(person));
  171. } else {
  172. System.out.println("Person not found");
  173. }
  174. }
  175.  
  176. public void printBook() {
  177. for(E obj : person) {
  178. System.out.println(obj.toString());
  179. }
  180. }
  181.  
  182. public static void main(String[] args) {
  183.  
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement