Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.63 KB | None | 0 0
  1. import UIKit
  2. import Foundation
  3.  
  4. // Definisco gli utenti
  5. struct User {
  6.     var name: String
  7.     var surname: String
  8.     var email: String
  9.     var password: String
  10. }
  11. // Ne creo una loro collezione
  12. var userList: [User] = []
  13. // Creo una funzione che aggiunge nuovi utenti alla lista se non già registrati
  14. func register(name: String, surname: String, email: String, password: String) {
  15.     for user in userList {
  16.         if user.email == email {
  17.             print("Email already registered!")
  18.             return
  19.         }
  20.     }
  21.     let thisUser = User(name: name, surname: surname, email: email, password: password)
  22.     userList.append(thisUser)
  23.     print("Registration completed!")
  24. }
  25. // Creo una funzione per fare il login
  26. func login(email: String, password: String) {
  27.     for user in userList {
  28.         if user.email == email {
  29.             if user.password == password {
  30.                 print("Login successful!")
  31.                 return
  32.             } else {
  33.                 print("Wrong password!")
  34.                 return
  35.             }
  36.         }
  37.     }
  38.     print("User not registered!")
  39. }
  40. // Definisco i turni per gli uffici (time è di tipo "hh:mm")
  41. struct turn {
  42.     var time: String
  43.     var isBooked: Bool
  44.     var queueType: String
  45. }
  46. // Definisco gli uffici
  47. struct Office {
  48.     var ID: Int
  49.     var name: String
  50.     var address: String
  51.     var coordinates: (Float, Float)
  52.     var timeTable: [turn]
  53.     // var notes: String?
  54. }
  55. // Ne creo una loro collezione
  56. var officeList: [Office] = []
  57. // Creo una funzione che crea un nuovo ufficio e lo aggiunge alla lista
  58. func addOffice(name: String, address: String, coordinates: (Float, Float), turns: [String]) {
  59.     // Assegna un ID sempre maggiore a seconda di quanti uffici son già registrati
  60.     var ID: Int
  61.     if officeList.isEmpty {
  62.         ID = 1
  63.     } else {
  64.         ID = (officeList.last?.ID)! + 1
  65.     }
  66.     var timeTable: [turn] = []
  67.     for element in turns {
  68.         let thisTurn = turn(time: element, isBooked: false, queueType: "")
  69.         timeTable.append(thisTurn)
  70.     }
  71.     let thisOffice = Office(ID: ID, name: name, address: address, coordinates: coordinates, timeTable: timeTable)
  72.     officeList.append(thisOffice)
  73. }
  74. // Creo una funzione che controlla la disponibilità di un turno in un ufficio
  75. func checkTurn(ID: Int, time: String) {
  76.     for office in officeList {
  77.         if office.ID == ID {
  78.             for turn in office.timeTable {
  79.                 if turn.time == time {
  80.                     if turn.isBooked == false {
  81.                         print("This turn is available for booking!")
  82.                         return
  83.                     } else {
  84.                         print("This turn has already been booked for \(turn.queueType)!")
  85.                         return
  86.                     }
  87.                 }
  88.             }
  89.             print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
  90.             return
  91.         }
  92.     }
  93.     print("Office not found!")
  94. }
  95. // Creo la funzione che prenota il turno in un ufficio
  96. func bookTurn (ID: Int, time: String, queueType: String) {
  97.     for office in officeList {
  98.         if office.ID == ID {
  99.             for turn in office.timeTable {
  100.                 if turn.time == time {
  101.                     if turn.isBooked == false {
  102.                         //turn.isBooked = true
  103.                         //turn.queueType = queueType
  104.                         print("This turn has been successfully booked for \(queueType)!")
  105.                         return
  106.                     } else {
  107.                         print("This turn has already been booked!")
  108.                         return
  109.                     }
  110.                 }
  111.             }
  112.             print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
  113.             return
  114.         }
  115.     }
  116.     print("Office not found!")
  117. }
  118.  
  119.  
  120. register(name: "Mattia", surname: "Fonisto", email: "m.fonisto@gmail.com", password: "mammt")
  121. register(name: "Federica", surname: "Fonisto", email: "federìca.fonisto@gmail.com", password: "patt")
  122. login(email: "m.fonisto@gmail.com", password: "mammt")
  123. login(email: "m.fonisto@gmail.com", password: "patt")
  124. login(email: "mario.fonisto@gmail.com", password: "nonnt")
  125. addOffice(name: "Poste", address: "Via Mammt", coordinates: (1.000, 1.000), turns: ["08:00", "08:30", "09:00"])
  126. addOffice(name: "Banca", address: "Via Patt", coordinates: (2.000, 2.000), turns: ["15:00", "15:30", "16:00"])
  127. checkTurn(ID: 1, time: "08:00")
  128. checkTurn(ID: 10, time: "08:00")
  129. checkTurn(ID: 1, time: "Turno che non esiste")
  130. bookTurn(ID: 2, time: "15:00", queueType: "withdrawal")
  131. checkTurn(ID: 2, time: "15:00")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement