Java - Pila con solso 5 numeros para mascota virtual :)

 
Vista:

Pila con solso 5 numeros para mascota virtual :)

Publicado por Natalia R. (1 intervención) el 07/06/2015 07:15:08
Antes que nada hola a tod@s! :D , tengo una duda que aun no he podido solucionar. Estoy haciendo una mascota virtual como proyecto final de mi materia "Estructura de datos" , y quiero usar una pila para poder almacenar solos 5 numeros (espacios) solamente con numeros 1 , no se si logro hacerme entender, quiero implementar una pila para que coma el animalito solos 5 veces y se llene , asi puede jugar y bajar de uno en uno esa pila :) . Hasta ahora he hecho este programa funcional pero sin implementar la pila. Acontinuacion lo anexo:

Clase StartGame:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
package mascotavirtual;
 
import java.util.Scanner;
 
/**
 *
 * @author Natalia
 */
public class StartGame {
 
    private Mascota mascota;
    private Scanner sc;
 
    public StartGame () {
        mascota = new Mascota();
        sc = new Scanner (System.in);
        preguntaNombre();
        preguntaEdad();
    }
 
    private void preguntaNombre (){
        System.out.print("Nombre de la mascota: ");
        String nombre = sc.nextLine();
        mascota.setNombre(nombre);
    }
 
    private void preguntaEdad (){
        System.out.print("Edad de la mascota: ");
        int edad = sc.nextInt();
        mascota.setEdad(edad);
    }
 
    private int presentaMenu(){
        System.out.println("##########################################");
        System.out.println("Selecciona una opcion que deseas hacer con " + mascota.getNombre() + "?");
        System.out.println("[1] - Alimentar");
        System.out.println("[2] - Bañar");
        System.out.println("[3] - Jugar");
        System.out.println("\n[0] - Salir");
        System.out.print("Opcion: ");
        int opc = sc.nextInt();
        return opc;
    }
 
    public static void main(String [] args) {
        StartGame game = new StartGame();
        boolean continuar = true;
        do{
            int opc = game.presentaMenu();
            switch(opc){
                case 0:
                    continuar = false;
                    break;
                case 1:
                    System.out.println("Alimentado..");
                    game.mascota.alimentar();
                    System.out.println("Nivel de hambre: " + game.mascota.getHambre());
                    break;
                case 2:
                    System.out.println("Bañado..");
                    game.mascota.bañarse();
                    System.out.println("Nivel de limpieza: " + game.mascota.getLimpieza());
                    break;
                case 3:
                    System.out.println("Jugando..");
                    game.mascota.jugar();
                    System.out.println("Nivel de limpieza: " + game.mascota.getLimpieza());
                    System.out.println("Nivel de hambre: " + game.mascota.getHambre());
                    System.out.println("Nivel de energia:" + game.mascota.getEnergia());
                    break;
                default:
                    System.out.println("Opcion incorrecta. Intentalo de nuevo");
            }
 
        } while(continuar);
    }
}

Clase MascotaVirtual:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mascotavirtual;
 
import java.util.Vector;
 
/**
 *
 * @author Natalia
 */
public class Mascota {
    private String nombre;
    private int edad;
    private int energia;
    private int hambre;
    private int limpieza;
    private Vector<String> items;
    private boolean alive = true;
 
    public Mascota(String nombre, int edad) {
        this();//Esto llama al contructor por defecto(vacio)
        this.nombre = nombre;
        this.edad = edad;
 
    }
 
    public Mascota(){
        nombre = new String();
        edad=0;
        energia = 100;
        hambre = 0;
        limpieza = 100;
        items = new Vector<String>();
 
    }
 
    public void alimentar (){
        if (alive) {
            if (hambre >=5){
                hambre -=5;
            }else{
                hambre = 0;
            }
        }
    }
 
    public void jugar (){
        if(alive){
            hambre += 5;
            energia -= 4;
            limpieza -= 5;
            if(hambre >= 100  ||  energia < 1) {
            //muere
                alive = false;
        }
        }
    }
 
    public void bañarse () {
        if (alive) {
                limpieza += 10;
        }
    }
 
    public String getNombre() {
        return nombre;
    }
 
    public void setNombre(String nombre){
        this.nombre = nombre;
    }
 
    public int getEdad() {
        return edad;
    }
 
    public void setEdad(int edad) {
        this.edad = edad;
    }
 
    public int getEnergia() {
        return energia;
    }
 
    public void setEnergia (int energia) {
        this.energia = energia;
    }
 
    public int getHambre (){
        return hambre;
    }
 
    public void setHambre(int hambre) {
        this.hambre = hambre;
    }
 
    public int getLimpieza () {
        return limpieza;
    }
 
    public void setLimpieza (int limpieza) {
        this.limpieza =  limpieza;
    }
 
    public Vector<String> getItems() {
        return items;
    }
 
    public void setItems(Vector<String> items) {
        this.items = items;
    }
 
    public boolean isAlive() {
        return alive;
    }
 
}


Gracias de antemano! :D y saludos!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Pila con solso 5 numeros para mascota virtual :)

Publicado por V.M (1 intervención) el 27/04/2022 14:57:54
Hola, buenos dias, como estas? En el colegio debemos hacer un proyecto de Macota virtual, queria saber si nos podrias ayudar.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar