Advertisement
NelloRizzo

Flotta veicoli

Apr 23rd, 2020
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.11 KB | None | 0 0
  1. Module Module1
  2.     ' Flotta di veicoli
  3.     ' Data una flotta di veicoli, determinare se due diversi veicoli hanno lo stesso consumo
  4.     ' quale veicolo è più veloce e quale ha il maggior numero di ruote
  5.  
  6.     Class Vehicle
  7.         Public Property Model As String ' modello di veicolo
  8.         Public Property Consumption As Integer ' consumo al km
  9.         Public Property Speed As Integer ' velocità al km
  10.         Public ReadOnly Property Wheels As Integer ' numero di ruote (è un'informazione che dipende dal tipo e non dall'utente!!!)
  11.         ' wheels non è modificabile durante il ciclo di vita di un veicolo, quindi
  12.         ' DEVE essere NECESSARIAMENTE inizializzata nel costruttore
  13.         Public Sub New(wheels As Integer)
  14.             Me.Wheels = wheels
  15.         End Sub
  16.         ' regola di uguaglianza tra veicoli: per me sono uguali quando hanno lo stesso consumo
  17.         Public Overrides Function Equals(obj As Object) As Boolean
  18.             If TypeOf obj Is Vehicle Then
  19.                 Dim tmp As Vehicle = obj
  20.                 Return tmp.Consumption = Consumption
  21.             End If
  22.             Return False
  23.         End Function
  24.         ' gethashcode deve solo restituire un valore conforme ad equals:
  25.         ' quando due oggetti sono uguali restituiscono lo stesso hashcode
  26.         Public Overrides Function GetHashCode() As Integer
  27.             Return Consumption
  28.         End Function
  29.     End Class
  30.  
  31.     Public Sub FleetManagement(vehicles() As Vehicle)
  32.         Dim wheelsMax As Vehicle = vehicles(0)
  33.         Dim faster As Vehicle = vehicles(0)
  34.         For Each vehicle In vehicles ' individua il più veloce e quello che ha più ruote
  35.             If vehicle.Speed > faster.Speed Then faster = vehicle
  36.             If vehicle.Wheels > wheelsMax.Wheels Then wheelsMax = vehicle
  37.         Next
  38.         Console.WriteLine("Veicolo più veloce: {0}", faster)
  39.         Console.WriteLine("Veicolo con più ruote: {0}", wheelsMax)
  40.  
  41.         Dim index = 0 ' cerca se ci sono veicoli uguali
  42.         Dim same1 As Vehicle = Nothing
  43.         Dim same2 As Vehicle = Nothing
  44.         ' scansione esterna
  45.         While same1 Is Nothing And index < vehicles.Length - 1
  46.             Dim innerIndex = index + 1
  47.             ' scansione interna
  48.             While same1 Is Nothing And innerIndex < vehicles.Length
  49.                 If vehicles(index).Equals(vehicles(innerIndex)) Then
  50.                     same1 = vehicles(index)
  51.                     same2 = vehicles(innerIndex)
  52.                 End If
  53.                 innerIndex += 1
  54.             End While
  55.             index += 1
  56.         End While
  57.         If same1 Is Nothing Then
  58.             Console.WriteLine("Non ci sono veicoli uguali")
  59.         Else
  60.             Console.WriteLine("I veicoli {0} e {1} sono uguali", same1, same2)
  61.         End If
  62.     End Sub
  63.     Sub Main()
  64.         Dim biciDaCorsa As New Bicycle With {.Model = "Bici da corsa", .Speed = 40, .Consumption = 5}
  65.         Dim biciDaPasseggio As New Bicycle With {.Model = "Bici da passeggio", .Speed = 15, .Consumption = 1}
  66.         Dim c4 As New Machine With {.Model = "Citroen C4", .Speed = 180, .Consumption = 13}
  67.         Dim ferrari As New Machine With {.Model = "Ferrari", .Speed = 280, .Consumption = 100}
  68.         Dim truck As New Truck With {.Model = "Iveco", .Speed = 90, .Consumption = 60}
  69.         Dim tucson As New Machine With {.Model = "Hyunday Tucson", .Speed = 190, .Consumption = 13}
  70.  
  71.         Dim fleet() As Vehicle = {biciDaCorsa, biciDaPasseggio, c4, ferrari, truck, tucson}
  72.         FleetManagement(fleet)
  73.     End Sub
  74.  
  75. End Module
  76. Class Bicycle
  77.     Inherits Vehicle
  78.  
  79.     Public Sub New()
  80.         MyBase.New(2)
  81.     End Sub
  82.  
  83.     Public Overrides Function ToString() As String
  84.         Return String.Format("Bicicletta: {0}", Model)
  85.     End Function
  86. End Class
  87.  
  88. Class Machine
  89.     Inherits Vehicle
  90.  
  91.     Public Sub New()
  92.         MyBase.New(4)
  93.     End Sub
  94.     Public Overrides Function ToString() As String
  95.         Return String.Format("Automobile: {0}", Model)
  96.     End Function
  97. End Class
  98.  
  99. Class Truck
  100.     Inherits Vehicle
  101.  
  102.     Public Sub New()
  103.         MyBase.New(6)
  104.     End Sub
  105.     Public Overrides Function ToString() As String
  106.         Return String.Format("Camion: {0}", Model)
  107.     End Function
  108.  
  109. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement