在Swift中存储不同类的对象的数组

时间:2019-06-13 05:16:56

标签: arrays swift class

我定义了从Account类继承的Saving和CreditCard类,对于我要定义的最后一个类(Client类),我需要一个列表来存储所有客户的银行帐户作为Saving或CreditCard对象(以及初始化为空列表)。我在制作存储不同类对象的“帐户”列表时遇到麻烦。尝试类似var帐户的操作:[AnyObject]不起作用...

//Custom Exceptions:
enum MyError: Error {
    case transactionLimitExceeded
    case overdraftError
    case creditLimitExceeded
    case deleteError
}

//Account Class:
class Account {
    var account_number:String
    var transaction_limit:Int
    var balance:Int
    //Initializer
    init(_ accNum:String, _ transLim:Int ) {
        self.account_number = accNum
        self.transaction_limit = transLim
        self.balance = 0
    }
    //Functions
    func getAccountNumber() -> String {
        return account_number
    }
    func getBalance() -> Int {
        return balance
    }
}


//Saving Class
class Saving : Account {
    func deposit(_ num:Int) {
        self.balance = self.getBalance() + num
    }
    func withdraw(_ num:Int) throws {
        //raise TransactionLimitExceeded if num is more than transaction_limit
        //raise OverdraftError if balance is less than num
        //otherwise, deduct num from balance
        if num > self.transaction_limit {
            throw MyError.transactionLimitExceeded
        }
        else if self.getBalance() < num {
            throw MyError.overdraftError
        }
        else {
            self.balance = self.getBalance() - num
        }
    }
}


//CreditCard Class
class CreditCard : Account {
    var credit_limit:Int

    init (_ accNum:String, _ transLim:Int, _ credLim:Int) {
        self.credit_limit = credLim
        super.init(accNum, transLim)
    }

    func getCreditLimit() -> Int {
        return self.credit_limit
    }

    func getRemainingAvailableCredit() -> Int {
        //Return credit - balance
        let balance = self.getBalance()
        let credit = self.getCreditLimit()
        return credit - balance
    }

    func deposit(_ num:Int) {
        //set balance to balance minus num
        let balance = self.getBalance()
        self.balance = balance - num
    }

    func withdraw(_ num:Int) throws {
        //Raise transactionLimitExceeded if num is more than transaction limit
        //raise creditLimitExceeded if remaining credit available is less than num (you might want to call the method getRemainingAvailableCredit)
        //Otherwise, add num to balance
        if num > self.transaction_limit {
            throw MyError.transactionLimitExceeded
        }
        else if self.getRemainingAvailableCredit() < num {
            throw MyError.creditLimitExceeded
        }
        else {
            self.balance = self.getBalance() + num
        }
    }   
}

//Client Class
class Client {
    var name:String
    var accounts:[Saving,CreditCard]() //<- ???

    init(_ name:String) {
        self.name = name
        self.accounts = []
    }

    func createSavingAccount(_ accNum:String, _ transLim:Int) {
        //Creates a Saving Account and adds it to the accounts list
        let s = Saving(accNum, transLim)
        accounts += [s]
    }

    func createCreditCardAccount(_ accNum:String, _ transLim:Int, _ credLim:Int) {
        //Creates a Credit Card Account and adds it to the accounts list
        let c = CreditCard(accNum, transLim, credLim)
        accounts += [c]
    }

    func transferBetweenAccounts(_ account1:Account, _ account2:Account, _ num:Int) {
        //Withdraw num from account1 and deposit it to account2
        //Do this by calling the method withdraw from account1 and deposit from account2
        var account1Index = 0
        var account2Index = 0
        for i in 0...accounts.count-1 {
            let account =  accounts[i]
            if account.getAccountNumber() == account1.getAccountNumber() {
                account1Index = i
            }
            else if account.getAccountNumber() == account2.getAccountNumber() {
                account2Index = i
            }
        }
        self.accounts[account1Index].withdraw(num)
        self.accounts[account2Index].deposit(num)
    }

}

2 个答案:

答案 0 :(得分:1)

制作超类的数组。

var accounts = [Account]()

答案 1 :(得分:0)

Account应该是protocol,而Saving & CreditCard应该符合以下要求。另外,货币应始终为Decimal,而不是Int

protocol Account {
    var accountNumber: String { get set }
    var transactionLimit: Decimal { get set }
    var balance: Decimal { get set }
    func deposit(_ amount: Decimal)
    func withdraw(_ amount: Decimal) throws
}


//Saving Class
class Saving: Account {

    var accountNumber: String
    var transactionLimit: Decimal
    var balance: Decimal

    init(_ accountNumber: String, _ transactionLimit: Decimal) {
        self.accountNumber = accountNumber
        self.transactionLimit = transactionLimit
        self.balance = 0
    }

    func deposit(_ amount: Decimal) {
        self.balance = self.balance + amount
    }

    func withdraw(_ amount: Decimal) throws {
        //raise TransactionLimitExceeded if num is more than transaction_limit
        //raise OverdraftError if balance is less than num
        //otherwise, deduct num from balance
        if amount > self.transactionLimit {
            throw MyError.transactionLimitExceeded
        }
        else if self.balance < amount {
            throw MyError.overdraftError
        }
        else {
            self.balance = self.balance - amount
        }
    }
}


//CreditCard Class
class CreditCard : Account {

    var creditLimit: Decimal
    var accountNumber: String
    var transactionLimit: Decimal
    var balance: Decimal

    init(_ accountNumber: String, _ transactionLimit: Decimal, _ creditLimit: Decimal) {
        self.accountNumber = accountNumber
        self.transactionLimit = transactionLimit
        self.balance = 0
        self.creditLimit = creditLimit
    }

    func getRemainingAvailableCredit() -> Decimal {
        return self.creditLimit - self.balance
    }

    func deposit(_ amount: Decimal) {
        //set balance to balance minus amount
        self.balance = self.balance - amount
    }

    func withdraw(_ amount: Decimal) throws {
        //Raise transactionLimitExceeded if num is more than transaction limit
        //raise creditLimitExceeded if remaining credit available is less than num (you might want to call the method getRemainingAvailableCredit)
        //Otherwise, add num to balance
        if amount > self.transactionLimit {
            throw MyError.transactionLimitExceeded
        }
        else if self.getRemainingAvailableCredit() < amount {
            throw MyError.creditLimitExceeded
        }
        else {
            self.balance = self.balance + amount
        }
    }
}

//Client Class
class Client {
    var name: String
    var accounts = [Account]()

    init(_ name: String) {
        self.name = name
    }

    func createSavingAccount(_ accountNumber: String, _ transactionLimit: Decimal) {
        //Creates a Saving Account and adds it to the accounts list
        accounts += [Saving(accountNumber, transactionLimit)]
    }

    func createCreditCardAccount(_ accountNumber: String, _ transactionLimit: Decimal, _ creditLimit: Decimal) {
        //Creates a Credit Card Account and adds it to the accounts list
        accounts += [CreditCard(accountNumber, transactionLimit, creditLimit)]
    }

    func transfer(_ from: Account, _ to: Account, _ amount: Decimal) {
        do {
            try from.withdraw(amount)
        } catch {
            print(error)
        }
        to.deposit(amount)
    }
}