在php中对一个对象的属性应用一串命令


applying a string of commands to properties of an object in php

我有一个帐户类,看起来像这样:

class Account {
public $accID;
public $balance;
public function __construct($accNum, $startBalance){
    $this->accID = $accNum;
    $this->balance = $startBalance;
}
public function deposit($amount){
    $this->balance = $balance + $amount;
}
public function withdraw($amount){
    if($amount > $this->balance)
        die("There is not enough money in this account to withdraw");
    $this->balance = $balance + $amount;
}
public function getbalance() {
    return $this->balance;
}
public function getaccID() {
    return $this->accID;
}
public function setaccID($accID){
    $this->accID = $accID;
}
}

这很好,但是我是从处理事务的文本文件输入的。例如:"105 D 200",意思是去账户105和存款200。我已经能够创建多个帐户,并将交易文件拆分为不同的部分。

foreach($getFile as $v) {
list($c, $d, $e) = explode(" ", $v);
$acc[] = $c;
$type[] = $d;
$amount[] = $e;
}

我只是不知道如何使用这些子字符串来与帐户类中的函数一起工作。

任何帮助将是感激的,谢谢你!

首先,在您的提取方法中存在逻辑错误,您正在添加金额而不是从余额中减去它,

您可以创建某种帐户管理器,它将存储所有帐户,您可以从中获取帐户,删除,获取所有帐户…等

然后你可以读取文件并处理它。整个代码应该是这样的:

    foreach($getFile as $v) {
        list($c, $d, $e) = explode(' ', $v);
        $account = AccountManager::manager()->getAccountWithId($c);
        if($d == 'D') {
            $account->deposit($e);
        }
        // add more cases when to withdraw ... etc
    }
    print_r(AccountManager::manager()->getAccounts());
    // This will be a Singleton
    class AccountManager {
        private static $instance;
        private $accounts;
        protected function __construct() {
            $this->accounts = Array();
        }
        // To create a single instance
        public static function manager() {
            if(AccountManager::$manager === null) {
                AccountManager::$manager = new AccountManager();
            }
            return AccountManager::$manager;
        }
        public getAccountWithId($accountId,$autoCreate = true) {
            if(array_key_exists($accountId,$this->accounts)) {
                return $this->accounts[$accountId];
            } else if($autoCreate) {
                // Create a new account with zero balance
                $account = new Account($accountId,0);
                $this->accounts[$accountId] = $account;
                return $account;
            }
            return null;
        }
        public deleteAccountWithId($accountId) {
            if(array_key_exists($accountId,$this->accounts)) {
                unset($this->accounts[$accountId]);
            }
        }
        public getAccounts() {
            return array_values($this->accounts);
        }
    }

    class Account {
        public $accID;
        public $balance;
        public function __construct($accNum, $startBalance){
            $this->accID = $accNum;
            $this->balance = $startBalance;
        }
        public function deposit($amount){
            $this->balance += $amount;
        }
        public function withdraw($amount){
            if($amount > $this->balance) {
                die("There is not enough money in this account to withdraw");
            }

            // Make sure you are substracting the amount and not adding it.
            $this->balance -= $amount;
        }
        public function getbalance() {
            return $this->balance;
        }
        public function getaccID() {
            return $this->accID;
        }
        public function setaccID($accID){
            $this->accID = $accID;
        }
    }