package com.example.pokemongsonbattle
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
class MainActivity : AppCompatActivity() {
private val tag = "PokemonGSONDemo"
private lateinit var pokemonList: MutableList<Pokemon>
private val gson = Gson()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createPokemonList()
displayPokemonList("Initial Pokemon List")
modifyPokemonList()
displayPokemonList("Modified Pokemon List")
depletePokemonList()
}
private fun createPokemonList() {
pokemonList = mutableListOf(
Pokemon("Pikachu", 100, 55, 40),
Pokemon("Charmander", 90, 60, 45),
Pokemon("Squirtle", 95, 50, 65),
Pokemon("Bulbasaur", 100, 45, 55),
Pokemon("Jigglypuff", 110, 40, 35),
Pokemon("Meowth", 85, 55, 40),
Pokemon("Psyduck", 90, 50, 50),
Pokemon("Geodude", 100, 70, 80),
Pokemon("Magikarp", 80, 20, 30),
Pokemon("Eevee", 95, 55, 50)
)
Log.d(tag, "Created 10 Pokemon records")
}
private fun displayPokemonList(message: String) {
Log.d(tag, "--- $message ---")
val jsonList = gson.toJson(pokemonList)
Log.d(tag, jsonList)
}
private fun modifyPokemonList() {
pokemonList.forEachIndexed { index, pokemon ->
pokemon.hp += 10
pokemon.attack += 5
Log.d(tag, "Modified Pokemon $index: ${gson.toJson(pokemon)}")
}
}
private fun depletePokemonList() {
while (pokemonList.isNotEmpty()) {
val removed = pokemonList.removeAt(0)
Log.d(tag, "Removed Pokemon: ${gson.toJson(removed)}")
Log.d(tag, "Remaining Pokemon: ${pokemonList.size}")
}
Log.d(tag, "All Pokemon depleted")
}
data class Pokemon(
val name: String,
var hp: Int,
var attack: Int,
val defense: Int
)
}