Skip to content

Commit

Permalink
chore: day18 exercise - partial
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed May 1, 2024
1 parent a0b91fd commit c6ac27d
Show file tree
Hide file tree
Showing 31 changed files with 760 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/alex4386/gachon/sw14462/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.*;

public class Main {
public static String currentTarget = "day17";
public static String currentTarget = "day18";
public static boolean fallbackToLatest = true;

public static Map<String, Class<?>> getAvailableTargetClassNames() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/alex4386/gachon/sw14462/day17/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ public static void main(String[] args) throws Throwable {
throw e;
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alex4386.gachon.sw14462.day18;

import me.alex4386.gachon.sw14462.utils.Chainloader;

public class Main {
public static String chainloadTarget = "ex9_4";

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day18.ex9_2a;

public class Main {
public static void main(String[] args) {
PolymorphismDemo.main(args);
}
}
35 changes: 35 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/ex9_2a/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.alex4386.gachon.sw14462.day18.ex9_2a;

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alex4386.gachon.sw14462.day18.ex9_2a;

public class PolymorphismDemo
{
public static void main(String[] args)
{
Person[] people = new Person[4];
people[0] = new Undergraduate("Cotty, Manny", 4910, 1);
people[1] = new Undergraduate("Kick, Anita", 9931, 2);
people[2] = new Student("DeBanque, Robin", 8812);
people[3] = new Undergraduate("Bugg, June", 9901, 4);
for (Person p : people)
{
p.writeOutput();
System.out.println();
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/ex9_2a/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.alex4386.gachon.sw14462.day18.ex9_2a;

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);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.alex4386.gachon.sw14462.day18.ex9_2a;


public class Undergraduate extends Student
{
private int level; //1 for freshman, 2 for sophomore //3 for junior, or 4 for senior.
public Undergraduate()
{
super();
level = 1;
}
public Undergraduate(String initialName,
int initialStudentNumber, int initialLevel)
{
super(initialName, initialStudentNumber); setLevel(initialLevel); //checks 1 <= initialLevel <= 4
}
public void reset(String newName, int newStudentNumber,
int newLevel)
{
reset(newName, newStudentNumber); //Student's reset setLevel(newLevel); //Checks 1 <= newLevel <= 4
}
public int getLevel()
{
return level;
}
public void setLevel(int newLevel)
{
if ((1 <= newLevel) && (newLevel <= 4))
level = newLevel;
else
{
System.out.println("Illegal level!");
System.exit(0);
} }
public void writeOutput()
{
super.writeOutput();
System.out.println("StudentLevel: " + level);
}
public boolean equals(Undergraduate otherUndergraduate)
{
return equals((Student)otherUndergraduate) &&
(this.level == otherUndergraduate.level);
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/ex9_2b/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.alex4386.gachon.sw14462.day18.ex9_2b;

public class Circle extends Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}

public String toString() {
return "Circle: radius=" + radius;
}

public boolean equals(Circle obj) {
return this.radius == obj.radius;
}
}
17 changes: 17 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/ex9_2b/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.alex4386.gachon.sw14462.day18.ex9_2b;

public class Main {
public static void main(String[] args) {
Shape[] shapes = new Shape[4];
shapes[0] = new Rectangle(10, 20);
shapes[1] = new Circle(10);
shapes[2] = new RightTriangle(10, 20);
shapes[3] = new Shape();

for (Shape s : shapes) {
System.out.println(s);
System.out.println("Area: " + s.getArea());
System.out.println();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day18.ex9_2b;

public class Rectangle extends Shape {
private double width;
private double height;

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
public double getArea() {
return width * height;
}

@Override
public String toString() {
return "Rectangle: width=" + width + ", height=" + height;
}

public boolean equals(Rectangle obj) {
return this.width == obj.width && this.height == obj.height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day18.ex9_2b;

public class RightTriangle extends Shape {
private double width;
private double height;

public RightTriangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
public double getArea() {
return width * height / 2;
}

@Override
public String toString() {
return "RightTriangle: width=" + width + ", height=" + height;
}

public boolean equals(RightTriangle obj) {
return this.width == obj.width && this.height == obj.height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day18.ex9_2b;

public class Shape {
public double getArea() {
return 0.0;
}
}
43 changes: 43 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/ex9_3/Billing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.alex4386.gachon.sw14462.day18.ex9_3;

public class Billing {
private Patient patient;
private Doctor doctor;

private int visitCount;

public Billing(Patient patient, Doctor doctor) {
this.patient = patient;
this.doctor = doctor;

this.visitCount = 0;
}

public Patient getPatient() {
return this.patient;
}

public void setPatient(Patient patient) {
this.patient = patient;
}

public Doctor getDoctor() {
return this.doctor;
}

public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}

public void visit() {
this.visitCount++;
}

public int getVisitCount() {
return this.visitCount;
}

public double getIncome() {
return this.visitCount * this.doctor.getVisitFee();
}
}
54 changes: 54 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day18/ex9_3/Doctor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.alex4386.gachon.sw14462.day18.ex9_3;

import me.alex4386.gachon.sw14462.day17.ex9_1b.Person;

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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.alex4386.gachon.sw14462.day18.ex9_3;

public enum DoctorSpeciality {
MEDICINE,
SURGERY,
DENTIST,
ORIENTAL;
}
Loading

0 comments on commit c6ac27d

Please sign in to comment.