Operations between various classes

0

Well, I'm relatively new to programming in Java! It is the following I have a Super Class of User name of which there are 2 subclasses Citizen and Autarchy, within the class Citizen I have created an ArrayList that will guard the Prizes of this citizen, those prizes will be inserted in this list by Autarchy being that I also have a class Prize of which I will create several instances of prizes to assign to the citizens how I must proceed so that the Autarchy can give the prizes to the Citizen?

    package Users;


    public class User {
private String name;
private String password;
private String email;

public User(String name, String password, String email) {
    this.name = name;
    this.password = password;
    this.email = email;
}

//public receiveEmail(this.email) function to email a confirmation email to the user


public String getName() {
    return name;
}

public String getPassword() {
    return password;
}

public String getEmail() {
    return email;
}


}
package Users;

import java.util.ArrayList;

public class Citizen extends User{
private ArrayList prizes = new ArrayList<>();

public Citizen(String name, String password, String email) {
    super(name, password, email);
}

public ArrayList getPrizes() {
    return this.prizes;
}


@Override
public String toString() {
    return "Name:  " + this.getName() + " | Password:  " + this.getPassword() +" | E-mail:  " + this.getEmail() + " | prizes:  " + this.prizes;
}
package Users;

public class Prize{
private String name;
private String description;
private int value;

public Prize(String name, String description, int value) {
    this.name = name;
    this.description = description;
    this.value = value;
}

@Override
public String toString() {
    return "Achievment: " + this.name;
}
    
asked by anonymous 05.11.2017 / 04:08

1 answer

0

There are several ways. You could write a method for your Citizen class, for example:

public receivePrize(Prize newPrize) {
    if(prize != NULL)
        prizes.add(prize);
}

and in its Autarchy class:

public givePrize(Citizen winner) { 
    winner.receivePrize(takePrize());
}

private Prize takePrize() {
    Prize prize = NULL;
    if (prizes.size() > 0) {
        prize = prizes.get(prizes.size() - 1);
        prizes.remove(prizes.size() - 1);
    } else {
        System.out.print("Sorry there are no more prizes\n");
    }
    return prize;
}
    
05.11.2017 / 04:39