79299624

Date: 2024-12-21 15:14:59
Score: 1
Natty:
Report link

Thank you all. I managed to solve this problem. There have been a few changes to my code. Let me tell you the idea that I used to solve my problem: "The first two fish, male and female, appear. Fish can reproduce from the age of 4. When the male fish is 4 years old, I add it to the adultMaleFishList. When a female fish turns 4 years old, she starts looking for a mate randomly from the adultMaleFishList. After finding a mate, we delete the male fish from the adultMaleFishList. And these two fish will mate and give birth to a new babyFish and after that these fish will rest for 3 years. After 3 years, we will add the male fish to the adultMaleFishList. The female fish again starts looking for a mate in the adultMaleFishList, and so on. When the age of the fish is equal to the age of death, the fish dies and we delete it from the fishList. When the fishList runs out of fish, the program stops. This is how the pretend I used works".

An example of my program code:

Main class:

package lesson.uz;

public class Main {

    public static void main(String[] args) {

        Fish fish1 = new Fish(Gender.MALE);
        fish1.start();
        Aquarium.fishList.add(fish1);

        try {
            Thread.sleep(1_00);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        Fish fish2 = new Fish(Gender.FEMALE);
        fish2.start();
        Aquarium.fishList.add(fish2);

        Information.start();
    }

}

Fish class:

package lesson.uz;

import lombok.Getter;
import lombok.Setter;

import java.util.Random;

@Getter
@Setter
public class Fish extends Thread {

    private Gender gender;
    private Integer lifespan;
    private String fishName;
    private Integer age = 0;
    private boolean isAddedToList;
    private Integer spawnInterval;

    public Fish(Gender gender) {
        this.gender = gender;
        this.lifespan = new Random().nextInt(6) + 10;
        this.isAddedToList = false;
        this.spawnInterval = -1;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(1000);

                if(this.age >= this.lifespan){
                    if(this.gender == Gender.MALE){
                        Aquarium.adultMaleFishList.remove(this);
                        Aquarium.fishList.remove(this);
                        Information.maleFish.decrementAndGet();
                    }else{
                        Aquarium.fishList.remove(this);
                        Information.femaleFish.decrementAndGet();
                    }
                    Information.diedFish.incrementAndGet();
                    Thread.currentThread().interrupt();
                }

                if(spawnInterval == 0 || spawnInterval == 1 || spawnInterval == 2 || spawnInterval == 3) {
                    if(spawnInterval == 3){
                        if(gender == Gender.MALE){
                            Aquarium.adultMaleFishList.add(this);
                        }
                        spawnInterval = -1;
                    }else {
                        spawnInterval++;
                    }
                }

                if(gender == Gender.FEMALE && age>=4 && spawnInterval==-1){
                    reproduce();
                }

                if(!isAddedToList && gender == Gender.MALE && age>=4){
                    Aquarium.adultMaleFishList.add(this);
                    isAddedToList = true;
                }

                age++;
            } catch (InterruptedException e) {
            }
        }
    }

    public static Fish createFish() {
        Fish newFish = new Fish(Math.random() > 0.5 ? Gender.MALE : Gender.FEMALE);
        newFish.start();
        if (newFish.getGender() == Gender.MALE) {
            Information.maleFish.incrementAndGet();
        }else {
            Information.femaleFish.incrementAndGet();
        }
        return newFish;
    }

    public void reproduce() {
        Fish randomFish = Aquarium.getRandomMaleFish();

        if(randomFish == null){
            return;
        }

        this.spawnInterval = 0;
        Fish babyFish = createFish();
        Aquarium.fishList.add(babyFish);
    }
}

Aquarium class:

package lesson.uz;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public class Aquarium {

    public static CopyOnWriteArrayList<Fish> fishList = new CopyOnWriteArrayList<>();
    public static CopyOnWriteArrayList<Fish> adultMaleFishList = new CopyOnWriteArrayList<>();

    public static synchronized Fish getRandomMaleFish() {
        if(adultMaleFishList.isEmpty()) {
            return null;
        }
        Fish selectedFish = adultMaleFishList.get(new Random().nextInt(adultMaleFishList.size()));

        selectedFish.setSpawnInterval(0);
        adultMaleFishList.remove(selectedFish);
        return selectedFish;
    }
}

Information class:

package lesson.uz;

import java.util.concurrent.atomic.AtomicInteger;

public class Information {

    public static AtomicInteger allFish = new AtomicInteger(0);
    public static AtomicInteger maleFish = new AtomicInteger(1);
    public static AtomicInteger femaleFish = new AtomicInteger(1);
    public static AtomicInteger diedFish = new AtomicInteger(0);

    public static void start() {
        System.out.println("\n=====================     Start     =====================\n");

        while (true) {

            allFish.set(Aquarium.fishList.size());

            System.out.println("All fish: " + allFish+
                    "\nMale fish: " + maleFish +
                    "\nFemale fish: " + femaleFish +
                    "\nDied fish: " + diedFish +"\n");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if(allFish.get() == 0) {
                break;
            }
        }
        System.out.println("=====================     Finish     =====================");
    }
}

Gender enum:

package lesson.uz;

public enum Gender {
    MALE, FEMALE
}

It's true that my code is not pretty. My code may not be beautifully written based on SOLID, OOP principles. Because I am still new to programming. Thanks to everyone who helped me solve this problem.

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Tuychi Sharipov