Creating and Using a Class - Java Object Oriented Design

Java examples for Object Oriented Design:Class

Description

Creating and Using a Class

Demo Code

public class Main {
  public static void main(String[] args) {
    // instantiate CommissionEmployee object
    Employee employee = new Employee("Ana", "Bates", "222-22-2222", 10000, .06);

    // get commission employee data
    System.out.println("Employee information obtained by get methods:");
    System.out.printf("%n%s %s%n", "First name is", employee.getFirstName());
    System.out.printf("%s %s%n", "Last name is", employee.getLastName());
    System.out.printf("%s %s%n", "Social security number is",
        employee.getSocialSecurityNumber());
    System.out.printf("%s %.2f%n", "Gross sales is", employee.getGrossSales());
    System.out.printf("%s %.2f%n", "Commission rate is",
        employee.getCommissionRate());//  w  w  w  .ja  va  2 s.c o  m

    employee.setGrossSales(5000);
    employee.setCommissionRate(.1);

    System.out.printf("%n%s:%n%n%s%n",
        "Updated employee information obtained by toString", employee);
  }
}

class Employee extends Object {
  private final String firstName;
  private final String lastName;
  private final String socialSecurityNumber;
  private double grossSales; // gross weekly sales
  private double commissionRate; // commission percentage

  // five-argument constructor
  public Employee(String firstName, String lastName,
      String socialSecurityNumber, double grossSales, double commissionRate) {
    // implicit call to Object's default constructor occurs here

    // if grossSales is invalid throw exception
    if (grossSales < 0.0)
      throw new IllegalArgumentException("Gross sales must be >= 0.0");

    // if commissionRate is invalid throw exception
    if (commissionRate <= 0.0 || commissionRate >= 1.0)
      throw new IllegalArgumentException(
          "Commission rate must be > 0.0 and < 1.0");

    this.firstName = firstName;
    this.lastName = lastName;
    this.socialSecurityNumber = socialSecurityNumber;
    this.grossSales = grossSales;
    this.commissionRate = commissionRate;
  }

  // return first name
  public String getFirstName() {
    return firstName;
  }

  // return last name
  public String getLastName() {
    return lastName;
  }

  // return social security number
  public String getSocialSecurityNumber() {
    return socialSecurityNumber;
  }

  // set gross sales amount
  public void setGrossSales(double grossSales) {
    if (grossSales < 0.0)
      throw new IllegalArgumentException("Gross sales must be >= 0.0");

    this.grossSales = grossSales;
  }

  // return gross sales amount
  public double getGrossSales() {
    return grossSales;
  }

  // set commission rate
  public void setCommissionRate(double commissionRate) {
    if (commissionRate <= 0.0 || commissionRate >= 1.0)
      throw new IllegalArgumentException(
          "Commission rate must be > 0.0 and < 1.0");

    this.commissionRate = commissionRate;
  }

  // return commission rate
  public double getCommissionRate() {
    return commissionRate;
  }

  // calculate earnings
  public double earnings() {
    return commissionRate * grossSales;
  }

  // return String representation of CommissionEmployee object
  @Override
  // indicates that this method overrides a superclass method
  public String toString() {
    return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
        "commission employee", firstName, lastName, "social security number",
        socialSecurityNumber, "gross sales", grossSales, "commission rate",
        commissionRate);
  }
}

Result


Related Tutorials