예제 #1
0
파일: Account.cs 프로젝트: yonglehou/PSharp
        private Boolean CheckPin(String p)
        {
            if (!this.IsLocked && p.StringValue().Trim().Equals(this.Pin))
            {
                this.Counter = 0;
                return true;
            }
            else
            {
                this.Counter = this.Counter + 1;

                if (this.Counter >= 3)
                {
                    this.IsLocked = true;
                }

                return false;
            }
        }
예제 #2
0
        public Transaction(MachineId machine, Type callback, Integer accountNumber, Integer amount,
            String pin, Integer type, Double rate)
        {
            this.Machine = machine;
            this.Callback = callback;
            this.AccountNumber = accountNumber;
            this.Amount = amount;
            this.Pin = pin;

            if (type == 3)
            {
                this.Type = 3;
                this.Rate = new Double(0.0);
                return;
            }

            if ((type != 0) && (type != 1))
            {
                throw new Exception("Invalid account type.");
            }

            this.Type = type;
            this.Rate = rate;
        }
예제 #3
0
 public Transfer(MachineId machine, Type callback, Integer from, Integer to,
     Integer amount, string pin)
     : base(machine, callback, to, amount, pin, 3, 0)
 {
     this.From = from;
 }
예제 #4
0
파일: Account.cs 프로젝트: yonglehou/PSharp
        void DoDeposit()
        {
            var transaction = (this.ReceivedEvent as Account.DepositEvent).Transaction;

            if (this.IsLocked)
            {
                this.Send(transaction, new FailureResponse("Account is locked."));
                return;
            }

            if (!this.CheckPin(transaction.GetPin()))
            {
                this.Send(transaction, new FailureResponse("Invalid PIN."));
                return;
            }

            Double amount = new Double(transaction.GetAmount());

            if (amount < 0)
            {
                this.Send(transaction, new FailureResponse("Deposit failed."));
            }
            else
            {
                this.Cents = this.Cents + (int)amount;
                this.Send(transaction, new SuccessResponse("Deposit succeeded."));
            }
        }
예제 #5
0
파일: Account.cs 프로젝트: yonglehou/PSharp
 public Config(String pin, Integer ammount, Double rate)
     : base()
 {
     this.Pin = pin;
     this.Ammount = ammount;
     this.Rate = rate;
 }
예제 #6
0
파일: Account.cs 프로젝트: yonglehou/PSharp
 public Config(String pin, Integer ammount)
     : base()
 {
     this.Pin = pin;
     this.Ammount = ammount;
 }
예제 #7
0
파일: Account.cs 프로젝트: yonglehou/PSharp
        void DoWithdraw()
        {
            var transaction = (this.ReceivedEvent as Account.WithdrawEvent).Transaction;
            var multiples = (this.ReceivedEvent as Account.WithdrawEvent).Multiples;

            if (this.IsLocked)
            {
                this.Send(transaction, new FailureResponse("Account is locked."));
                return;
            }

            if (!this.CheckPin(transaction.GetPin()))
            {
                this.Send(transaction, new FailureResponse("Invalid PIN."));
                return;
            }

            if (!multiples.BooleanValue() && (transaction.GetAmount() % 2000 != 0))
            {
                this.Send(transaction, new FailureResponse("Withdrawals must be in multiples of 20."));
                return;
            }

            if ((transaction.GetAmount() < 0) || (transaction.GetAmount() > this.Cents))
            {
                this.Send(transaction, new FailureResponse("Withdraw failed."));
            }
            else
            {
                this.Cents = this.Cents - transaction.GetAmount();
                this.Send(transaction, new SuccessResponse("Withdraw succeeded."));
            }
        }
예제 #8
0
파일: Account.cs 프로젝트: yonglehou/PSharp
 void DoUnlock()
 {
     var transaction = (this.ReceivedEvent as Account.UnlockEvent).Transaction;
     this.Counter = 0;
     this.IsLocked = false;
     this.Send(transaction, new SuccessResponse("Account successfully unlocked!"));
 }
예제 #9
0
파일: Account.cs 프로젝트: yonglehou/PSharp
        void DoTransferComplete()
        {
            var response = (this.ReceivedEvent as Account.TransferComplete).Response;

            if (response is SuccessResponse)
            {
                this.Cents = this.Cents + this.TransferTransaction.GetAmount();
                this.Send(this.TransferTransaction, new SuccessResponse("Transfer succeeded!"));
            }
            else
            {
                this.Send(this.TransferTransaction, new FailureResponse("Withdraw during transfer failed."));
            }

            this.TransferTransaction = null;
        }
예제 #10
0
파일: Bank.cs 프로젝트: yonglehou/PSharp
        void DoCreateAccount()
        {
            var transaction = (this.ReceivedEvent as CreateAccountEvent).Transaction;

            MachineId newAccount = null;

            Integer accountNumber = this.AccountIds + 1;

            if (transaction.GetAccountType() == 0)
            {
                newAccount = this.CreateMachine(typeof(CheckingAccount), new CheckingAccount.Config(
                    transaction.GetPin(), transaction.GetAmount()));
            }
            else if (transaction.GetAccountType() == 1)
            {
                newAccount = this.CreateMachine(typeof(SavingsAccount), new SavingsAccount.Config(
                    transaction.GetPin(), transaction.GetAmount(), transaction.GetRate()));
            }
            else
            {
                this.Send(transaction, new FailureResponse("Illegal account type."));
                return;
            }

            this.Accounts.Add(accountNumber, newAccount);
            this.AccountIds = this.AccountIds + 1;

            this.Send(transaction, new OpenedResponse(accountNumber));
        }
예제 #11
0
 public OpenedResponse(Integer acctNumber)
     : base("Account opened.")
 {
     this.AccountNumber = acctNumber;
 }