0

I have homework in which I should imitate a game in which you have a chest and some objects (weapons, herbs, etc).

I need to create 2 classes - `Chest`(containing array `[Inventory]` and functions to append and remove) and `Inventory`(containing list of objects) and then pass `Inventory` to `Chest` as an array.

Later I need to create sub-classes for `Inventory`, through which I will add or delete objects to/from the `Chest`.

I tried to write some code, but it looks completely wrong even for a beginner like me.

How can I make this work?

class Inventory {
    var plant = ""
    var weapon = ""
    var questObjects = ""
    var pants = ""
}
class Chest {
    var inventory: [Inventory]
}
class Inv2: Inventory {
    var plant2 = "oak"
    func append() {
       inventory.append(contentsOf: plant)
    }
}
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Hmm so `Inventory` might need to contain arrays instead of singular properties - for example `var plants = [String]()` instead of `var plant = ""` – aheze Sep 01 '21 at 21:39
  • I added your suggestion to my code, but still how can I make changes in the Inventory sub class? – Kseniia Piskun Sep 01 '21 at 21:43
  • So each Inventory is a thing like a weapon or a herb or is it a group of things? And what is the purpose of subclassing Inventory? – Joakim Danielson Sep 02 '21 at 06:09
  • Each Inventory contains group of things. Purpose is unknown, at least for me, but so it is said in my homework. My guess, main purpose is to train students into doing different types of classes – Kseniia Piskun Sep 02 '21 at 09:30

1 Answers1

1

Maybe this is what you are looking for. The Inventory holds lots of Items. Each of which is an Object with some quantity.

You can use addItem(_:) to add a new item to the inventory, and useItem(_:) to use one of those items (quantity decreases by 1).

The Chest has its own inventory you can access.

Inv2 inherits everything from Inventory, but has its own addPlant() convenience method.

Inventory:

class Inventory {
    enum Object {
        case plant
        case weapon
        case questObjects
        case pants
    }

    struct Item {
        let object: Object
        var quantity: Int
    }

    private(set) var items: [Item]

    init(items: [Item]) {
        self.items = items
    }

    func addItem(_ item: Item) {
        items.append(item)
    }

    func useItem(_ object: Object) -> Bool {
        guard let index = items.firstIndex(where: { $0.object == object }) else {
            // No item could be removed
            return false
        }
        items[index].quantity -= 1

        if items[index].quantity == 0 {
            items.remove(at: index)
        }

        return true
    }
}

Chest:

class Chest {
    let inventory: Inventory

    init(inventory: Inventory = Inventory(items: [])) {
        self.inventory = inventory
    }
}

Inventory 2:

class Inv2: Inventory {
    func addPlant() {
        addItem(Inventory.Item(object: .plant, quantity: 1))
    }
}
George
  • 25,988
  • 10
  • 79
  • 133
  • Wow! yes, it is The answer! But, can you, please, explain what is happening in this part of code? guard let index = items.firstIndex(where: { $0.object == object }) else { // No item could be removed return false } $0 is like a placeholder? .firstIndex refers to what? and what is guard let? Thank you so so much! – Kseniia Piskun Sep 02 '21 at 09:34
  • @KseniiaPiskun Sure. [`firstIndex(where:)`](https://developer.apple.com/documentation/swift/array/2994722-firstindex) searches for the first item which meets the condition, then returns the index if it exists, or `nil`. The `$0` is the shorthand instead of providing the variable, like `... where: { item in item.object == object }`. See [here](https://stackoverflow.com/q/36144322/9607863) for what $0 means in more detail. Since `firstIndex` may return `nil` if no item matches the condition (the item isn't in the inventory), then we `return false` to indicate no item was used. [1/2] – George Sep 02 '21 at 09:42
  • [Here](https://www.hackingwithswift.com/sixty/10/3/unwrapping-with-guard) is how to use `guard let`. [2/2] – George Sep 02 '21 at 09:43
  • Thank you! Now it all seems more understandable and picture starts to clarify! Thanks again! – Kseniia Piskun Sep 02 '21 at 10:18