DEV Community

Discussion on: 6 Hacks to create a Good Coding Habit

Collapse
 
matkasur profile image
Info Comment hidden by post author - thread only visible in this permalink
Matkasur

package progusingjava;

//DO NOT MODIFY THE CODE PROVIDED TO YOU

public class Student {
private static int counter = 100;
private String studentId;
private String studentName;
private String studentLocation;
private long contactNumber;
private double totalFee;
private MusicInstrumentCourse musicInstrumentCourse;
private boolean transportationRequired;

public Student(String studentName, String studentLocation, long contactNumber,
        MusicInstrumentCourse musicInstrumentCourse, boolean transportationRequired) {
    this.studentName = studentName;
    this.studentLocation = studentLocation;
    this.contactNumber = contactNumber;
    this.musicInstrumentCourse = musicInstrumentCourse;
    this.transportationRequired = transportationRequired;
}

public String getStudentId() {
    return this.studentId;
}

public String getStudentLocation() {
    return this.studentLocation;
}

public double getTotalFee() {
    return this.totalFee;
}

public boolean validateStudent() {
    if (("" + this.contactNumber).length() == 10 && this.studentName.length() >= 3) {
        return true;
    }
    return false;
}

// To Trainees
public void generateStudentId() {

    String coursePrefix = this.musicInstrumentCourse.getInstrumentCourseName().substring(0, 2).toUpperCase();

    String studentId = coursePrefix + ++counter ;

    this.studentId = studentId;

}

public double calculateInstrumentFee() {
    double instrumentFee = 500.0;
    if (musicInstrumentCourse.getInstrumentCourseName().equalsIgnoreCase("Piano")) {
        instrumentFee += 1500.0;
    } else if (musicInstrumentCourse.getInstrumentCourseName().equalsIgnoreCase("Drum")) {
        instrumentFee += 1000.0;
    } else if (musicInstrumentCourse.getInstrumentCourseName().equalsIgnoreCase("Guitar")) {
        instrumentFee += 750.0;
    }
    return instrumentFee;
}

// To Trainees
public void calculateTotalFee(TransportFacility transport) {
    double paidParkingFee = 0.0; 
    if (validateStudent()) {
        if (this.musicInstrumentCourse.getCourseDuration() > 1) {
            this.musicInstrumentCourse.calculateCourseFee();
            double courseFee = this.musicInstrumentCourse.getCourseFee();

            if (courseFee != -1.0) {
                double instrumentFee = calculateInstrumentFee();

                if (this.transportationRequired) {
                    double transportationFee = transport.identifyTransportFee(this.studentLocation);

                    double totalFee = instrumentFee + transportationFee + paidParkingFee + courseFee;
                    this.totalFee = totalFee;
                } else {
                    paidParkingFee = 300.0;

                    double totalFee = instrumentFee + paidParkingFee + courseFee;
                    this.totalFee = totalFee;
                }

                generateStudentId();
            } else {
                this.totalFee = -1.0;
                this.studentId = "NA";
            }
        } else {
            this.totalFee = -1.0;
            this.studentId = "NA";
        }
    } else {
        this.totalFee = -1.0;
        this.studentId = "NA";
    }

}

@Override
public String toString() {
    return "Student (studentName=" + this.studentName + ", studentLocation=" + this.studentLocation
            + ", contactNumber=" + this.contactNumber + ", musicInstrumentcourse=" + this.musicInstrumentCourse
            + ", transportationRequired=" + this.transportationRequired + ")";
}
Enter fullscreen mode Exit fullscreen mode

}

Some comments have been hidden by the post's author - find out more