package com.kekstudio.musictheory;

/**
 * Music Key.
 * @author Andy671
 */
public class Key implements Comparable<Key> {
    
    private Note note;
    private String quality;
    private String name;
    private Scale scale;
    
    /**
     * Initializes the key with a name and a quality.
     * @param noteName the key note.
     * @param quality
     */
    public Key(String noteName, String quality){
        this(new Note(noteName),  quality);
    }
    
    /**
     * Initializes the key with a note and a quality.
     * @param note the key note.
     * @param quality the quality of the key .
     */
    public Key(Note note, String quality){
        this.note = note;
        this.name = note.getName() + " " + quality;
        this.quality = quality;
        scale = note.scale(quality);
    }
    
    /**
     * Initializes the chord with a degree and a type.
     * @param degree degree of the chord (e.g. bV, I, IV).
     * @param type type of the chord.
     * @return the chord.
     */
    public Chord chord(String degree, String type){
        String degreeSymbol = degree;
        char flatOrSharp = degree.charAt(0);

        if (flatOrSharp == Music.FLAT || flatOrSharp == Music.SHARP) {
          degreeSymbol = degree.substring(1);
        }

        int scaleIndex = Music.Degrees.indexOf(degreeSymbol);
        Note rootNote = this.scale.notes[scaleIndex].copy();
        
        if (flatOrSharp == Music.FLAT) {
          Note.semitoneDown(rootNote);
        } else if (flatOrSharp == Music.SHARP) {
          Note.semitoneUp(rootNote);
        }

        return rootNote.chord(type);
    }

    
    @Override
    public int compareTo(Key otherKey) {
        return this.note.getMidiValue() - otherKey.note.getMidiValue();
    }

    @Override
    public String toString() {
        return name + " " + quality;
    }

}

@@ -163,23 +163,28 @@
  String 
-quality
+scaleKeyType
 ;%0A    pr
@@ -345,23 +345,28 @@
  @param 
-quality
+scaleKeyType
 %0A     */
@@ -397,39 +397,44 @@
 oteName, String 
-quality
+scaleKeyType
 )%7B%0A        this(
@@ -454,23 +454,28 @@
 Name),  
-quality
+scaleKeyType
 );%0A    %7D
@@ -589,23 +589,28 @@
  @param 
-quality
+scaleKeyType
  the qua
@@ -668,23 +668,28 @@
  String 
-quality
+scaleKeyType
 )%7B%0A     
@@ -748,31 +748,36 @@
 e() + %22 %22 + 
-quality
+scaleKeyType
 ;%0A        th
@@ -783,25 +783,35 @@
 his.
-quality = quality
+scaleKeyType = scaleKeyType
 ;%0A  
@@ -835,23 +835,28 @@
 e.scale(
-quality
+scaleKeyType
 );%0A    %7D
@@ -1311,32 +1311,190 @@
 (1);%0A        %7D%0A%0A
+        if(!Music.Degrees.contains(degreeSymbol))%7B%0A            throw new MusicTheoryException(%22Unknown degree symbol name '%22 + degreeSymbol + %22'%22);%0A        %7D%0A
         int scal
@@ -2042,23 +2042,28 @@
 + %22 %22 + 
-quality
+scaleKeyType
 ;%0A    %7D%0A

Refactor Key and add exceptions