func digit(character:Character) -> Int? { let digitValue: Int? switch character{ case "0": digitValue = 0 case "1": digitValue = 1 case "2": digitValue = 2 case "3": digitValue = 3 case "4": digitValue = 4 case "5": digitValue = 5 case "6": digitValue = 6 case "7": digitValue = 7 case "8": digitValue = 8 case "9": digitValue = 9 default: digitValue = nil } return digitValue } func number(string:String) -> Int { var n = 0 for character in string { let digitValue = digit(character:character) if digitValue != nil { n *= 10 n += digitValue! } } return n } // Prints the specific bill used (if it can be apliied to the current situation) // Returns the number of bills issued func printBill (dollars:Int, billValue:Int) -> Int{ var billCount = 0 var remainingDollars = dollars while (remainingDollars >= billValue) { billCount += 1 remainingDollars -= billValue } var printString = "\(billCount) x $\(billValue) bill" if billCount > 1 { printString += "s" } if billCount > 0 { print(printString) } return billCount } func currencyString(dollars:Int, cents:Int) -> String { let dollarString = "$\(dollars)" var cents = "\(cents)" if cents.count < 2 { cents = "0" + cents } let completeString = dollarString + "." + cents return completeString } func printCent (Cent:Int, centValue:Int) -> Int{ var centCount = 0 var remainingCents = Cent while (remainingCents >= centValue) { centCount += 1 remainingCents -= centValue } var printString = "" switch centValue { case 50: printString = "\(centCount) x half-dollar coin" case 25: printString = "\(centCount) x quarter" case 10: printString = "\(centCount) x dime" case 5: printString = "\(centCount) x nickel" case 1: printString = "\(centCount) x penny" default: break } if centValue == 1 && centCount > 1{ printString = "\(centCount) x pennies" print(printString) } else if centCount > 1 { printString += "s" print(printString) } else if centCount > 0 { print(printString) } return centCount } //This prints the bills used added to the sum of dollars // This returns the number of bills which were used func printBills(dollars:Int) -> Int { var remainingDollars = dollars var totalBillsIssued = 0 let hundredsValue = 100 let hundredsIssued = printBill(dollars:remainingDollars, billValue:hundredsValue) totalBillsIssued += hundredsIssued remainingDollars -= hundredsIssued * hundredsValue let fiftiesValue = 50 let fiftiesIssued = printBill(dollars:remainingDollars, billValue:fiftiesValue) totalBillsIssued += fiftiesIssued remainingDollars -= fiftiesIssued * fiftiesValue let twentiesValue = 20 let twentiesIssued = printBill(dollars:remainingDollars, billValue:twentiesValue) totalBillsIssued += twentiesIssued remainingDollars -= twentiesIssued * twentiesValue let tensValue = 10 let tensIssued = printBill(dollars:remainingDollars, billValue:tensValue) totalBillsIssued += tensIssued remainingDollars -= tensIssued * tensValue let fivesValue = 5 let fivesIssued = printBill(dollars:remainingDollars, billValue:fivesValue) totalBillsIssued += fivesIssued remainingDollars -= fivesIssued * fivesValue let twosValue = 2 let twosIssued = printBill(dollars:remainingDollars, billValue:twosValue) totalBillsIssued += twosIssued remainingDollars -= twosIssued * twosValue let onesValue = 1 let onesIssued = printBill(dollars:remainingDollars, billValue:onesValue) totalBillsIssued += onesIssued remainingDollars -= onesIssued * onesValue return totalBillsIssued } func printCents(cents:Int) -> Int { var remainingCents = cents var totalCoinsIssued = 0 let halfDollarValue = 50 let halfDollarsIssued = printCent(Cent:remainingCents, centValue:halfDollarValue) totalCoinsIssued += halfDollarsIssued remainingCents -= halfDollarsIssued * halfDollarValue let quarterValue = 25 let quarterIssued = printCent(Cent:remainingCents, centValue:quarterValue) totalCoinsIssued += quarterIssued remainingCents -= quarterIssued * quarterValue let dimeValue = 10 let dimeIssued = printCent(Cent:remainingCents, centValue:dimeValue) totalCoinsIssued += dimeIssued remainingCents -= dimeIssued * dimeValue let nickelValue = 5 let nickelIssued = printCent(Cent:remainingCents, centValue:nickelValue) totalCoinsIssued += nickelIssued remainingCents -= nickelIssued * nickelValue let penniesValue = 1 let penniesIssued = printCent(Cent:remainingCents, centValue:penniesValue) totalCoinsIssued += penniesIssued remainingCents -= penniesIssued * penniesValue return totalCoinsIssued } func printDenominations(forSum:String){ let totalcents = number(string:forSum) let dollars = totalcents / 100 let cents = totalcents % dollars let originalSum = "Original sum: \(currencyString(dollars:dollars, cents:cents))" print(originalSum) let billsIssued = printBills(dollars:dollars) let coinsIssued = printCents(cents:cents) print("") print("Bills: \(billsIssued)") print("Coins: \(coinsIssued)") } | switch character{