Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

Inside the InterestInfo Class

The other class in this program is the InterestInfo class. The next piece of code shows what I mean by saying that the InterestInfo class uses the InterestController class:

   class InvestmentInfo
   {
     InvestmentController Ic = new InvestmentController();
     . . .
   }

The first class member of the InterestInfo class is an object of type InvestmentController named Ic. Alone, Ic is merely an empty reference, but this code uses the new operator to instantiate an instance of the InvestmentController class, which Ic will refer to.

Every C# program has an entry point named Main(), with a capital M. This is where the program starts off. The following code shows the Main() method for the InterestInfo class:

     static void Main(string[] args)
     {
        InvestmentInfo Ii = new InvestmentInfo();

        Ii.ShowMenu();
     }

This code shows another object instantiation. The class member Ii is a reference to an object of type InvestmentInfo. The next statement shows how to use the members of an object by using the dot operator between the object and member names. This invokes the ShowMenu() method shown in the following code:

     void ShowMenu()
     {
        string choice;

        do 
        {
          Console.WriteLine("Investment Information");
          Console.WriteLine("----------------------\n");

          Console.WriteLine("1 - Initial Estimate");
          Console.WriteLine("2 - Supplemental Estimate");

          Console.WriteLine("Q - Quit\n");
          Console.Write("Your Choice? (1-1 or Q): ");

          choice = Console.ReadLine();

This code shows some string action and console I/O. The string is another C# intrinsic type, but, unlike types such as int or float, a string is a reference type. Console is a console I/O class with the Read(), Write(), ReadLine(), and WriteLine() methods. The *Line() methods read or write a newline character, and the others don't. The previous code prints a menu and reads a users selection, loading it into the choice string field. The next group of code shows what the program does with the user's selection:

          switch(choice.ToUpper()) 
          {
             case "1":
               GetInitialEstimate();
               break;
             case "2":
               GetSupplementalEstimate();
               break;
             case "Q":
               Console.WriteLine("\nGoodbye.\n");
               break;
             defaule:
               Console.WriteLine("\n{0} isn't on the menu. Please Try Again.\a\n", choice);
               break;
          }
        } while (choice.ToUpper() != "Q");
     }

All the logic of the ShowMenu() method is wrapped in a do/while loop, guaranteeing that this code will execute at least one time. C# also has a while(condition){ . . . } loop, just in case you were curious. All conditions for loop statements must yield Boolean true/false values; 0/<not zero> results throw compile-time errors. If you need zero-based logic, try something like (expression == 0).

The string type has numerous methods that enable manipulation of strings. In the example, the ToUpper() string method converts the users selection into an uppercase character. The switch statement takes this value and executes one of the cases, depending on the given value. Cases can be an integral type, enum, or a string. Additionally, every case must have a branching statement before it ends—even the default case, which is called when the others don't apply. The example uses the break statement, but this could have easily been a continue, goto, return, or throws clause. The cases for "1" and "2" call a method within the InterestInfo class. They don't have to be referenced with the objectname.methodname syntax because they already belong to the class. Part of the code from one of the methods is shown next:

     void GetInitialEstimate()
     {
        Console.WriteLine("\n\tGetting Initial Estimate...\n");

        Console.Write("\t\tPrincipal: ");
        Ic.Principal = Convert.ToDecimal(Console.ReadLine());

        . . .

        Console.WriteLine("\n\tEstimated Return on Investment: {0:C}\n", Ic.EstimateReturn());
     }

The first interesting thing about this method is that it's using the properties defined in the InterestController class. Taking the string result from the Console.ReadLine() method, the static Convert class transforms each string to the type defined by the given method. Because the Principal property is a decimal type, its value must be converted by using the ToDecimal() method. The Convert class is the standard way of performing intrinsic type value conversions, and it has all methods for all intrinsic types.

The last statement shows another form of the Console.WriteLine() method that accepts a parameter in curly braces. The zero-based number in the curly braces corresponds to subsequent arguments after the first formatting string parameter of the Console.WriteLine() method. That is, if there were two arguments, there would be two format parameters {0} and {1}. The formatting for the parameter in the example is {0:C}, where 0 is the parameter number, as just discussed, and C is a formatter for currency output. The input parameter is the decimal type output from the EstimateReturn() method of the InterestController class.

  • + Share This
  • 🔖 Save To Your Account