这是一个评估问题,我需要以下方面的帮助: ” 使用包含以下内容的代码创建银行应用程序:
deposit(id:Int, amount:Double)->Void
将金额存入ID。withdraw(id:Int,amount:Double)->Void
退出ID(保持最低余额500)getCustomer(ID:Int)->
返回指定ID的客户,否则返回nil。到目前为止我得到的代码:
import Foundation
/* Editable Code */
class Customer{
var id:Int
var name:String
var balance:Double
init(id:Int,name:String,balance:Double){
self.id = id
self.name = name
if(self.balance <= 0){
self.balance = 500
} else {
self.balance = balance
}
}
}
class Bank: Customer{
// create customer array with name customers
var customers:Array = [
Customer(id:123,name:"Ben",balance:12340.00),
Customer(id:124,name:"Tom",balance:12350.00),
Customer(id:125,name:"Jerry",balance:12389.90)
]
var count:Int = 1
init(count:Int){
self.count = count
}
//implement deposit, withdraw , and getCustomer
func deposit(id:Int,amount:Double) -> Void {
if(balance >= 0){
balance = balance + amount
}
}
func withdraw(id:Int,amount:Double) -> Void {
if amount > 0.0 {
if balance - amount >= 500.0 {
balance = balance - amount
}
}
}
func getCustomer(id:Int) -> Customer {
if(Customer[id] == id){
return Customer
}
}
}
/*End of Editable Code*/
/*Uneditable Code from Here*/
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let custCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let testCaseCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
var bank = Bank(count:custCount)
guard let id = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for _ in 1...testCaseCount {
guard let ops = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let amount = Double((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
switch ops{
case 1 ://deposit
bank.deposit(id:id,amount:amount)
case -1 : //withdraw
bank.withdraw(id:id,amount:amount)
default : break
}
}
let cust:Customer! = bank.getCustomer(id:id)
fileHandle.write(String(bank.customers.count).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
if cust != nil {
fileHandle.write(String(cust.balance).data(using: .utf8)!)
}
我苦苦挣扎的部分是上面的客户数组属性。
需要有关如何创建上述客户数组属性的指针
请让我知道如何修改我的代码以达到上述要求。 非常感谢您提供任何帮助。
更新2)在此处添加了解决方案,因为很少有人以为我没问对问题:
使用了Bradley Mackey提示并致力于此解决方案。 解决方案
import Foundation
/* Editable Code */
class Bank: Customer{
// create customer array with name customers
var customers:[Customer]
/*var customers:Array = [
Customer(id:1,name:"Ben",balance:12340.00),
Customer(id:2,name:"Tom",balance:12350.00),
Customer(id:3,name:"Jerry",balance:12389.90)
]*/
var count:Int = 0
init(count:Int){
NSLog("C: \(count)")
self.count = count
customers = []
var id:Int = 1
var name:String = ""
var balance:Double = 500.00
for i in 1...count {
id = i
customers.append(Customer(id:id,name:name,balance:balance))
}
super.init(id:id,name:name,balance:balance)
}
//implement deposit, withdraw , and getCustomer
func deposit(id:Int,amount:Double) -> Void {
NSLog("Deposit init: \(id) \(amount) \(count)")
if(id > count){
NSLog("\(id) > \(count)")
}
if(id == count){
customers[id-1].balance = customers[id-1].balance + amount
NSLog("\(id) = \(count) balance after deposit is \(customers[id-1].balance)")
}
if(id < count) {
for i in 1...id{
NSLog("for deposit \(i) \(customers[i-1].id)")
if(customers[i-1].id == id){
customers[i-1].balance = customers[i-1].balance + amount
NSLog("\(i)")
}
}
NSLog("\(id) < \(count) balance after deposit is \(customers[id].balance)")
}
NSLog("deposit last \(id) \(amount)")
}
func withdraw(id:Int,amount:Double) -> Void {
NSLog("withdraw: \(id) \(amount)")
if(id <= count){
for i in 1...id{
if(customers[i-1].id == id){
if((customers[i-1].balance - amount) > 500){
customers[i-1].balance = customers[i-1].balance - amount
NSLog("withdraw made \(amount) on \(i-1)")
}
}
}
}
NSLog("withdraw \(id) \(amount)")
}
func getCustomer(id:Int) -> Customer? {
NSLog("getcus \(id)")
/*if(id == 1){
return customers[id]
}*/
if(id > count){
return nil
}
if(id == count){
return customers[id-1]
}
if(id < count){
for i in 0...id {
if(customers[i].id == id){
return customers[i]
}
}
}
NSLog("Bal: \(customers[id].balance)")
return nil
}
}
/*End of Editable Code*/
/*Uneditable Code from Here*/
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let custCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let testCaseCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
var bank = Bank(count:custCount)
guard let id = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for _ in 1...testCaseCount {
guard let ops = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let amount = Double((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
switch ops{
case 1 ://deposit
bank.deposit(id:id,amount:amount)
case -1 : //withdraw
bank.withdraw(id:id,amount:amount)
default : break
}
}
let cust:Customer! = bank.getCustomer(id:id)
fileHandle.write(String(bank.customers.count).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
if cust != nil {
fileHandle.write(String(cust.balance).data(using: .utf8)!)
}
答案 0 :(得分:1)
我将给出一些一般性提示,因为这是一项评估。
客户数组属性引用Bank
上的此属性。
var customers:Array = [
Customer(id:123,name:"Ben",balance:12340.00),
Customer(id:124,name:"Tom",balance:12350.00),
Customer(id:125,name:"Jerry",balance:12389.90)
]
您需要将其设置为一些传递给init
'initializer'的客户,这在Swift中被称为。为此,请使用FOR LOOP确定您需要多少个客户,然后在每个客户中创建一个客户,并将每个客户append
放入数组。例如:
init(count:Int){
self.count = count
// first of all, clear the customers array
customers = []
for i in 1...count {
// CREATE THE CUSTOMER HERE
// ...
// use i as the customer id to create each customer,
// setting their initial balance to 500, then `append` to `customers`
}
}