Professional Documents
Culture Documents
FA19-BCS-146 Abdullah Ali
FA19-BCS-146 Abdullah Ali
Solution Code
import Foundation
func calculateDailyPayment(loanAmount: Double, totalMonths: Int) -> Double {
var interestRate: Double = 0.015 // Default interest rate for less than a year
if totalMonths > 12 {
interestRate = 0.025 // Interest rate for more than a year
}
let totalInterest: Double = loanAmount * interestRate
let totalAmountToPay: Double = loanAmount + totalInterest
let dailyPayment: Double = totalAmountToPay / Double(totalMonths * 30) // Assuming
30 days in a month
return dailyPayment
}
func trackAmountPaid(loanAmount: Double, totalMonths: Int, currentMonth: Int) -> Double
{
let dailyPayment: Double = calculateDailyPayment(loanAmount: loanAmount,
totalMonths: totalMonths)
let totalDays: Int = currentMonth * 30 // Assuming 30 days in a month
let amountPaid: Double = dailyPayment * Double(totalDays)
return amountPaid
}
func displayMenu() {
print("Mortgage Calculator")
print("-------------------")
print("1. Calculate daily payment")
print("2. Track amount paid")
print("3. Exit")
print()
print("Please enter your choice (1-3):", terminator: " ")
}
func getUserInput() -> String {
let input = readLine() ?? ""
return input
}
// Main
var isRunning = true
while isRunning {
displayMenu()
let choice = getUserInput()
print()
switch choice {
case "1":
print("Enter loan amount:", terminator: " ")
let loanAmountInput = getUserInput()
if let loanAmount = Double(loanAmountInput) {
print("Enter total number of months:", terminator: " ")
let totalMonthsInput = getUserInput()
if let totalMonths = Int(totalMonthsInput) {
let dailyPayment = calculateDailyPayment(loanAmount: loanAmount, totalMonths:
totalMonths)
print("Daily payment: $\(dailyPayment)")
} else {
print("Invalid input for total number of months.")
}
} else {
print("Invalid input for loan amount.")
}
print()
case "2":
print("Enter loan amount:", terminator: " ")
let loanAmountInput = getUserInput()
if let loanAmount = Double(loanAmountInput) {
print("Enter total number of months:", terminator: " ")
let totalMonthsInput = getUserInput()
if let totalMonths = Int(totalMonthsInput) {
print("Enter current month:", terminator: " ")
let currentMonthInput = getUserInput()
if let currentMonth = Int(currentMonthInput) {
let amountPaid = trackAmountPaid(loanAmount: loanAmount, totalMonths:
totalMonths, currentMonth: currentMonth)
print("Amount paid so far: $\(amountPaid)")
} else {
print("Invalid input for current month.")
}
} else {
print("Invalid input for total number of months.")
}
} else {
print("Invalid input for loan amount.")
}
print()
case "3":
isRunning = false
default:
print("Invalid choice. Please enter a number between 1 and 3.")
print()
}
}