首页 > 开发 > 综合 > 正文

几个C# PROGRAMS

2024-07-21 02:17:50
字体:
来源:转载
供稿:网友
 

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());  
      }
   }
}

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表