Demonstrating Strings in switch. - Java Language Basics

Java examples for Language Basics:switch

Description

Demonstrating Strings in switch.

Demo Code

public class Main {
  public static void main(String[] args) {
    // create two AutoPolicy objects
    Car policy1 = new Car(1, "Toyota", "NJ");
    Car policy2 = new Car(2, "Ford", "ME");
    // display whether each policy is in a no-fault state
    policyInNoFaultState(policy1);/*from w w  w.  j  av  a2s .co  m*/
    policyInNoFaultState(policy2);
  }

  // method that displays whether an AutoPolicy
  // is in a state with no-fault auto insurance
  public static void policyInNoFaultState(Car policy) {
    System.out.println("The auto policy:");
    System.out.printf(
        "Account #: %d; Car: %s;%nState %s %s a no-fault state%n%n",
        policy.getAccountNumber(), policy.getMakeAndModel(), policy.getState(),
        (policy.isNoFaultState() ? "is" : "is not"));
  }
}

class Car {
  private int accountNumber; // policy account number
  private String makeAndModel; // car that the policy applies to
  private String state; // two-letter state abbreviation

  // constructor
  public Car(int accountNumber, String makeAndModel, String state) {
    this.accountNumber = accountNumber;
    this.makeAndModel = makeAndModel;
    this.state = state;
  }

  // sets the accountNumber
  public void setAccountNumber(int accountNumber) {
    this.accountNumber = accountNumber;
  }

  // returns the accountNumber
  public int getAccountNumber() {
    return accountNumber;
  }

  // sets the makeAndModel
  public void setMakeAndModel(String makeAndModel) {
    this.makeAndModel = makeAndModel;
  }

  // returns the makeAndModel
  public String getMakeAndModel() {
    return makeAndModel;
  }

  // sets the state
  public void setState(String state) {
    this.state = state;
  }

  // returns the state
  public String getState() {
    return state;
  }

  // predicate method returns whether the state has no-fault insurance
  public boolean isNoFaultState() {
    boolean noFaultState;

    // determine whether state has no-fault auto insurance
    switch (getState()) {
    case "MA":
    case "NJ":
    case "NY":
    case "PA":
      noFaultState = true;
      break;
    default:
      noFaultState = false;
      break;
    }

    return noFaultState;
  }
}

Result


Related Tutorials