Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Foundation
- // Funzioni matematiche di supporto per la conversione degli angoli
- func deg2rad(deg: Double) -> Double {
- return deg * Double.pi / 180
- }
- func rad2deg(rad: Double) -> Double {
- return rad * 180.0 / Double.pi
- }
- // Funzione per calcolare la distanza in km tra due posizioni gps
- func distance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
- let theta = lon1 - lon2
- var dist = sin(deg2rad(deg: lat1)) * sin(deg2rad(deg: lat2)) + cos(deg2rad(deg: lat1)) * cos(deg2rad(deg: lat2)) * cos(deg2rad(deg: theta))
- dist = acos(dist)
- dist = rad2deg(rad: dist)
- dist = dist * 60 * 1.853159616
- return dist
- }
- // 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")
- class turn {
- var time: String
- var isBooked: Bool
- var queueType: String
- init(time: String, isBooked: Bool, queueType: String) {
- self.time = time
- self.isBooked = isBooked
- self.queueType = queueType
- }
- }
- // Definisco gli uffici
- class Office {
- var ID: Int
- var name: String
- var address: String
- var coordinates: (Double, Double)
- var timeTable: [turn]
- init(name: String, address: String, coordinates: (Double, Double), turns: [String]) {
- self.name = name
- self.address = address
- self.coordinates = coordinates
- // Assegna un ID sempre maggiore a seconda di quanti uffici son già registrati
- if officeList.isEmpty {
- self.ID = 1
- } else {
- self.ID = (officeList.last?.ID)! + 1
- }
- self.timeTable = []
- for element in turns {
- let thisTurn = turn(time: element, isBooked: false, queueType: "")
- self.timeTable.append(thisTurn)
- }
- }
- // Creo una funzione che controlla la disponibilità di un turno in un ufficio
- func checkTurn(time: String) {
- for turn in 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
- }
- // Creo la funzione che prenota il turno in un ufficio
- func bookTurn (time: String, queueType: String) {
- for turn in 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 for \(turn.queueType)!")
- return
- }
- }
- }
- print("Wrong time format (not hh:mm) or this turn for that office does not exist!")
- return
- }
- func getDistance (myLocation: (Double, Double)) {
- print("Your distance from this office is \(((distance(lat1: myLocation.0, lon1: myLocation.1, lat2: coordinates.0, lon2: coordinates.1) * 10).rounded() / 10)) km!")
- }
- }
- // Ne creo una loro collezione che contiene solo ID e coordinate
- var officeList: [(ID: Int, coordinates: (Double, Double))] = []
- // Creo una funzione che crea un nuovo ufficio e lo aggiunge alla lista
- func newGenericOffice(name: String, address: String, coordinates: (Double, Double), turns: [String]) -> Office? {
- for office in officeList {
- if office.coordinates == coordinates {
- print("Office already exists with same location!")
- return nil
- }
- }
- let thisOffice = Office(name: name, address: address, coordinates: coordinates, turns: turns)
- officeList.append((ID: thisOffice.ID, coordinates: coordinates))
- print("Office created successfully!")
- return thisOffice
- }
- // Creo una sottoclasse di uffici del gruppo Poste
- class PostOffice: Office {
- var officeType: String
- var internalID: Int
- init(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, internalID: Int) {
- self.officeType = officeType
- self.internalID = internalID
- super.init(name: name, address: address, coordinates: coordinates, turns: turns)
- }
- }
- // Ne creo una loro collezione che contiene solo internalID e coordinate
- var postOfficeList: [(internalID: Int, coordinates: (Double, Double))] = []
- // Creo una funzione che crea un nuovo ufficio postale e lo aggiunge alla lista generale e specifica
- func newPostOffice(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, internalID: Int) -> PostOffice? {
- for office in officeList {
- if office.coordinates == coordinates {
- print("Post office already exists with same location!")
- return nil
- }
- }
- let thisOffice = PostOffice(name: name, address: address, coordinates: coordinates, turns: turns, officeType: officeType, internalID: internalID)
- officeList.append((ID: thisOffice.ID, coordinates: coordinates))
- postOfficeList.append((internalID: internalID, coordinates: coordinates))
- print("Post office created successfully!")
- return thisOffice
- }
- // Creo una sottoclasse di uffici del gruppo Banche
- class Bank: Office {
- var officeType: String
- var withdrawalAvailable: Bool
- init(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, withdrawalAvailable: Bool) {
- self.officeType = officeType
- self.withdrawalAvailable = withdrawalAvailable
- super.init(name: name, address: address, coordinates: coordinates, turns: turns)
- }
- func changeWithdrawalStatus(status: Bool) {
- if self.withdrawalAvailable == status {
- print("Already been set \(status)!")
- return
- } else {
- self.withdrawalAvailable = status
- print("Withdrawal availability changed correctly! New status is: \(self.withdrawalAvailable)!")
- return
- }
- }
- }
- // Ne creo una loro collezione che contiene solo internalID e coordinate
- var bankList: [(Double, Double)] = []
- // Creo una funzione che crea una nuova banca e la aggiunge alla lista generale e specifica
- func newBank(name: String, address: String, coordinates: (Double, Double), turns: [String], officeType: String, withdrawalAvailable: Bool) -> Bank? {
- for office in officeList {
- if office.coordinates == coordinates {
- print("Bank already exists with same location!")
- return nil
- }
- }
- let thisOffice = Bank(name: name, address: address, coordinates: coordinates, turns: turns, officeType: officeType, withdrawalAvailable: withdrawalAvailable)
- officeList.append((ID: thisOffice.ID, coordinates: coordinates))
- bankList.append(coordinates)
- print("Bank created successfully!")
- return thisOffice
- }
- // Un po' di debug
- register(name: "Mattia", surname: "Fonisto", email: "m.fonisto@gmail.com", password: "herp")
- register(name: "Federica", surname: "Fonisto", email: "federìca.fonisto@gmail.com", password: "derp")
- print("UserList:", userList)
- login(email: "m.fonisto@gmail.com", password: "herp")
- login(email: "m.fonisto@gmail.com", password: "derp")
- login(email: "mario.fonisto@gmail.com", password: "durr")
- 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)
- 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)
- 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)
- let questura = newGenericOffice(name: "Questura di Napoli", address: "Via Medina, 75, 80133 Napoli NA", coordinates: (40.84298, 14.252435), turns: ["09:00"])
- print("OfficeList:", officeList)
- print("PostOfficeList:", postOfficeList)
- print("BankList:", bankList)
- banca?.changeWithdrawalStatus(status: false)
- poste?.checkTurn(time: "08:00")
- poste?.checkTurn(time: "Turno che non esiste")
- banca?.bookTurn(time: "15:00", queueType: "withdrawal")
- banca?.checkTurn(time: "15:00")
- banca?.bookTurn(time: "15:00", queueType: "deposit")
- banca?.getDistance(myLocation: (40.85946, 14.262271))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement