From a0b91fd8d70d90544822c368b0f29003a54c83f9 Mon Sep 17 00:00:00 2001 From: Alex4386 Date: Mon, 29 Apr 2024 10:08:21 +0900 Subject: [PATCH] chore: add day17 practice --- .../java/me/alex4386/gachon/sw14462/Main.java | 2 +- .../alex4386/gachon/sw14462/day17/Main.java | 19 +++++++ .../sw14462/day17/ex9_1a/InheritanceDemo.java | 12 +++++ .../gachon/sw14462/day17/ex9_1a/Main.java | 7 +++ .../gachon/sw14462/day17/ex9_1a/Person.java | 35 +++++++++++++ .../gachon/sw14462/day17/ex9_1a/Student.java | 42 +++++++++++++++ .../gachon/sw14462/day17/ex9_1b/Doctor.java | 52 +++++++++++++++++++ .../day17/ex9_1b/DoctorSpeciality.java | 8 +++ .../gachon/sw14462/day17/ex9_1b/Main.java | 45 ++++++++++++++++ .../gachon/sw14462/day17/ex9_1b/Person.java | 35 +++++++++++++ 10 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/InheritanceDemo.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Person.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Student.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Doctor.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/DoctorSpeciality.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Person.java diff --git a/src/main/java/me/alex4386/gachon/sw14462/Main.java b/src/main/java/me/alex4386/gachon/sw14462/Main.java index ab2959e..09ab7b3 100644 --- a/src/main/java/me/alex4386/gachon/sw14462/Main.java +++ b/src/main/java/me/alex4386/gachon/sw14462/Main.java @@ -5,7 +5,7 @@ import java.util.*; public class Main { - public static String currentTarget = "day14"; + public static String currentTarget = "day17"; public static boolean fallbackToLatest = true; public static Map> getAvailableTargetClassNames() { diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day17/Main.java new file mode 100644 index 0000000..6413cec --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/Main.java @@ -0,0 +1,19 @@ +package me.alex4386.gachon.sw14462.day17; + +import me.alex4386.gachon.sw14462.utils.Chainloader; + +public class Main { + public static String chainloadTarget = "ex9_1b"; + + public static void main(String[] args) throws Throwable { + String packageName = Main.class.getPackage().getName(); + String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; + + try { + Chainloader.chainloadTarget(chainLoadTargetClass, args); + } catch (Exception e) { + throw e; + } + } + +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/InheritanceDemo.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/InheritanceDemo.java new file mode 100644 index 0000000..c1d885f --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/InheritanceDemo.java @@ -0,0 +1,12 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1a; + +public class InheritanceDemo +{ + public static void main(String[] args) + { + Student s = new Student(); + s.setName("Warren Peace"); + s.setStudentNumber(1234); + s.writeOutput(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Main.java new file mode 100644 index 0000000..78ffb7f --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1a; + +public class Main { + public static void main(String[] args) { + InheritanceDemo.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Person.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Person.java new file mode 100644 index 0000000..e5657bb --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Person.java @@ -0,0 +1,35 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1a; + +public class Person +{ + private String name; + public Person() + { + name = "No name yet"; + } + + public Person(String initialName) + { + name = initialName; + } + + public void setName(String newName) + { + name = newName; + } + + public String getName() + { + return name; + } + + public void writeOutput() + { + System.out.println("Name: " + name); + } + + public boolean hasSameName(Person otherPerson) + { + return this.name.equalsIgnoreCase(otherPerson.name); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Student.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Student.java new file mode 100644 index 0000000..e68c140 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1a/Student.java @@ -0,0 +1,42 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1a; + + +public class Student extends Person +{ + private int studentNumber; + public Student() + { + super(); + studentNumber = 0;//Indicating no number yet + } + public Student(String initialName, int initialStudentNumber) + { + super(initialName); + studentNumber = initialStudentNumber; + } + public void reset(String newName, int newStudentNumber) + { + setName(newName); + studentNumber = newStudentNumber; + } + public int getStudentNumber() + { + return studentNumber; + } + public void setStudentNumber(int newStudentNumber) + { + studentNumber = newStudentNumber; + } + public void writeOutput() + { + System.out.println("Name: " + getName()); + System.out.println("Student Number: " + studentNumber); + } + public boolean equals(Student otherStudent) + { + return this.hasSameName(otherStudent) && + (this.studentNumber == otherStudent.studentNumber); + } +} + + diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Doctor.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Doctor.java new file mode 100644 index 0000000..ba65f9d --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Doctor.java @@ -0,0 +1,52 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1b; + +public class Doctor extends Person { + private DoctorSpeciality speciality; + private double visitFee = 0.0; + + public Doctor() { + super(); + this.speciality = null; + this.visitFee = 0.0; + } + + public Doctor(String initialName, DoctorSpeciality speciality, double visitFee) { + super(initialName); + this.speciality = speciality; + this.visitFee = visitFee; + } + + public void setSpeciality(DoctorSpeciality speciality) { + if (speciality == null) { + throw new IllegalArgumentException("Speciality cannot be null"); + } + + this.speciality = speciality; + } + + public DoctorSpeciality getSpeciality() { + return this.speciality; + } + + public void setVisitFee(double visitFee) { + if (visitFee < 0) { + throw new IllegalArgumentException("Visit Fee cannot be negative"); + } + + this.visitFee = visitFee; + } + + public double getVisitFee() { + return this.visitFee; + } + + @Override + public String toString() { + return "Doctor: " + this.getName() + ", Speciality: " + this.getSpeciality() + ", Visit Fee: " + this.getVisitFee(); + } + + public boolean equals(Doctor otherDoctor) { + return this.hasSameName(otherDoctor) && this.getSpeciality() == otherDoctor.getSpeciality() && this.getVisitFee() == otherDoctor.getVisitFee(); + } + +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/DoctorSpeciality.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/DoctorSpeciality.java new file mode 100644 index 0000000..c7032e9 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/DoctorSpeciality.java @@ -0,0 +1,8 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1b; + +public enum DoctorSpeciality { + MEDICINE, + SURGERY, + DENTIST, + ORIENTAL; +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Main.java new file mode 100644 index 0000000..2418342 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Main.java @@ -0,0 +1,45 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1b; + +public class Main { + public static void main(String[] args) { + System.out.println("=== Doctor 1 ==="); + Doctor d1 = new Doctor(); + d1.setName("Dr. John Doe"); + d1.setSpeciality(DoctorSpeciality.MEDICINE); + d1.setVisitFee(10000); + System.out.println(d1); + + System.out.println("=== Doctor 2 ==="); + Doctor d2 = new Doctor(); + d2.setName("Dr. Jane Smith"); + d2.setSpeciality(DoctorSpeciality.SURGERY); + d2.setVisitFee(15000); + System.out.println(d2); + + System.out.println("=== Doctor 3 ==="); + Doctor d3 = new Doctor(); + d3.setName("Dr. John Doe"); + d3.setSpeciality(DoctorSpeciality.MEDICINE); + d3.setVisitFee(10000); + System.out.println(d3); + + + System.out.println("=== Compare Doctor 1 & Doctor 2 ==="); + System.out.println("Expected Result: Different"); + if (d1.equals(d2)) { + System.out.println("Doctor 1 and Doctor 2 are the same."); + } else { + System.out.println("Doctor 1 and Doctor 2 are different."); + } + + System.out.println("=== Compare Doctor 1 & Doctor 3 ==="); + System.out.println("Expected Result: Same"); + if (d1.equals(d3)) { + System.out.println("Doctor 1 and Doctor 3 are the same."); + } else { + System.out.println("Doctor 1 and Doctor 3 are different."); + } + + + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Person.java b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Person.java new file mode 100644 index 0000000..52a5dbe --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day17/ex9_1b/Person.java @@ -0,0 +1,35 @@ +package me.alex4386.gachon.sw14462.day17.ex9_1b; + +public class Person +{ + private String name; + public Person() + { + name = "No name yet"; + } + + public Person(String initialName) + { + name = initialName; + } + + public void setName(String newName) + { + name = newName; + } + + public String getName() + { + return name; + } + + public void writeOutput() + { + System.out.println("Name: " + name); + } + + public boolean hasSameName(Person otherPerson) + { + return this.name.equalsIgnoreCase(otherPerson.name); + } +}