flight booker
zsx / 7guis
program
-- 7GUIs task 3 — Flight Booker. -- -- A combobox picks one-way or return; the return field is -- enabled only for return flights; an ill-formatted date (or a -- return date before the start date) disables Book. Booking -- prints a confirmation. Dates are `dd.mm.yyyy`. -- @cmd — zo run 003_flight_booker.zo load core::zsx::*; -- A date as a comparable `yyyymmdd` int, or `None` when the -- text is not a well-formed `dd.mm.yyyy`. fun parse_date(text: str) -> Option<int> { imu parts: []str = text.split("."); if parts.len != 3 { return Option::None; } mut day: int = 0; mut month: int = 0; mut year: int = 0; match parts[0].parse_int() { Option::Some(value) => day = value, Option::None => return Option::None, }; match parts[1].parse_int() { Option::Some(value) => month = value, Option::None => return Option::None, }; match parts[2].parse_int() { Option::Some(value) => year = value, Option::None => return Option::None, }; if day < 1 || day > 31 || month < 1 || month > 12 || year < 1 { return Option::None; } Option::Some(year * 10000 + month * 100 + day) } -- Book is allowed when the start date parses and — for a -- return flight — the return date parses and is not before -- the start date. fun bookable(mode: str, start_text: str, return_text: str) -> bool { mut start_date: int = 0; match parse_date(start_text) { Option::Some(value) => start_date = value, Option::None => return false, }; if mode == "one-way flight" { return true; } match parse_date(return_text) { Option::Some(value) => return value >= start_date, Option::None => return false, }; false } fun main() { mut mode: str = "one-way flight"; mut start_text: str = "27.03.2014"; mut return_text: str = "27.03.2014"; mut message: str = ""; mut book_disabled: bool = false; mut return_disabled: bool = true; imu booker: </> ::= <> <select value={mode} @change={fn(e) => { mode = e.value; return_disabled = mode == "one-way flight"; book_disabled = !bookable(mode, start_text, return_text); }}> <option>one-way flight</option> <option>return flight</option> </select> <input value={start_text} @input={fn(e) => { start_text = e.value; book_disabled = !bookable(mode, start_text, return_text); }} /> <input value={return_text} disabled={return_disabled} @input={fn(e) => { return_text = e.value; book_disabled = !bookable(mode, start_text, return_text); }} /> <button disabled={book_disabled} @click={fn() => { message = "You have booked a " ++ mode ++ " on " ++ start_text ++ "."; }}>Book</button> <p>{message}</p> </>; #render booker; }
This program declares a combobox C with the options “one-way flight” and “return flight”, two textfields T1 and T2 holding a start and a return date (same date initially), and a Book button B. T2 is enabled iff C is “return flight”. A textfield holding an ill-formatted date disables B; a return date strictly before the start date disables B too. Clicking B displays a message describing the booked flight.
source — 7GUIs (Flight Booker)