Loesung Praktikum 01 PDF

Title Loesung Praktikum 01
Course Algorithmen und Programmierung II
Institution Technische Hochschule Köln
Pages 4
File Size 92 KB
File Type PDF
Total Downloads 21
Total Views 214

Summary

Lösung zur 1. Praktikumsaufgabe...


Description

import kotlin.math.ceil class Time init { if if if } }

(val hour : Int, val minute : Int ){ (hour < 0 || minute < 0) throw Exception("Keine -") (hour > 24) throw Exception("Hour zu hoch") (minute > 59) throw Exception("minute zu hch")

class ParkTicket (val entryTime : Time){ var exitTime : Time? = null fun parkingDuration() : Int{ if (exitTime != null) { var hours : Int var minutes : Int hours = exitTime!!.hour - entryTime.hour minutes = exitTime!!.minute - entryTime.minute return (hours * 60) + minutes } else throw Exception("ParkTicket muss vorher korrekt abgestempelt werden") } //ALS EIGENSCHAFT val parkingDuration : Int get(){ if (exitTime != null) { var hours : Int var minutes : Int hours = exitTime!!.hour - entryTime.hour minutes = exitTime!!.minute - entryTime.minute return (hours * 60) + minutes } else throw Exception("ParkTicket muss vorher korrekt abgestempelt werden") }

fun hoursStarted() : Int{ if (exitTime != null) { var count = ceil(parkingDuration().toDouble() / 60.0).toInt() return count

} else throw Exception("ParkTicket muss vorher korrekt abgestempelt werden") } // ASL EIGENSCHAFT val hoursStarted : Int get(){

if (exitTime != null) { var count = ceil(parkingDuration().toDouble() / 60.0).toInt() return count

} else throw Exception("ParkTicket muss vorher korrekt abgestempelt werden") }

fun checkout (exitTime : Time){ if (exitTime.hour > this.entryTime.hour || exitTime.hour == this.entryTime.hour && exitTime.minute > this.entryTime.minute) this?.exitTime = exitTime } } enum class Tariff(val price : Double){ STANDARD(1.99), EVENT(1.49),WEEKEND(2.99); fun price () : Double{ return when (this){ STANDARD -> 1.99 EVENT -> 1.49 WEEKEND -> 2.99 } } }

class TicketMachine(val aktuellerTarif : Tariff){ val scheinListe = mutableListOf()

fun generate (entryTime : Time) : ParkTicket{ val Ticket = ParkTicket(entryTime) this.scheinListe.add(Ticket) return Ticket } fun checkout(ticketNr : Int, checkoutTime : Time){ val validTicket = scheinListe[ticketNr] validTicket.exitTime = checkoutTime } fun shortestParkingDuration(): Int { var count = 0 var shortest = 0 var duration = 1440 while (count < this.scheinListe.size) {

if (this.scheinListe[count].exitTime != null) { if (this.scheinListe[count].parkingDuration < duration) { shortest = count duration = this.scheinListe[count].parkingDuration } } count++ } return shortest } fun averageParkingDuration(): Int{ var count = 0 var validCount = 0 var duration = 0 while (count < this.scheinListe.size) { if (this.scheinListe[count].exitTime != null) { duration += this.scheinListe[count].parkingDuration validCount++ } count++ } return duration / validCount } fun revenues () : Double { var count = 0 var hours = 0 while (count < this.scheinListe.size) { if (this.scheinListe[count].exitTime != null) { hours += this.scheinListe[count].hoursStarted } count++ } return this.aktuellerTarif.price * hours.toDouble() } fun wrapUp(){ var stundenGesamt = 0.0 for(each in this.scheinListe) { if (each.exitTime == null) each.exitTime = Time(23, 59) println("Von " + each.entryTime.hour + ":" + each.entryTime.minute + " - " + each.exitTime!!.hour + ":" + each.exitTime!!.minute + " : " + each.parkingDuration + " minuten (berechnet " + each.hoursStarted + "stunden)") stundenGesamt += each.hoursStarted } println("Gesamte Einnahmen des Tages: " + (this.aktuellerTarif.price * stundenGesamt.toDouble())) } }

fun main(){ val machine = TicketMachine ( Tariff . STANDARD ) val ticket1 = machine . generate ( Time (12 , 0) ) val ticket2 = machine . generate ( Time (12 , 0) ) val ticket3 = machine . generate ( Time (12 , 0) ) val ticket4 = machine . generate ( Time (13 , 30) ) ticket1 . checkout ( Time (12 , 30) ) // 30 min (1h) ticket2 . checkout ( Time (13 , 0) ) // 60 min (1h) ticket3 . checkout ( Time (14 , 0) ) // 80 min (2h) // ticket4 wird nicht abgestempelt und wird daher für alle // folgenden Methodenaufrufe nicht ber ü cksichtigt println ( machine.scheinListe[machine.shortestParkingDuration()].parkingDuration ) // Gibt 30 aus println ( machine . averageParkingDuration () ) // Gibt 56 aus ((30 + 60 + 80) / 3) println ( machine . revenues () ) // Gibt 7.96 aus ((1 + 1 + 2) * machine.tariff . price ()) machine.wrapUp () }...


Similar Free PDFs