我的印度軟體老師,給的 幾個C# PROGRAMS

來源:互聯網
上載者:User

using System;

namespace Wrox.ProCSharp.OOProg     //The first program
{
   class MainEntryPoint
   {
      static void Main()
      {
         Authenticator myAccess = new Authenticator();
         bool done;
         done = myAccess.ChangePassword("", "MyNewPassword");
         if (done == true)
            Console.WriteLine("Password for myAccess changed");
         else
            Console.WriteLine("Failed to change password for myAccess");
         done = myAccess.ChangePassword("", "AnotherPassword");
         if (done == true)
            Console.WriteLine("Password for myAccess changed");
         else
            Console.WriteLine("Failed to change password for myAccess");

         if (myAccess.IsPasswordCorrect("WhatPassword"))
            Console.WriteLine("Verified myAccess/' password");
         else
            Console.WriteLine("Failed to verify myAccess/' password");
      }

   }

   public class Authenticator
   {
      // implementation as shown earlier

      private string password = "";
  
      public bool IsPasswordCorrect(string tryPassword)
      {
         return (tryPassword == password) ? true : false;
      }

      public bool ChangePassword(string oldPassword, string newPassword)
      {
         if (oldPassword == password)
         {
            password = newPassword;
            return true;
         }
         else
            return false;
      }
   }
}

using System;

namespace Wrox.ProCSharp.OOProg    //The Second program
{
   class MainEntryPoint
   {
      static void Main()
      {
         Authenticator myAccess = new Authenticator();
         bool done;
         done = myAccess.ChangePassword("", "MyNewPassword");
         if (done == true)
            Console.WriteLine("Password for myAccess changed");
         else
            Console.WriteLine("Failed to change password for myAccess");
         done = myAccess.ChangePassword("", "AnotherPassword");
         if (done == true)
            Console.WriteLine("Password for myAccess changed");
         else
            Console.WriteLine("Failed to change password for myAccess");

         if (myAccess.IsPasswordCorrect("WhatPassword"))
            Console.WriteLine("Verified myAccess/' password");
         else
            Console.WriteLine("Failed to verify myAccess/' password");
      }

   }

   public class Authenticator
   {
      // implementation as shown earlier

      private string password = "";
  
      public bool IsPasswordCorrect(string tryPassword)
      {
         return (tryPassword == password) ? true : false;
      }

      public bool ChangePassword(string oldPassword, string newPassword)
      {
         if (oldPassword == password)
         {
            password = newPassword;
            return true;
         }
         else
            return false;
      }
   }
}

namespace Wrox.ProCSharp.OOProg
{
   using System;
   public enum TypeOfCall
   {
      CallToCellPhone, CallToLandline
   }

   public class Customer
   {
      private string name;
      private decimal balance;
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      public decimal Balance
      {
         get
         {
            return balance;
         }
      }
      public void RecordPayment(decimal amountPaid)
      {
         balance -= amountPaid;
      }
      public void RecordCall(TypeOfCall callType, uint nMinutes)
      {
         switch (callType)
         {
            case TypeOfCall.CallToLandline:
               balance += (0.02M * nMinutes);
               break;
            case TypeOfCall.CallToCellPhone:
               balance += (0.30M * nMinutes);
               break;
            default:
               break;
         }
      }
   }
   public class MainEntryPoint
   {
      public static void Main()
      {
         Customer arabel = new Customer();
         arabel.Name = "Arabel Jones";
         Customer mrJones = new Customer();
         mrJones.Name = "Ben Jones";
         arabel.RecordCall(TypeOfCall.CallToLandline, 20);
         arabel.RecordCall(TypeOfCall.CallToCellPhone, 5);
         mrJones.RecordCall(TypeOfCall.CallToLandline, 10);
         Console.WriteLine("{0,-20} owes ${1:F2}", arabel.Name, arabel.Balance);
         Console.WriteLine("{0,-20} owes ${1:F2}", mrJones.Name, mrJones.Balance);
      }
   }
}

namespace Wrox.ProCSharp.OOProg
{
   using System;
   public enum TypeOfCall
   {
      CallToCellPhone, CallToLandline
   }

   public class Customer
   {
      private string name;
      protected decimal balance;
      public string GetFunnyString()
      {
         return "Plain ordinary customer. Kaark!";
      }
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      public decimal Balance
      {
         get
         {
            return balance;
         }
      }
      public void RecordPayment(decimal amountPaid)
      {
         balance -= amountPaid;
      }
      public virtual void RecordCall(TypeOfCall callType, uint nMinutes)
      {
         switch (callType)
         {
            case TypeOfCall.CallToLandline:
               balance += (0.02M * nMinutes);
               break;
            case TypeOfCall.CallToCellPhone:
               balance += (0.30M * nMinutes);
               break;
            default:
               break;
         }
      }
   }
   public class Nevermore60Customer : Customer
   {
      private uint highCostMinutesUsed;
      public new string GetFunnyString()
      {
         return "Nevermore60. Nevermore!";
      }
      public override void RecordCall(TypeOfCall callType, uint nMinutes)
      {
         switch (callType)
         {
            case TypeOfCall.CallToLandline:
               balance += (0.02M * nMinutes);
               break;
            case TypeOfCall.CallToCellPhone:
         uint highCostMinutes, lowCostMinutes;
         uint highCostMinutesToGo =
            (highCostMinutesUsed < 60) ? 60 - highCostMinutesUsed : 0;
         if (nMinutes > highCostMinutesToGo)
         {
            highCostMinutes = highCostMinutesToGo;
            lowCostMinutes = nMinutes - highCostMinutes;
         }
         else
         {
            highCostMinutes = nMinutes;
            lowCostMinutes = 0;
         }
         highCostMinutesUsed += highCostMinutes;
         balance += (0.50M * highCostMinutes + 0.20M *
            lowCostMinutes);
         break;
            default:
               break;
         }
      }
   }
   public class MainEntryPoint
   {
      public static void Main()
      {
         Customer cust1;
         Nevermore60Customer cust2;
         cust1 = new Customer();
         Console.WriteLine("Customer referencing Customer: "
            + cust1.GetFunnyString());
         cust1 = new Nevermore60Customer();
         Console.WriteLine("Customer referencing Nevermore60Customer: "
            + cust1.GetFunnyString());
         cust2 = new Nevermore60Customer();
         Console.WriteLine("Nevermore60Customer referencing: "
            + cust2.GetFunnyString());  
      }
   }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.