fcamuso

Kotlin video 28

Aug 26th, 2025
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.45 KB | None | 0 0
  1. import java.io.File
  2. import java.io.FileWriter
  3. import java.io.InvalidObjectException
  4. import javax.management.ImmutableDescriptor
  5. import kotlin.random.Random
  6.  
  7. //open class Arma(var nome: String, protected open var dannoBase: Int = 1) {
  8. //
  9. //    init { require(dannoBase>=0) }
  10. //
  11. //    var valore: Int = 0
  12. //
  13. //    //costrutture secondario che assicura univocitΓ  del nome scelto per l'arma
  14. //    constructor (uniqueName: String) : this(uniqueName,0)
  15. //    {
  16. //        //logica che verifica che nessun'altra arma sia stata chiamatata
  17. //        //con lo stesso nome; se scopre
  18. //        println("passato di qui")
  19. //
  20. //    }
  21. //
  22. //    open fun spara() {
  23. //        println("Danno cinetico: $dannoBase")
  24. //    }
  25. //}
  26.  
  27. // Contratto per il comportamento di un'arma automatica
  28. interface Immagazzinabile {
  29.     val peso: Int
  30.     val volume: Int
  31.  
  32.     fun schedaOggetto(): MutableList<String>
  33.     //{
  34. //        val descrizioni: MutableList<String> = mutableListOf()
  35. //
  36. //        descrizioni.add("Peso: $peso")
  37. //        descrizioni.add("Volume: $volume")
  38. //
  39. //        return descrizioni
  40. //}
  41. }
  42.  
  43. abstract class Arma(var nome: String, protected open var dannoBase: Int = 1) : Immagazzinabile  {
  44.  
  45.     init { require(dannoBase>=0) }
  46.  
  47.     abstract var valore: Int
  48.  
  49.     //costrutture secondario che assicura univocitΓ  del nome scelto per l'arma
  50.     constructor (uniqueName: String) : this(uniqueName,0)
  51.     {
  52.         //logica che verifica che nessun'altra arma sia stata chiamatata
  53.         //con lo stesso nome; se scopre
  54.         println("passato di qui")
  55.  
  56.     }
  57.  
  58.     abstract fun spara()
  59. }
  60.  
  61. class Armatura : Immagazzinabile{
  62.     override val peso: Int = 0
  63.     override val volume: Int = 0
  64.  
  65.     override fun schedaOggetto(): MutableList<String> {
  66.         val descrizioni: MutableList<String> = mutableListOf()
  67.  
  68.         descrizioni.add("ARMATURA Peso: $peso")
  69.         descrizioni.add("ARMATURA Volume: $volume")
  70.  
  71.         return descrizioni
  72.     }
  73.  
  74. }
  75. class Reliquia : Immagazzinabile{
  76.     override val peso: Int = 0
  77.     override val volume: Int = 0
  78.  
  79.     override fun schedaOggetto(): MutableList<String> {
  80.         val descrizioni: MutableList<String> = mutableListOf()
  81.  
  82.         descrizioni.add("RELIQUIA Peso: $peso")
  83.         descrizioni.add("RELIQUIA Volume: $volume")
  84.  
  85.         return descrizioni
  86.     }
  87.  
  88. }
  89.  
  90.  
  91. class Zaino {
  92.     val listaOggetti: MutableList<Immagazzinabile> = mutableListOf()
  93. //    val listaArmature: MutableList<Armatura> = mutableListOf()
  94. //    val listaReliquie: MutableList<Reliquia> = mutableListOf()
  95. //    //ecc. ecc.
  96.  
  97.     fun add(nuovoOggetto: Immagazzinabile) { listaOggetti.add(nuovoOggetto)}
  98. //    fun addArmatura(nuovaArmatura: Armatura) { listaArmature.add(nuovaArmatura)}
  99. //    fun addReliquia(nuovaReliquia: Reliquia) { listaReliquie.add(nuovaReliquia)}
  100. }
  101.  
  102.  
  103.  
  104. class ArmaElettrica(uniqueName: String)  : Arma(uniqueName) {
  105.  
  106.     override val peso: Int = 0
  107.     override val volume: Int = 0
  108.  
  109.     override  var dannoBase : Int = 2
  110.         get() {return field;}
  111.         set(value) {field = value}
  112.  
  113.     var livelloDannoElementale: Int = 1
  114.  
  115.     override var valore: Int = 0
  116.  
  117.     constructor(nome: String, livelloDannoElementale: Int) : this("")
  118.     {
  119.         require(livelloDannoElementale in 1..5)
  120.         this.livelloDannoElementale = livelloDannoElementale
  121.     }
  122.  
  123.     constructor() : this("", 4) {}
  124.  
  125.     fun metodoClasseFiglia() {
  126.         //println(dannoBase)
  127.     }
  128.  
  129.     override fun spara() {
  130.         //super.spara() //non avrebbe senso
  131.         println("Danno ELETTRICO: ${dannoBase+10*livelloDannoElementale}")
  132.     }
  133.  
  134.     override fun schedaOggetto(): MutableList<String> {
  135.         val descrizioni: MutableList<String> = mutableListOf()
  136.  
  137.         descrizioni.add("ARMA ELETTRICA Peso: $peso")
  138.         descrizioni.add("ARMA ELETTRICA: $volume")
  139.  
  140.         return descrizioni
  141.     }
  142.  
  143.  
  144.     }
  145.  
  146.  
  147. fun main() {
  148.  
  149. //val unArmaElettrica = ArmaElettrica()
  150.  
  151.     //println("${unArmaElettrica.nome} ${unArmaElettrica.metodoClasseFiglia()}" )
  152.     //unArmaElettrica.spara()
  153.  
  154.  
  155.     val loZaino = Zaino();
  156.  
  157.     val unArma= ArmaElettrica("boom", 5)
  158.     loZaino.add( unArma )
  159.     loZaino.add( Reliquia() )
  160.     loZaino.add( Armatura() )
  161.  
  162.     for (oggetto in loZaino.listaOggetti) {
  163.         for (rigaDescrizione in oggetto.schedaOggetto())
  164.             println(rigaDescrizione)
  165.         println("---------------------------------------")
  166.     }
  167.  
  168.     //(loZaino.listaOggetti[0] as Arma).spara()
  169.  
  170.  
  171.     //late binding
  172. }
Advertisement
Add Comment
Please, Sign In to add comment