Transactions between several classes

Asked

Viewed 43 times

0

Good, I’m relatively new to Java programming! The following is a Super Class named User of which there are 2 subclasses Citizen and Autarchy, within the Citizen class I have created an Arraylist that will guard the Prizes of that Citizen, these prizes will be inserted in this list by Autarchy and I also have a Prize class from which I will create several instances of prizes to be awarded to the Cities as I must proceed so that the Autarchy can give the prizes to the Citizens?

    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;
}
  • 1

    But all the Cizitens sane Users ? It seems a little strange this modeling. And should always use ArrayList generic, which in the case of its class Citizen would be private ArrayList<Prize> prizes

  • Yes all Citizens and Autarchy will be users of the app, only have different functionalities

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;
}
  • If I insert these methods into my classes it keeps saying that "there is no default constructor in Users.User", some suggestion Luciano?

  • Try creating a default constructor in User: public User(){}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.