Challenge 2 Of Chapter 13 Problem

I am not sure if it is just a mishap from the editorial team, but by no mean can we initialize an object from any of the struct or class below. Anyway the challenge question is this

Your challenge here is to build a set of objects to support a t-shirt store. Decide if each object should be a class or a struct, and why.

TShirt - Represents a shirt style you can buy. Each TShirt has a size, color, price, and an optional image on the front.

User - A registered user of the t-shirt store app. A user has a name, email, and a ShoppingCart (below).

Address - Represents a shipping address, containing the name, street, city, and zip code.

ShoppingCart - Holds a current order, which is composed of an array of TShirt that the User wants to buy, as well as a method to calculate the total cost. Additionally, there is an Address that represents where the order will be shipped.

These are the code


 struct  Tshirt {

 let  size: String

 let color: String

 let price: Double

 var image: String?

}

 class Customer {

 let name: String

 var emailAddress: String

 let shoppingCart: ShoppingCart

 init (name: String, emailAddress: String, shoppingCart: ShoppingCart) {

 self.name = name

self.emailAddress = emailAddress

self.shoppingCart = shoppingCart

}

}

struct Address {

 let name: Customer

var street: String

var city: String

var zipCode: String

}

class ShoppingCart {

var orders: [Tshirt] = []

 let address: Address

init (address: Address) {

self.address = address

}

as you can see the customer object requires the shoppingCart object for initialization, and the shoppingCart object requires the address struct for initialization and then the address struct requires the customer object for its initialization as well. It becomes like a cycle which depends on others to be initialized in order to get themselves created.

@mattjgalloway any suggestions?

This topic was automatically closed after 166 days. New replies are no longer allowed.