Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main
- {
- private static Employee[] staff;
- public static void main(String[] args){
- Employee h1 = new Hourly(100, "f1", "L1", 20.0, 12.50);
- //polymorphic demo of our class design
- // Employee[] staff = new Employee[3];
- // staff[0] = new Hourly(100, "f1", "L1", 20.0, 12.50);
- // staff[1] = new Salary(200, "f2", "L2", 65000);
- // staff[2] = new Hourly(101, "f3", "L3", 40.0, 15.50);
- staff = new Employee[]{
- new Hourly(100, "f1", "L1", 20.0, 12.50),
- new Salary(200, "f2", "L2", 65000),
- new Hourly(101, "f3", "L3", 40.0, 15.50)
- };
- for (Employee e : staff){
- System.out.printf("%s%.2f%n", "Employee paycheck: $", e.paycheck() );
- }
- for (Employee e : staff){
- if ( e instanceof Hourly){
- Hourly h = (Hourly) e;
- System.out.println("Hours worked this week: " + h.getHoursPerWeek() );
- } else if (e instanceof Salary){
- Salary s = (Salary) e;
- System.out.println("Annual compensation: " + s.getAnnualSalary() );
- }
- }
- System.out.printf("h1 present? %s", contains(h1) );
- }
- public static boolean contains(Employee o){
- // for (int i = 0; i <= staff.length - 1; i++){
- // if (staff[i].equals(o) ){
- // return true;
- // }
- // }
- for (Employee e : staff){
- if (e.equals(o) ){
- return true;
- }
- }
- return false;
- }
- }
- import java.util.Objects;
- public abstract class Employee
- {
- protected int sid;
- protected String first;
- protected String last;
- public Employee(int sid, String first, String last){
- setSid(sid);
- setFirst(first);
- setLast(last);
- }
- public final int getSid() {return sid;}
- public final String getFirst() {return first;}
- public final String getLast() {return last;}
- public final void setSid(int sid) {this.sid = sid;}
- public final void setFirst(String first) {this.first = first;}
- public final void setLast(String last) {this.last = last;}
- /* Overwrite the equals method. Use the @Override annotation from here on our when overwriting */
- @Override
- public boolean equals(Object o){
- if ( o == null){ return false;}
- else if (this.getClass() != o.getClass() ) {return false;}
- else if (this == o){return true;}
- else{
- Employee that = (Employee) o;
- return Objects.equals(this.sid, that.sid) && Objects.equals(this.first, that.first) &&
- Objects.equals(this.last, that.last);
- }
- }
- //this method is required to be implemented by all subclasses
- public abstract double paycheck();
- @Override
- public String toString(){
- String str = "";
- //Note the getClass().getCanonicalName() display's objects class name
- str += String.format("%s: [%d][%s, %s]", getClass().getCanonicalName(), sid, first, last);
- return str;
- }
- }
- import java.util.Objects;
- public class Salary extends Employee{
- protected double annualSalary;
- public Salary(int sid, String first, String last, double annualSalary){
- super(sid, first, last);
- setAnnualSalary(annualSalary);
- }
- public final double getAnnualSalary() {return annualSalary;}
- public final void setAnnualSalary(double annualSalary) {this.annualSalary = annualSalary;}
- @Override
- public double paycheck(){
- return annualSalary / 26.0;
- }
- public boolean equals(Salary o){
- String thisName = last + " " + first;
- String oName = o.last + " " + o.first;
- return thisName.equals(oName);
- }
- @Override
- public boolean equals(Object o){
- if ( o == null){ return false;}
- else if (this.getClass() != o.getClass() ) {return false;}
- else if (this == o){return true;}
- else{
- Salary that = (Salary) o;
- return super.equals(o) && Objects.equals(this.annualSalary, that.annualSalary);
- }
- }
- @Override
- public String toString(){
- String str = "";
- str += String.format("%s[$%.0f][Paycheck: $%.2f]", super.toString(), annualSalary, paycheck());
- return str;
- }
- }
- import java.util.Objects;
- public class Hourly extends Employee{
- protected double hoursPerWeek;
- protected double payPerHour;
- public Hourly(){
- this(0, "N/a", "N/a", 0.0, 0.0);
- }
- public Hourly(int sid, String first, String last, double hoursPerWeek, double payPerHour){
- super(sid, first, last);
- setHoursPerWeek(hoursPerWeek);
- setPayPerHour(payPerHour);
- }
- public final double getHoursPerWeek(){ return hoursPerWeek; }
- public final double getPayPerHour(){ return payPerHour; }
- public final void setHoursPerWeek(double hoursPerWeek){ this.hoursPerWeek = hoursPerWeek; }
- public final void setPayPerHour(double payPerHour){ this.payPerHour = payPerHour; }
- @Override
- public double paycheck(){
- return hoursPerWeek * payPerHour;
- }
- public boolean equals(Hourly o){
- String thisName = last + " " + first;
- String oName = o.last + " " + o.first;
- return thisName.equals(oName);
- }
- @Override
- public boolean equals(Object o){
- if ( o == null){ return false;}
- else if (this.getClass() != o.getClass() ) {return false;}
- else if (this == o){return true;}
- else{
- Hourly that = (Hourly) o;
- return super.equals(o) && Objects.equals(this.hoursPerWeek, that.hoursPerWeek) &&
- Objects.equals(this.payPerHour, that.payPerHour);
- }
- }
- @Override
- public String toString(){
- String str = "";
- str += super.toString();
- str += String.format("[%.1f, $%.2f][Paycheck: $%.2f]", hoursPerWeek, payPerHour, paycheck());
- return str;
- }
- }
- public class Student{
- private int sid;
- private String first;
- private String last;
- private String academicYear;
- //Two memory types = stack and heap(free store)
- // cds = designated constructor calls the setter, the others are the convenience ctrs - call designated
- public Student(){
- this(0, "N/A", "N/A", "N/A");
- }
- //Student me = new Student(); me is an object reference of the Student() object
- //me is a part of the static context, the stack whereas the object is part
- //of the dynamic memory, the heap
- public Student(int sid, String first, String last){
- this(sid, first, last, "N/A");
- }
- //me.getFirst(), me is the receiving object
- //designated ctor
- public Student(int sid, String first, String last, String academicYear){
- setSid(sid);
- setFirst(first);
- setLast(last);
- setAcademicYear(academicYear);
- }
- //this is a reference
- public int getSid() { return sid; }
- public String getFirst() { return first; }
- public String getLast() { return last; }
- public String getAcademicYear() { return academicYear; }
- //Value types = primitive --> int, double vs reference --> Class objects
- public void setSid(int sid){
- if (sid > 0){
- this.sid = sid;
- }
- }
- public void setFirst(String first) { this.first = first; }
- public void setLast(String last) { this.last = last; }
- public void setAcademicYear(String academicYear){
- switch (academicYear.toUpperCase() ){
- case "FR":
- case "SO":
- case "JR":
- case "SR":
- this.academicYear = academicYear;
- break;
- }
- }
- public String toString() {
- String str = "";
- str += "(" + sid + ")" + "[" + first + " " + last + "]" + " - " + academicYear;
- return str;
- }
- }
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- public class Main{
- private static final int MAX_STUDENTS = 5;
- private static Student[] students = new Student[MAX_STUDENTS];
- private static int numberOfStudents;
- public static void main(String[] args){
- try{
- loadStudentsFromFile("students.txt");
- writeStudentsToFile("studentsOut.txt");
- displayStudents();
- } catch(FileNotFoundException e){
- System.err.println( e.getMessage() );
- System.exit(1);
- }
- }
- public static void loadStudentsFromFile(String fileName) throws FileNotFoundException{
- try (Scanner fs = new Scanner(new File(fileName)); ){
- String record = "";
- String[] fields = null;;
- int count = 0;
- int sid;
- String first;
- String last;
- String academicYear;
- while (fs.hasNext() ){
- record = fs.nextLine();
- fields = record.split(",");
- //extract the fields into the local vars
- sid = Integer.parseInt(fields[0]);
- first = fields[1];
- last = fields[2];
- academicYear = fields[3];
- students[count++] = new Student(sid, first, last, academicYear);
- }
- }
- }
- public static void writeStudentsToFile(String fileName) throws FileNotFoundException{
- try (PrintWriter pw = new PrintWriter(new File(fileName)); ){
- for (Student s : students){
- pw.println(s);
- }
- }
- }
- public static void displayStudents(){
- for (Student s : students){
- System.out.println(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement