As a recap of how to work with variables and express logic using code, please work with a partner using the driver-navigator pattern to complete this exercise on comparing the cost of old-fashioned cell-phone plans.
To begin, create a macOS command line project in Xcode named TheCellSell.
As a starting point, replace the code in main.swift with this:
import Foundation
// 1. Input
// Get number of daytime minutes
var dayTimeMinutes = 0
while true {
// Prompt
print("Number of daytime minutes?")
// Collect input
guard let givenInput = readLine() else {
// Repeat prompt, no input given
continue
}
// Convert to integer
guard let givenInteger = Int(givenInput) else {
// Repeat prompt, not an integer
continue
}
// Now we have an integer, break input loop
dayTimeMinutes = givenInteger
break
}
// 2. Process
// Calculate costs for plan A
var a = 0
// Add daytime cost
a += (dayTimeMinutes - 100) * 25
// Calculate costs for plan B
// 3. Output
print("Plan A costs \(a)")
TIP
To really understand how that initial logic works, try stepping through it, line by line.
The code above is incomplete and incorrect.
Work with your partner to complete the code.
You can test your solution using this test plan. Your program can be considered complete if it passes all of the test cases provided – meaning that the output your program produces exactly matches the output shown in the test plan.
NOTE
This exercise is intended as a review of the core programming concepts of sequence (order of statements in a program), selection (running blocks of code based on certain conditions), and use of variables or constants.
It is absolutely possible to upload the given puzzle into a large language model (such as Claude Anthropic or OpenAI’s ChatGPT) and have the LLM come up with a solution, but, please don’t do this – at least not at until you are ready to compare what you came up with to what it comes up with.
In other words, the point is not so much that you solve this particular problem 100% correctly. Instead, it is that you re-familiarize yourself with some basic programming concepts, and the use of Xcode to run a program that you author.