{
  "format": "tokbit",
  "version": 1,
  "name": "Cash machine with PIN change",
  "main": [
    {
      "id": "u1",
      "kind": "declare",
      "name": "pin",
      "type": "str",
      "init": "\"0420\""
    },
    {
      "id": "u2",
      "kind": "declare",
      "name": "entered",
      "type": "str"
    },
    {
      "id": "u3",
      "kind": "declare",
      "name": "tries",
      "type": "int",
      "init": "0"
    },
    {
      "id": "u4",
      "kind": "declare",
      "name": "ok",
      "type": "bool",
      "init": "False"
    },
    {
      "id": "u5",
      "kind": "while",
      "condition": "not ok and tries < 3",
      "body": [
        {
          "id": "u6",
          "kind": "input",
          "target": "entered",
          "prompt": "PIN (4 digits):"
        },
        {
          "id": "u7",
          "kind": "assign",
          "target": "tries",
          "value": "tries + 1"
        },
        {
          "id": "u8",
          "kind": "if",
          "condition": "len(entered) == 4 and entered.isdigit()",
          "then": [
            {
              "id": "u9",
              "kind": "if",
              "condition": "entered == pin",
              "then": [
                {
                  "id": "u10",
                  "kind": "assign",
                  "target": "ok",
                  "value": "True"
                }
              ],
              "else": [
                {
                  "id": "u11",
                  "kind": "output",
                  "values": "\"Wrong PIN.\""
                }
              ]
            }
          ],
          "else": [
            {
              "id": "u12",
              "kind": "output",
              "values": "\"The PIN has exactly 4 digits.\""
            }
          ]
        }
      ],
      "comment": "Three tries for the PIN, then the card is blocked."
    },
    {
      "id": "u13",
      "kind": "if",
      "condition": "ok",
      "then": [
        {
          "id": "u14",
          "kind": "declare",
          "name": "balance",
          "type": "int",
          "init": "1000"
        },
        {
          "id": "u15",
          "kind": "declare",
          "name": "option",
          "type": "str",
          "init": "\"\""
        },
        {
          "id": "u16",
          "kind": "while",
          "condition": "option != \"5\"",
          "body": [
            {
              "id": "u17",
              "kind": "output",
              "values": "\"\""
            },
            {
              "id": "u18",
              "kind": "output",
              "values": "\"1 Balance\""
            },
            {
              "id": "u19",
              "kind": "output",
              "values": "\"2 Withdraw\""
            },
            {
              "id": "u20",
              "kind": "output",
              "values": "\"3 Deposit\""
            },
            {
              "id": "u21",
              "kind": "output",
              "values": "\"4 Change PIN\""
            },
            {
              "id": "u22",
              "kind": "output",
              "values": "\"5 Quit\""
            },
            {
              "id": "u23",
              "kind": "input",
              "target": "option",
              "prompt": "Choice:"
            },
            {
              "id": "u24",
              "kind": "if",
              "condition": "option == \"1\"",
              "then": [
                {
                  "id": "u25",
                  "kind": "output",
                  "values": "\"Your balance is\", balance, \"CZK\""
                }
              ],
              "else": [
                {
                  "id": "u26",
                  "kind": "if",
                  "condition": "option == \"2\"",
                  "then": [
                    {
                      "id": "u27",
                      "kind": "declare",
                      "name": "amount",
                      "type": "int"
                    },
                    {
                      "id": "u28",
                      "kind": "input",
                      "target": "amount",
                      "prompt": "How much to withdraw:"
                    },
                    {
                      "id": "u29",
                      "kind": "if",
                      "condition": "amount <= 0",
                      "then": [
                        {
                          "id": "u30",
                          "kind": "output",
                          "values": "\"Enter a positive amount.\""
                        }
                      ],
                      "else": [
                        {
                          "id": "u31",
                          "kind": "if",
                          "condition": "amount > balance",
                          "then": [
                            {
                              "id": "u32",
                              "kind": "output",
                              "values": "\"Not enough money in the account.\""
                            }
                          ],
                          "else": [
                            {
                              "id": "u33",
                              "kind": "assign",
                              "target": "balance",
                              "value": "balance - amount"
                            },
                            {
                              "id": "u34",
                              "kind": "output",
                              "values": "\"Paid out\", amount, \"CZK\""
                            }
                          ],
                          "comment": "You cannot withdraw more than the balance."
                        }
                      ]
                    }
                  ],
                  "else": [
                    {
                      "id": "u35",
                      "kind": "if",
                      "condition": "option == \"3\"",
                      "then": [
                        {
                          "id": "u36",
                          "kind": "declare",
                          "name": "deposit",
                          "type": "int"
                        },
                        {
                          "id": "u37",
                          "kind": "input",
                          "target": "deposit",
                          "prompt": "How much to deposit:"
                        },
                        {
                          "id": "u38",
                          "kind": "if",
                          "condition": "deposit <= 0",
                          "then": [
                            {
                              "id": "u39",
                              "kind": "output",
                              "values": "\"Enter a positive amount.\""
                            }
                          ],
                          "else": [
                            {
                              "id": "u40",
                              "kind": "assign",
                              "target": "balance",
                              "value": "balance + deposit"
                            },
                            {
                              "id": "u41",
                              "kind": "output",
                              "values": "\"Deposited\", deposit, \"CZK\""
                            }
                          ]
                        }
                      ],
                      "else": [
                        {
                          "id": "u42",
                          "kind": "if",
                          "condition": "option == \"4\"",
                          "then": [
                            {
                              "id": "u43",
                              "kind": "declare",
                              "name": "new_pin",
                              "type": "str"
                            },
                            {
                              "id": "u44",
                              "kind": "input",
                              "target": "new_pin",
                              "prompt": "New PIN:"
                            },
                            {
                              "id": "u45",
                              "kind": "if",
                              "condition": "len(new_pin) == 4 and new_pin.isdigit()",
                              "then": [
                                {
                                  "id": "u46",
                                  "kind": "assign",
                                  "target": "pin",
                                  "value": "new_pin"
                                },
                                {
                                  "id": "u47",
                                  "kind": "output",
                                  "values": "\"PIN changed.\""
                                }
                              ],
                              "else": [
                                {
                                  "id": "u48",
                                  "kind": "output",
                                  "values": "\"The PIN has exactly 4 digits.\""
                                }
                              ]
                            }
                          ],
                          "else": [
                            {
                              "id": "u49",
                              "kind": "if",
                              "condition": "option != \"5\"",
                              "then": [
                                {
                                  "id": "u50",
                                  "kind": "output",
                                  "values": "\"Unknown choice.\""
                                }
                              ],
                              "else": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "id": "u51",
          "kind": "output",
          "values": "\"Goodbye.\""
        }
      ],
      "else": [
        {
          "id": "u52",
          "kind": "output",
          "values": "\"The card is blocked.\""
        }
      ]
    }
  ],
  "functions": [],
  "comment": "Solution to the task “Your own PIN”: Add the option “Change PIN”: the program asks for a new PIN and accepts it only if it has 4 digits.",
  "screen": "fit"
}
