6.9 Class AutoPolicy Case Study: strings in switch Statements

strings can be used in switch expressions, and string literals can be used in case labels. To demonstrate this, we’ll implement an app that meets the following requirements:

  • You’ve been hired by an auto insurance company that serves these northeast states— Connecticut, Maine, Massachusetts, New Hampshire, New Jersey, New York, Pennsylvania, Rhode Island and Vermont. The company would like you to create a program that produces a report indicating for each of their auto insurance policies whether the policy is held in a state with “no-fault” auto insurance—Massachusetts, New Jersey, New York and Pennsylvania.

The app contains two classes—AutoPolicy (Fig. 6.11) and AutoPolicyTest (Fig. 6.12).

Class AutoPolicy

Class AutoPolicy (Fig. 6.11) represents an auto insurance policy. The class contains:

  • auto-implemented int property AccountNumber (line 5) to store the policy’s account number,

  • auto-implemented string property MakeAndModel (line 6) to store the car’s make and model (such as a "Toyota Camry"),

  • auto-implemented string property State (line 7) to store a two-character state abbreviation representing the state in which the policy is held (e.g., "MA" for Massachusetts),

  • a constructor (lines 10–15) that initializes the class’s properties and

  • read-only property IsNoFaultState (lines 18–37) to return a bool value indicating whether the policy is held in a no-fault auto insurance state; note the property name—a common naming convention for a bool property is to begin the name with "Is".

In property IsNoFaultState, the switch expression (line 25) is the string returned by AutoPolicy’s State property. The switch statement compares the switch expression’s value with the case labels (line 27) to determine whether the policy is held in Massachusetts, New Jersey, New York or Pennsylvania (the no-fault states). If there’s a match, then line 28 sets local variable noFaultState to true and the switch statement terminates; otherwise, the default case sets noFaultState to false (line 31). Then IsNoFaultState’s get accessor returns local variable noFaultState’s value.

Fig. 6.11 Class that represents an auto insurance policy.

Alternate View

  1    // Fig. 6.11: AutoPolicy.cs
  2    // Class that represents an auto insurance policy.
  3    class AutoPolicy
  4    {
  5       public int AccountNumber { get; set; } // policy account number
  6       public string MakeAndModel { get; set; } // car that policy applies to
  7       public string State { get; set; } // two-letter state abbreviation
  8
  9         // constructor
 10         public AutoPolicy(int accountNumber, string makeAndModel, string state)
 11         {
 12            AccountNumber = accountNumber;
 13            MakeAndModel = makeAndModel;
 14            State = state;
 15         }
 16
 17         // returns whether the state has no-fault insurance
 18         public bool IsNoFaultState
 19         {
 20            get
 21            {
 22               bool noFaultState;
 23
 24               // determine whether state has no-fault auto insurance
 25               switch (State) // get AutoPolicy object's state abbreviation
 26               {                                                           
 27                  case "MA": case "NJ": case "NY": case "PA":              
 28                     noFaultState = true;                                  
 29                     break;                                                
 30                  default:                                                 
 31                     noFaultState = false;                                 
 32                     break;                                                
 33               }                                                           
 34
 35               return noFaultState;
 36           }
 37       }
 38    }

For simplicity, we do not validate an AutoPolicy’s properties, and we assume that state abbreviations are always two uppercase letters. In addition, a real AutoPolicy class would likely contain many other properties and methods for data such as the account holder’s name, address, birth date, etc. In Exercise 6.28, you’ll be asked to enhance class AutoPolicy by validating its state abbreviation using techniques that you’ll learn in Section 6.11.

Class AutoPolicyTest

Class AutoPolicyTest (Fig. 6.12) creates two AutoPolicy objects (lines 10–11 in Main). Lines 14–15 pass each object to static method policyInNoFaultState (lines 20–28), which uses AutoPolicy methods to determine and display whether the object it receives represents a policy in a no-fault auto insurance state.

Fig. 6.12 Demonstrating strings in switch.

Alternate View

  1    // Fig. 6.12: AutoPolicyTest.cs
  2    // Demonstrating strings in switch.
  3    using System;
  4
  5    class AutoPolicyTest
  6    {
  7       static void Main()
  8       {
  9          // create two AutoPolicy objects
 10          AutoPolicy policy1 = new AutoPolicy(11111111, "Toyota Camry", "NJ");
 11          AutoPolicy policy2 = new AutoPolicy(22222222, "Ford Fusion", "ME");
 12
 13          // display whether each policy is in a no-fault state
 14          PolicyInNoFaultState(policy1);
 15          PolicyInNoFaultState(policy2);
 16       }
 17
 18       // method that displays whether an AutoPolicy
 19       // is in a state with no-fault auto insurance
 20       public static void PolicyInNoFaultState(AutoPolicy policy)
 21       {
 22          Console.WriteLine("The auto policy:");
 23          Console.Write($"Account #: {policy.AccountNumber}; ");
 24          Console.WriteLine($"Car: {policy.MakeAndModel};");
 25          Console.Write($"State {policy.State}; ");
 26          Console.Write($"({policy.IsNoFaultState ? "is": "is not"})");
 27          Console.WriteLine(" a no-fault state
");
 28       }
 29    }

The auto policy:
Account #: 11111111; Car: Toyota Camry;
State NJ is a no-fault state

The auto policy:
Account #: 22222222; Car: Ford Fusion;
State ME is not a no-fault state
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset