This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public class PaymentProcessor { | |
// Variable Declaration | |
fileprivate var batchSum : Double! | |
private var queue: Queue<Double>! | |
private let BATCH_CAP: Double! | |
private var defaults: UserDefaults! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func shouldProcessBatch() -> Bool{ | |
//1 | |
guard batchSum < BATCH_CAP else { | |
return true | |
} | |
return false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func enqueue(transaction: Double){ | |
if(shouldProcessBatch()){ | |
//1 | |
processBatch() | |
addToBatch(transaction: transaction) | |
}else{ | |
//2 | |
addToBatch(transaction: transaction) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func addToBatch(transaction: Double) { | |
// 1 | |
queue.enqueue(element: transaction) | |
//2 | |
batchSum += transaction | |
if(shouldProcessBatch()){ | |
// 3 | |
processBatch() | |
}else{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func processBatch(){ | |
// 1 | |
for _ in 0..<queue.getCount(){ | |
// 2 | |
batchSum -= queue.peek()! | |
print("deueuing \(queue.dequeue()!)") | |
} | |
print("deueuing \(batchSum!)") | |
// 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func resetBatchSum(){ | |
//1 | |
batchSum = 0.0 | |
//2 | |
defaults.set(batchSum, forKey: "batch_sum") | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var transactionsDataSource1 = [0.32,0.10,0.01,0.02] | |
var processor = PaymentProcessor() | |
for i in 0..<transactionsDataSource1.count { | |
processor.enqueue(transaction: transactionsDataSource1[i]) | |
} | |
let sum = processor.batchSum | |
print(sum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension Double { | |
func format() -> String{ | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .currency | |
let formattedDouble = formatter.string(from: self as NSNumber) | |
return formattedDouble! | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate var batchSum : Double!{ | |
willSet{ | |
if (newValue > 0 && newValue > batchSum){ | |
print("\((newValue - batchSum).format()) enqueud into the batch, the updated batch value is: \(newValue!.format())") | |
} | |
defaults.set(newValue, forKey: "batch_sum") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func processBatch(){ | |
for _ in 0..<queue.getCount(){ | |
batchSum -= queue.peek()! | |
//1 | |
print("deueuing \(queue.dequeue()!.format())") | |
} | |
//2 | |
print("deueuing \(batchSum!.format())") |
OlderNewer