Skip to content

Commit

Permalink
feat: add item check
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamedblue1381 committed Feb 20, 2024
1 parent 16fe70d commit b93506b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions state/item_request_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ func (i *ItemRequestState) InsertCoin() error {
}

func (i *ItemRequestState) ChooseItem(id int) error {
itemExists := false
for _, item := range i.vendingMachine.Items {
if item.ID == id {
itemExists = true
break
}
}

if !itemExists {
return fmt.Errorf("item with ID %d does not exist", id)
}

if i.vendingMachine.Stock > 0 {
i.vendingMachine.SetState(i.vendingMachine.Dispensing)
return nil
Expand Down

0 comments on commit b93506b

Please sign in to comment.