Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. public class Main
  2. {
  3. private static Employee[] staff;
  4.  
  5. public static void main(String[] args){
  6. Employee h1 = new Hourly(100, "f1", "L1", 20.0, 12.50);
  7. //polymorphic demo of our class design
  8. // Employee[] staff = new Employee[3];
  9.  
  10. // staff[0] = new Hourly(100, "f1", "L1", 20.0, 12.50);
  11. // staff[1] = new Salary(200, "f2", "L2", 65000);
  12. // staff[2] = new Hourly(101, "f3", "L3", 40.0, 15.50);
  13.  
  14. staff = new Employee[]{
  15. new Hourly(100, "f1", "L1", 20.0, 12.50),
  16. new Salary(200, "f2", "L2", 65000),
  17. new Hourly(101, "f3", "L3", 40.0, 15.50)
  18. };
  19.  
  20. for (Employee e : staff){
  21. System.out.printf("%s%.2f%n", "Employee paycheck: $", e.paycheck() );
  22. }
  23.  
  24. for (Employee e : staff){
  25. if ( e instanceof Hourly){
  26. Hourly h = (Hourly) e;
  27. System.out.println("Hours worked this week: " + h.getHoursPerWeek() );
  28. } else if (e instanceof Salary){
  29. Salary s = (Salary) e;
  30. System.out.println("Annual compensation: " + s.getAnnualSalary() );
  31. }
  32. }
  33.  
  34. System.out.printf("h1 present? %s", contains(h1) );
  35. }
  36.  
  37. public static boolean contains(Employee o){
  38. // for (int i = 0; i <= staff.length - 1; i++){
  39. // if (staff[i].equals(o) ){
  40. // return true;
  41. // }
  42. // }
  43.  
  44. for (Employee e : staff){
  45. if (e.equals(o) ){
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. }
  52.  
  53.  
  54.  
  55. import java.util.Objects;
  56.  
  57. public abstract class Employee
  58. {
  59. protected int sid;
  60. protected String first;
  61. protected String last;
  62.  
  63. public Employee(int sid, String first, String last){
  64. setSid(sid);
  65. setFirst(first);
  66. setLast(last);
  67. }
  68.  
  69. public final int getSid() {return sid;}
  70. public final String getFirst() {return first;}
  71. public final String getLast() {return last;}
  72.  
  73. public final void setSid(int sid) {this.sid = sid;}
  74. public final void setFirst(String first) {this.first = first;}
  75. public final void setLast(String last) {this.last = last;}
  76.  
  77. /* Overwrite the equals method. Use the @Override annotation from here on our when overwriting */
  78. @Override
  79. public boolean equals(Object o){
  80. if ( o == null){ return false;}
  81. else if (this.getClass() != o.getClass() ) {return false;}
  82. else if (this == o){return true;}
  83. else{
  84. Employee that = (Employee) o;
  85. return Objects.equals(this.sid, that.sid) && Objects.equals(this.first, that.first) &&
  86. Objects.equals(this.last, that.last);
  87. }
  88. }
  89.  
  90. //this method is required to be implemented by all subclasses
  91. public abstract double paycheck();
  92.  
  93. @Override
  94. public String toString(){
  95. String str = "";
  96.  
  97. //Note the getClass().getCanonicalName() display's objects class name
  98. str += String.format("%s: [%d][%s, %s]", getClass().getCanonicalName(), sid, first, last);
  99. return str;
  100. }
  101. }
  102.  
  103.  
  104.  
  105. import java.util.Objects;
  106.  
  107. public class Salary extends Employee{
  108. protected double annualSalary;
  109.  
  110. public Salary(int sid, String first, String last, double annualSalary){
  111. super(sid, first, last);
  112. setAnnualSalary(annualSalary);
  113. }
  114.  
  115. public final double getAnnualSalary() {return annualSalary;}
  116. public final void setAnnualSalary(double annualSalary) {this.annualSalary = annualSalary;}
  117.  
  118. @Override
  119. public double paycheck(){
  120. return annualSalary / 26.0;
  121. }
  122.  
  123. public boolean equals(Salary o){
  124. String thisName = last + " " + first;
  125. String oName = o.last + " " + o.first;
  126.  
  127. return thisName.equals(oName);
  128. }
  129.  
  130. @Override
  131. public boolean equals(Object o){
  132. if ( o == null){ return false;}
  133. else if (this.getClass() != o.getClass() ) {return false;}
  134. else if (this == o){return true;}
  135. else{
  136. Salary that = (Salary) o;
  137. return super.equals(o) && Objects.equals(this.annualSalary, that.annualSalary);
  138. }
  139. }
  140.  
  141. @Override
  142. public String toString(){
  143. String str = "";
  144.  
  145. str += String.format("%s[$%.0f][Paycheck: $%.2f]", super.toString(), annualSalary, paycheck());
  146. return str;
  147. }
  148. }
  149.  
  150.  
  151.  
  152. import java.util.Objects;
  153.  
  154. public class Hourly extends Employee{
  155. protected double hoursPerWeek;
  156. protected double payPerHour;
  157.  
  158. public Hourly(){
  159. this(0, "N/a", "N/a", 0.0, 0.0);
  160. }
  161. public Hourly(int sid, String first, String last, double hoursPerWeek, double payPerHour){
  162. super(sid, first, last);
  163. setHoursPerWeek(hoursPerWeek);
  164. setPayPerHour(payPerHour);
  165. }
  166.  
  167. public final double getHoursPerWeek(){ return hoursPerWeek; }
  168. public final double getPayPerHour(){ return payPerHour; }
  169.  
  170. public final void setHoursPerWeek(double hoursPerWeek){ this.hoursPerWeek = hoursPerWeek; }
  171. public final void setPayPerHour(double payPerHour){ this.payPerHour = payPerHour; }
  172.  
  173. @Override
  174. public double paycheck(){
  175. return hoursPerWeek * payPerHour;
  176. }
  177.  
  178. public boolean equals(Hourly o){
  179. String thisName = last + " " + first;
  180. String oName = o.last + " " + o.first;
  181.  
  182. return thisName.equals(oName);
  183. }
  184.  
  185. @Override
  186. public boolean equals(Object o){
  187. if ( o == null){ return false;}
  188. else if (this.getClass() != o.getClass() ) {return false;}
  189. else if (this == o){return true;}
  190. else{
  191. Hourly that = (Hourly) o;
  192. return super.equals(o) && Objects.equals(this.hoursPerWeek, that.hoursPerWeek) &&
  193. Objects.equals(this.payPerHour, that.payPerHour);
  194. }
  195. }
  196.  
  197. @Override
  198. public String toString(){
  199. String str = "";
  200.  
  201. str += super.toString();
  202. str += String.format("[%.1f, $%.2f][Paycheck: $%.2f]", hoursPerWeek, payPerHour, paycheck());
  203. return str;
  204. }
  205. }
  206.  
  207.  
  208.  
  209. public class Student{
  210.  
  211. private int sid;
  212. private String first;
  213. private String last;
  214. private String academicYear;
  215. //Two memory types = stack and heap(free store)
  216. // cds = designated constructor calls the setter, the others are the convenience ctrs - call designated
  217. public Student(){
  218. this(0, "N/A", "N/A", "N/A");
  219. }
  220. //Student me = new Student(); me is an object reference of the Student() object
  221. //me is a part of the static context, the stack whereas the object is part
  222. //of the dynamic memory, the heap
  223. public Student(int sid, String first, String last){
  224. this(sid, first, last, "N/A");
  225. }
  226. //me.getFirst(), me is the receiving object
  227. //designated ctor
  228. public Student(int sid, String first, String last, String academicYear){
  229. setSid(sid);
  230. setFirst(first);
  231. setLast(last);
  232. setAcademicYear(academicYear);
  233. }
  234. //this is a reference
  235. public int getSid() { return sid; }
  236. public String getFirst() { return first; }
  237. public String getLast() { return last; }
  238. public String getAcademicYear() { return academicYear; }
  239. //Value types = primitive --> int, double vs reference --> Class objects
  240. public void setSid(int sid){
  241. if (sid > 0){
  242. this.sid = sid;
  243. }
  244. }
  245. public void setFirst(String first) { this.first = first; }
  246. public void setLast(String last) { this.last = last; }
  247. public void setAcademicYear(String academicYear){
  248. switch (academicYear.toUpperCase() ){
  249. case "FR":
  250. case "SO":
  251. case "JR":
  252. case "SR":
  253. this.academicYear = academicYear;
  254. break;
  255. }
  256. }
  257.  
  258. public String toString() {
  259. String str = "";
  260.  
  261. str += "(" + sid + ")" + "[" + first + " " + last + "]" + " - " + academicYear;
  262. return str;
  263. }
  264. }
  265.  
  266.  
  267.  
  268. import java.util.Scanner;
  269. import java.io.File;
  270. import java.io.FileNotFoundException;
  271. import java.io.PrintWriter;
  272.  
  273. public class Main{
  274. private static final int MAX_STUDENTS = 5;
  275. private static Student[] students = new Student[MAX_STUDENTS];
  276. private static int numberOfStudents;
  277.  
  278. public static void main(String[] args){
  279. try{
  280. loadStudentsFromFile("students.txt");
  281. writeStudentsToFile("studentsOut.txt");
  282. displayStudents();
  283.  
  284. } catch(FileNotFoundException e){
  285. System.err.println( e.getMessage() );
  286. System.exit(1);
  287. }
  288. }
  289.  
  290. public static void loadStudentsFromFile(String fileName) throws FileNotFoundException{
  291. try (Scanner fs = new Scanner(new File(fileName)); ){
  292. String record = "";
  293. String[] fields = null;;
  294. int count = 0;
  295.  
  296. int sid;
  297. String first;
  298. String last;
  299. String academicYear;
  300.  
  301. while (fs.hasNext() ){
  302. record = fs.nextLine();
  303. fields = record.split(",");
  304.  
  305. //extract the fields into the local vars
  306. sid = Integer.parseInt(fields[0]);
  307. first = fields[1];
  308. last = fields[2];
  309. academicYear = fields[3];
  310.  
  311. students[count++] = new Student(sid, first, last, academicYear);
  312. }
  313. }
  314. }
  315.  
  316. public static void writeStudentsToFile(String fileName) throws FileNotFoundException{
  317. try (PrintWriter pw = new PrintWriter(new File(fileName)); ){
  318. for (Student s : students){
  319. pw.println(s);
  320. }
  321. }
  322. }
  323.  
  324. public static void displayStudents(){
  325. for (Student s : students){
  326. System.out.println(s);
  327. }
  328. }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement