Group Group Group Group Group Group Group Group Group

Chapter 10 Challenge 2

Problem:Create a T-shirt structure that has size, color and material options. Provide methods to calculate the cost of a shirt based on its attributes.

Very poor description of the problem …
No rules provided for algorithm to be constructed.

Hi @surepic, apologies for the problem description. I would recommend giving it the challenge a shot and see if what you comes up is close to what the chapter taught you.

struct Tshirt{
let color: String
let size: Int
let material: String
func calculateCost() -> Int{
    var cost = 0
    
    switch self.size{
    case 0...1:
        cost += 1
    
    case 2...3:
    cost += 2
    
    case 3...5:
        cost += 3
        
    default:
        cost += 10
}
    
    switch self.color{
    case "red":
        cost += 5
        
    case "blue":
        cost += 20
        
    default:
        cost += 10
    }
    
    return cost
    
}

}

Hi @surepic, thanks for sharing your challenge! I think you did a great job breaking down the question. The constants were easy to read and your function using a switch statement was awesome for deciding on cost based off the cases. I separated the struct from the function and also added parameters so if you wanted to test your function all you would have to do is input the required parameters to get the cost. I ended up with a $7 Tshirt :smile: .

struct Tshirt {
    
    let color: String
    let size: Int
    let material: String
    
}

func calculateCost(color: String, size: Int, material: String) -> Int {
    var cost = 0
    
    let shirt = Tshirt(color: color, size: size, material: material)
    
    switch shirt.size {
    case 0...1:
        cost += 1
        
    case 2...3:
        cost += 2
        
    case 3...5:
        cost += 3
        
    default:
        cost += 10
    }
    
    switch shirt.color{
    case "red":
        cost += 5
        
    case "blue":
        cost += 20
        
    default:
        cost += 10
    }
    
    return cost
}

calculateCost(color: "red", size: 3, material: "")