Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Foundation
- // Definisco gli utenti
- struct User {
- var name: String
- var surname: String
- var email: String
- var password: String
- }
- // Ne creo una loro collezione
- var userList: [User] = []
- // Creo una funzione che aggiunge nuovi utenti alla lista se non già registrati
- func register(name: String, surname: String, email: String, password: String) {
- for user in userList {
- if user.email == email {
- print("Email already registered!")
- return
- }
- }
- let thisUser = User(name: name, surname: surname, email: email, password: password)
- userList.append(thisUser)
- print("Registration completed!")
- }
- // Creo una funzione per fare il login
- func login(email: String, password: String) {
- for user in userList {
- if user.email == email {
- if user.password == password {
- print("Login successful!")
- return
- } else {
- print("Wrong password!")
- return
- }
- }
- }
- print("User not registered!")
- }
- // Definisco i turni per gli uffici (time è di tipo "hh:mm")
- struct turn {
- var time: String
- var isBooked: Bool
- var queueType: String
- }
- // Definisco gli uffici
- struct Office {
- var ID: Int
- var name: String
- var address: String
- var coordinates: (Float, Float)
- var timeTable: [turn]
- // var notes: String?
- }
- // Ne creo una loro collezione
- var officeList: [Office] = []
- // Creo una funzione che crea un nuovo ufficio e lo aggiunge alla lista
- func addOffice(name: String, address: String, coordinates: (Float, Float), turns: [String]) {
- // Assegna un ID sempre maggiore a seconda di quanti uffici son già registrati
- var ID: Int
- if officeList.isEmpty {
- ID = 1
- } else {
- ID = (officeList.last?.ID)! + 1
- }
- var timeTable: [turn] = []
- for element in turns {
- let thisTurn = turn(time: element, isBooked: false, queueType: "")
- timeTable.append(thisTurn)
- }
- let thisOffice = Office(ID: ID, name: name, address: address, coordinates: coordinates, timeTable: timeTable)
- officeList.append(thisOffice)
- }
- // Creo una funzione che controlla la disponibilità di un turno in un ufficio
- func checkTurn(ID: Int, time: String) {
- for office in officeList {
- if office.ID == ID {
- for turn in office.timeTable {
- if turn.time == time {
- if turn.isBooked == false {
- print("This turn is available for booking!")
- return
- } else {
- print("This turn has already been booked for \(turn.queueType)!")
- return
- }
- }
- }
- print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
- return
- }
- }
- print("Office not found!")
- }
- // Creo la funzione che prenota il turno in un ufficio
- func bookTurn (ID: Int, time: String, queueType: String) {
- for office in officeList {
- if office.ID == ID {
- for turn in office.timeTable {
- if turn.time == time {
- if turn.isBooked == false {
- //turn.isBooked = true
- //turn.queueType = queueType
- print("This turn has been successfully booked for \(queueType)!")
- return
- } else {
- print("This turn has already been booked!")
- return
- }
- }
- }
- print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
- return
- }
- }
- print("Office not found!")
- }
- register(name: "Mattia", surname: "Fonisto", email: "m.fonisto@gmail.com", password: "mammt")
- register(name: "Federica", surname: "Fonisto", email: "federìca.fonisto@gmail.com", password: "patt")
- login(email: "m.fonisto@gmail.com", password: "mammt")
- login(email: "m.fonisto@gmail.com", password: "patt")
- login(email: "mario.fonisto@gmail.com", password: "nonnt")
- addOffice(name: "Poste", address: "Via Mammt", coordinates: (1.000, 1.000), turns: ["08:00", "08:30", "09:00"])
- addOffice(name: "Banca", address: "Via Patt", coordinates: (2.000, 2.000), turns: ["15:00", "15:30", "16:00"])
- checkTurn(ID: 1, time: "08:00")
- checkTurn(ID: 10, time: "08:00")
- checkTurn(ID: 1, time: "Turno che non esiste")
- bookTurn(ID: 2, time: "15:00", queueType: "withdrawal")
- checkTurn(ID: 2, time: "15:00")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement