示例#1
0
        public static CreditCardSystem getInstance()
        {
            if (uniqueInstance == null)
            {
                uniqueInstance = new CreditCardSystem();
            }

            return(uniqueInstance);
        }
示例#2
0
        static void customerPayCreditCard(List <Premium> premiums)
        {
            bool isRunning = true;

            while (isRunning)
            {
                // If no premiums then return function.
                if (premiums.Count == 0)
                {
                    Console.WriteLine("No outstanding premiums!");
                    isRunning = false;
                    break;
                }

                // Display premiums.
                int no = 1;
                foreach (Premium p in premiums)
                {
                    Console.WriteLine("#" + no++);
                    Console.WriteLine("ID: " + p.getPremiumID());
                    Console.WriteLine("Details: " + p.getDetails());
                    Console.WriteLine("Due Date: " + p.getDueDate().ToString());
                    Console.WriteLine("Amount Payable: $" + p.getAmountPayable().ToString(".00"));
                    Console.WriteLine();
                }

                // Get chosen premium from user.
                int premiumNo;
                while (true)
                {
                    Console.Write("Enter premium no: ");
                    premiumNo = Convert.ToInt32(Console.ReadLine());

                    if (premiumNo >= 0 && premiumNo <= premiums.Count)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("\nInvalid Input!");
                    }
                }

                // Retrieve premium.
                Premium premium = premiums[premiumNo - 1];

                // Get credit card info.
                Console.Write("Enter credit card no: ");
                int creditCardNo = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter CCV no: ");
                int ccv = Convert.ToInt32(Console.ReadLine());

                // Process payment
                bool paymentSuccess = CreditCardSystem.getInstance().processPayment(creditCardNo, ccv);

                if (paymentSuccess)
                {
                    Console.WriteLine("Payment successful!");
                    Console.WriteLine("Receipt no: " + premium.getPremiumID());
                    premium.setPaymentType("Credit Card");
                    premium.setDateTimeOfPayment(DateTime.Now);
                }
                else
                {
                    Console.WriteLine("Payment unsuccessful!");
                }

                // Get user option if he wants to continue or return.
                while (true)
                {
                    Console.WriteLine("\n\n[1] Pay Another Outstanding Premium(s)\n[0] Return back");
                    Console.Write("Enter option: ");
                    int option = Convert.ToInt32(Console.ReadLine());
                    if (option == 1) // Pay another outstandin premium
                    {
                        isRunning = true;
                        break;
                    }
                    else if (option == 0) // Return back
                    {
                        isRunning = false;
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Invalid option.");
                    }
                }
            }
        }