Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ASSIGNMENT 4

NAME: Abdullah Ali


ROLL NO: FA19-BCS-146
SECTION: BCS-8C
SUBJECT: TICS
SUBMITTED TO: Sir Taimur Sajjad
DATE: 4 June 2023
Question Statement
Using the concepts of swift, in this assignment you will make a mortgage calculator to help
users keep track of their loan repayment. The users can easily customize prices, set
repayment schedules, and track interest rates. They should also be able to see the loan
amount and estimated amount to pay daily. For example user will take amount from bank
and set total number of months, if user want to pay in more than a year then he/she will
pay 2.5% extra of the total amount else 1.5% of the total. Also user should be able to
check the amount they paid so far so there should be a kind of menu that appears to
them.

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()
}
}

You might also like