In: Computer Science
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver.
Source Code:
Person .java File:
public abstract class Person {
private String name;
/**
* Constructor
* @param name
*/
public Person(String name) {
super();
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Name: " + name;
}
}
**************************************************************************************
Student .java File:
public class Student extends Person {
private int age;
private Tuter tuter;
/**
* @param name
* @param age
* @param tuter
*/
public Student(String name, int age, Tuter tuter) {
super(name);
this.age = age;
this.tuter = tuter;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the tuter
*/
public Tuter getTuter() {
return tuter;
}
/**
* @param tuter the tuter to set
*/
public void setTuter(Tuter tuter) {
this.tuter = tuter;
}
private double getDiscountAmount() {
// Calculate discount rate
double discountRate = 0;
if(this.age < 10) {
discountRate = 20;
}
return tuter.getTotalDue() * discountRate / 100;
}
@Override
public String toString() {
return "Student Name: " + super.getName() + "\nAge: " + age;
}
public void printInvoice() {
// Print student info
System.out.println(this.toString());
// Print tuter information
System.out.println(tuter.toString());
// print the invoices details
System.out.printf("Rate per Hour: $%.2f\n",
tuter.getRatePerHour());
System.out.println("Hours of Instruction: " +
tuter.getHoursOfInstrction());
//Print the discount applied
System.out.printf("Discount applied: $%.2f\n",
this.getDiscountAmount());
System.out.printf("Total Due : $%.2f\n", (tuter.getTotalDue() -
this.getDiscountAmount()));
}
}
**************************************************************************************
Tuter.java File:
public class Tuter extends Person{
private int hoursOfInstrction;
private double ratePerHour;
private final static int MAX_HOURS = 30;
public Tuter(String name) {
super(name);
this.hoursOfInstrction = 0;
this.ratePerHour = 10.00;
}
/**
* @return the hoursOfInstrction
*/
public int getHoursOfInstrction() {
return hoursOfInstrction;
}
/**
* @param hours the hoursOfInstrction to set
*/
public boolean addHoursOfInstrction(int hours) {
if(this.hoursOfInstrction + hours <= MAX_HOURS) {
this.hoursOfInstrction+= hours;
return true;
}else
return false;
}
public double getRatePerHour() {
return ratePerHour;
}
public void setRatePerHour(double ratePerHour) {
this.ratePerHour = ratePerHour;
}
public double getTotalDue() {
return this.getRatePerHour() * this.getHoursOfInstrction();
}
@Override
public String toString() {
return "Tuter Name: " + super.getName();
}
}
**************************************************************************************
Driver .java File
public class Driver {
public static void main(String[] args) {
//Create a Tuter object
Tuter tuter = new Tuter("Sara");
// Create a Student object and assign the tuter
Student student = new Student("Mai Do", 19, tuter);
int hours;
Scanner scan = new Scanner(System.in);
String choice;
do {
System.out.println("Enter the amount of hours:");
hours = scan.nextInt();
//If tuter can teach more
if(tuter.addHoursOfInstrction(hours)) {
System.out.println("Do you want to teach more? yes/no");
choice = scan.next();
}else
break;
}while(choice.equals("yes"));
student.printInvoice();
System.out.println("Another tuter:");
}
}
**************************************************************************************
Solution: HI............ TRY THIS.........
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author VIDYA
*/
abstract class Person {
private String name;
/**
* Constructor
* @param name
*/
public Person(String name) {
super();
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Name: " + name;
}
}
class Student extends Person {
private int age;
private Tuter tuter;
/**
* @param name
* @param age
* @param tuter
*/
public Student(String name, int age, Tuter tuter) {
super(name);
this.age = age;
this.tuter = tuter;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the tuter
*/
public Tuter getTuter() {
return tuter;
}
/**
* @param tuter the tuter to set
*/
public void setTuter(Tuter tuter) {
this.tuter = tuter;
}
private double getDiscountAmount() {
// Calculate discount rate
double discountRate = 0;
if(this.age < 10) {
discountRate = 20;
}
return tuter.getTotalDue() * discountRate / 100;
}
@Override
public String toString() {
return "Student Name: " + super.getName() + "\nAge: " + age;
}
public void printInvoice() {
// Print student info
System.out.println(this.toString());
// Print tuter information
System.out.println(tuter.toString());
// print the invoices details
System.out.printf("Rate per Hour: $%.2f\n",
tuter.getRatePerHour());
System.out.println("Hours of Instruction: " +
tuter.getHoursOfInstrction());
//Print the discount applied
System.out.printf("Discount applied: $%.2f\n",
this.getDiscountAmount());
System.out.printf("Total Due : $%.2f\n", (tuter.getTotalDue() -
this.getDiscountAmount()));
}
}
class Tuter extends Person{
private int hoursOfInstrction;
private double ratePerHour;
final static int MAX_HOURS = 30;
public Tuter(String name) {
super(name);
this.hoursOfInstrction = 0;
this.ratePerHour = 10.00;
}
/**
* @return the hoursOfInstrction
*/
public int getHoursOfInstrction() {
return hoursOfInstrction;
}
/**
* @param hours the hoursOfInstrction to set
*/
public boolean addHoursOfInstrction(int hours) {
if(this.hoursOfInstrction + hours <= MAX_HOURS) {
this.hoursOfInstrction+= hours;
return true;
}else
return false;
}
public double getRatePerHour() {
return ratePerHour;
}
public void setRatePerHour(double ratePerHour) {
this.ratePerHour = ratePerHour;
}
public double getTotalDue() {
return this.getRatePerHour() * this.getHoursOfInstrction();
}
@Override
public String toString() {
return "Tuter Name: " + super.getName();
}
}
public class hours {
public static void main(String[] args) {
//Create a Tuter object
Tuter tuter = new Tuter("Sara");
// Create a Student object and assign the tuter
Student student = new Student("Mai Do", 19, tuter);
int hours;
Scanner scan = new Scanner(System.in);
String choice;
do
{
System.out.println("Enter the amount of hours:");
hours = scan.nextInt();
//If tuter can teach more
if(tuter.addHoursOfInstrction(hours)){
System.out.println("Do you want to teach more? yes/no");
choice = scan.next();
}
else
{
System.out.println("Oooops...!!! Your hours are exceeded");
student.printInvoice();
System.out.println("Another tuter:");
System.exit(0);
}
}while(tuter.addHoursOfInstrction(hours)==false);
}
}
OUTPUT: