Advertisement
Guest User

Il mio primo bimbo in programmazione ad oggetti

a guest
Nov 5th, 2017
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 9.81 KB | None | 0 0
  1. import UIKit
  2. import Foundation
  3.  
  4. // Funzioni matematiche di supporto per la conversione degli angoli
  5. func deg2rad(deg: Double) -> Double {
  6.     return deg * Double.pi / 180
  7. }
  8. func rad2deg(rad: Double) -> Double {
  9.     return rad * 180.0 / Double.pi
  10. }
  11. // Funzione per calcolare la distanza in km tra due posizioni gps
  12. func distance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
  13.     let theta = lon1 - lon2
  14.     var dist = sin(deg2rad(deg: lat1)) * sin(deg2rad(deg: lat2)) + cos(deg2rad(deg: lat1)) * cos(deg2rad(deg: lat2)) * cos(deg2rad(deg: theta))
  15.     dist = acos(dist)
  16.     dist = rad2deg(rad: dist)
  17.     dist = dist * 60 * 1.853159616
  18.     return dist
  19. }
  20. // Definisco gli utenti
  21. struct User {
  22.     var name: String
  23.     var surname: String
  24.     var email: String
  25.     var password: String
  26. }
  27. // Ne creo una loro collezione
  28. var userList: [User] = []
  29. // Creo una funzione che aggiunge nuovi utenti alla lista se non già registrati
  30. func register(name: String, surname: String, email: String, password: String) {
  31.     for user in userList {
  32.         if user.email == email {
  33.             print("Email already registered!")
  34.             return
  35.         }
  36.     }
  37.     let thisUser = User(name: name, surname: surname, email: email, password: password)
  38.     userList.append(thisUser)
  39.     print("Registration completed!")
  40. }
  41. // Creo una funzione per fare il login
  42. func login(email: String, password: String) {
  43.     for user in userList {
  44.         if user.email == email {
  45.             if user.password == password {
  46.                 print("Login successful!")
  47.                 return
  48.             } else {
  49.                 print("Wrong password!")
  50.                 return
  51.             }
  52.         }
  53.     }
  54.     print("User not registered!")
  55. }
  56. // Definisco i turni per gli uffici (time è di tipo "hh:mm")
  57. class turn {
  58.     var time: String
  59.     var isBooked: Bool
  60.     var queueType: String
  61.     init(time: String, isBooked: Bool, queueType: String) {
  62.         self.time = time
  63.         self.isBooked = isBooked
  64.         self.queueType = queueType
  65.     }
  66. }
  67. // Definisco gli uffici
  68. class Office {
  69.     var ID: Int
  70.     var name: String
  71.     var address: String
  72.     var coordinates: (Double, Double)
  73.     var timeTable: [turn]
  74.     init(name: String, address: String, coordinates: (Double, Double), turns: [String]) {
  75.         self.name = name
  76.         self.address = address
  77.         self.coordinates = coordinates
  78.         // Assegna un ID sempre maggiore a seconda di quanti uffici son già registrati
  79.         if officeList.isEmpty {
  80.             self.ID = 1
  81.         } else {
  82.             self.ID = (officeList.last?.ID)! + 1
  83.         }
  84.         self.timeTable = []
  85.         for element in turns {
  86.             let thisTurn = turn(time: element, isBooked: false, queueType: "")
  87.             self.timeTable.append(thisTurn)
  88.         }
  89.     }
  90.     // Creo una funzione che controlla la disponibilità di un turno in un ufficio
  91.     func checkTurn(time: String) {
  92.         for turn in timeTable {
  93.             if turn.time == time {
  94.                 if turn.isBooked == false {
  95.                     print("This turn is available for booking!")
  96.                     return
  97.                 } else {
  98.                     print("This turn has already been booked for \(turn.queueType)!")
  99.                     return
  100.                 }
  101.             }
  102.         }
  103.     print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
  104.     return
  105.     }
  106.     // Creo la funzione che prenota il turno in un ufficio
  107.     func bookTurn (time: String, queueType: String) {
  108.          for turn in timeTable {
  109.              if turn.time == time {
  110.                 if turn.isBooked == false {
  111.                     turn.isBooked = true
  112.                     turn.queueType = queueType
  113.                     print("This turn has been successfully booked for \(queueType)!")
  114.                     return
  115.                 } else {
  116.                     print("This turn has already been booked for \(turn.queueType)!")
  117.                     return
  118.                 }
  119.              }
  120.          }
  121.         print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
  122.         return
  123.     }
  124.     func getDistance (myLocation: (Double, Double)) {
  125.         print("Your distance from this office is \(((distance(lat1: myLocation.0, lon1: myLocation.1, lat2: coordinates.0, lon2: coordinates.1) * 10).rounded() / 10)) km!")
  126.     }
  127. }
  128. // Ne creo una loro collezione che contiene solo ID e coordinate
  129. var officeList: [(ID: Int, coordinates: (Double, Double))] = []
  130. // Creo una funzione che crea un nuovo ufficio e lo aggiunge alla lista
  131. func newGenericOffice(name: String, address: String, coordinates: (Double, Double), turns: [String]) -> Office? {
  132.     for office in officeList {
  133.         if office.coordinates == coordinates {
  134.             print("Office already exists with same location!")
  135.             return nil
  136.         }
  137.     }
  138.     let thisOffice = Office(name: name, address: address, coordinates: coordinates, turns: turns)
  139.     officeList.append((ID: thisOffice.ID, coordinates: coordinates))
  140.     print("Office created successfully!")
  141.     return thisOffice
  142. }
  143. // Creo una sottoclasse di uffici del gruppo Poste
  144. class PostOffice: Office {
  145.     var officeType: String
  146.     var internalID: Int
  147.     init(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, internalID: Int) {
  148.         self.officeType = officeType
  149.         self.internalID = internalID
  150.         super.init(name: name, address: address, coordinates: coordinates, turns: turns)
  151.     }
  152. }
  153. // Ne creo una loro collezione che contiene solo internalID e coordinate
  154. var postOfficeList: [(internalID: Int, coordinates: (Double, Double))] = []
  155. // Creo una funzione che crea un nuovo ufficio postale e lo aggiunge alla lista generale e specifica
  156. func newPostOffice(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, internalID: Int) -> PostOffice? {
  157.     for office in officeList {
  158.         if office.coordinates == coordinates {
  159.             print("Post office already exists with same location!")
  160.             return nil
  161.         }
  162.     }
  163.     let thisOffice = PostOffice(name: name, address: address, coordinates: coordinates, turns: turns, officeType: officeType, internalID: internalID)
  164.     officeList.append((ID: thisOffice.ID, coordinates: coordinates))
  165.     postOfficeList.append((internalID: internalID, coordinates: coordinates))
  166.     print("Post office created successfully!")
  167.     return thisOffice
  168. }
  169. // Creo una sottoclasse di uffici del gruppo Banche
  170. class Bank: Office {
  171.     var officeType: String
  172.     var withdrawalAvailable: Bool
  173.     init(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, withdrawalAvailable: Bool) {
  174.         self.officeType = officeType
  175.         self.withdrawalAvailable = withdrawalAvailable
  176.         super.init(name: name, address: address, coordinates: coordinates, turns: turns)
  177.     }
  178.     func changeWithdrawalStatus(status: Bool) {
  179.         if self.withdrawalAvailable == status {
  180.             print("Already been set \(status)!")
  181.             return
  182.         } else {
  183.             self.withdrawalAvailable = status
  184.             print("Withdrawal availability changed correctly! New status is: \(self.withdrawalAvailable)!")
  185.             return
  186.         }
  187.     }
  188. }
  189. // Ne creo una loro collezione che contiene solo internalID e coordinate
  190. var bankList: [(Double, Double)] = []
  191. // Creo una funzione che crea una nuova banca e la aggiunge alla lista generale e specifica
  192. func newBank(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, withdrawalAvailable: Bool) -> Bank? {
  193.     for office in officeList {
  194.         if office.coordinates == coordinates {
  195.             print("Bank already exists with same location!")
  196.             return nil
  197.         }
  198.     }
  199.     let thisOffice = Bank(name: name, address: address, coordinates: coordinates, turns: turns, officeType: officeType, withdrawalAvailable: withdrawalAvailable)
  200.     officeList.append((ID: thisOffice.ID, coordinates: coordinates))
  201.     bankList.append(coordinates)
  202.     print("Bank created successfully!")
  203.     return thisOffice
  204. }
  205. // Un po' di debug
  206. register(name: "Mattia", surname: "Fonisto", email: "m.fonisto@gmail.com", password: "herp")
  207. register(name: "Federica", surname: "Fonisto", email: "federìca.fonisto@gmail.com", password: "derp")
  208. print("UserList:", userList)
  209. login(email: "m.fonisto@gmail.com", password: "herp")
  210. login(email: "m.fonisto@gmail.com", password: "derp")
  211. login(email: "mario.fonisto@gmail.com", password: "durr")
  212. let poste = newPostOffice(name: "Poste Italiane", address: "Piazza Matteotti, 2, 80133 Napoli NA, Italy", coordinates: (40.84373, 14.251225), turns: ["08:00", "08:30", "09:00"], officeType: "Poste Centrali", internalID: 101)
  213. let poste2 = newPostOffice(name: "Poste Italiane", address: "Galleria Umberto I, 22, 80132 Napoli NA, Italy", coordinates: (40.838489, 14.249982), turns: ["08:00", "08:30", "09:00"], officeType: "Filiale", internalID: 102)
  214. let banca = newBank(name: "Banco di Napoli", address: "Via Toledo, 396, 80132 Napoli NA, Italy", coordinates: (40.846459, 14.249199), turns: ["15:00", "15:30", "16:00"], officeType: "Filiale Hub", withdrawalAvailable: true)
  215. let questura = newGenericOffice(name: "Questura di Napoli", address: "Via Medina, 75, 80133 Napoli NA", coordinates: (40.84298, 14.252435), turns: ["09:00"])
  216. print("OfficeList:", officeList)
  217. print("PostOfficeList:", postOfficeList)
  218. print("BankList:", bankList)
  219. banca?.changeWithdrawalStatus(status: false)
  220. poste?.checkTurn(time: "08:00")
  221. poste?.checkTurn(time: "Turno che non esiste")
  222. banca?.bookTurn(time: "15:00", queueType: "withdrawal")
  223. banca?.checkTurn(time: "15:00")
  224. banca?.bookTurn(time: "15:00", queueType: "deposit")
  225. banca?.getDistance(myLocation: (40.85946, 14.262271))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement