Java representation of Chapter 24 by Pink Floyd


Originally posted as GitHub Gist on 04 Oct 2016 , updated at 20 Mar 2018 (details)


Source: https://gist.github.com/paulera/bc934e1da56bca9175d01d8d33731425

Description: This is the Java implementation of Pink Floyd's song "Chapter 24"


Java

Chapter24_PinkFloyd.java

  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This is the Java implementation of the song
   Chapter 24, by Pink Floyd.
   Album:  The Piper at the Gates of Dawn
   Year: 1967

   References:
   https://en.wikipedia.org/wiki/Chapter_24
   https://www.youtube.com/watch?v=CqMBQptI7Nk

   It expects that some external object call
   the notifyDarknessChange() method, so when it
   is increased by one the young light is formed
   and all movement can be accomplished

   Compiled in https://ideone.com/xW54vj
   
   @author: github.com/paulera
   */

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) {
		/* The time is with the month of winter solstice
		   When the change is due to come. */
		Chapter24 crazyTune = new Chapter24(new Date(2016, 12, 31));
	}
}

class Chapter24 {

	private Map<String, Light> lights = null;
	private int currentStage = 0;
	private Boolean movementAccomplished = false;
	private int initialDarkness;

	public int heavenDirection;
	public int thunderDirection;

	public Chapter24(Date timeOfTheYear) {

		if (timeOfTheYear.getMonth() == 1) {
			change();
		}

		this.lights = new HashMap<String, Light>(2);
		this.lights.put("old", new Light(70));
		this.initialDarkness = Piper.getDarkness();

		/* hold on loop waiting for the movement to be accomplished */
		while (!(this.movementAccomplished = this.doMovement())) {
		}
	}

	public Boolean canDestroy() {
		/* Thunder in the other course of heaven. */
		if (this.thunderDirection == (this.heavenDirection * -1)) {
			/* Things cannot be destroyed once and for all. */
			return false;
		}

		return true;
	}

	public Boolean doMovement() {
		if (this.lights.containsKey("young")) {
			while (this.executeStage() < ((Light)this.lights.get("young")).number) {
				if (this.currentStage == ((Light)this.lights.get("young")).number - 1) {
					/* All movement is accomplished in six stages */
					this.movementAccomplished = true;
				}
			}
			/* And the seventh brings return. */
			return true;
		}
		return false;
	}

	private int executeStage() {
		return ++this.currentStage;
	}

	public void notifyDarknessChange (int darknessLevel) {
		if (darknessLevel == this.initialDarkness + 1) {
			/* The seven is the number of the young light
			It forms when darkness is increased by one. */
			this.lights.put("young", new Light(7));
		}
	}

	public String change() {
		try {
			/* Going */
			this.go();
			/* and coming */
			this.come();
		} catch (Exception ex) {} // without error

		/* Change returns success */
		return "success";
	}

	/* Action brings good fortune. */
	public int action() {
		return 7;
	}

	/* Sunset */
	private void go() {
		System.out.println("Sunset");
	}

	/* Sunrise */
	private void come() {
		System.out.println("Sunrise");
	}

}

class Light {
	public int number = -1;
	public Light(int lightNumber) {
		this.number = lightNumber;
	}
}

class Piper {
	public static int getDarkness() {
		// TODO: implement darkness level getter
		return -1;
	}
}