package Uebungen.Uebung5;
import java.util.Scanner;
import java.lang.Character;
public class Aufgabe2 {
    public static void main(String[] args){
        // initiate scanner for input
        Scanner scanner = new Scanner(System.in);
        while(true){
            userInterface();
            System.out.print("\n\n Check anoter string? [y/N]: ");
            if(scanner.nextLine()!="y") break;
            System.out.println();
        }
    }
    public static void userInterface() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter desired string: ");
        char[] stringAsCharArr = scanner.nextLine().toCharArray();
        stringAsCharArr = normalize(stringAsCharArr);
        char[] usedCharArr = usedChars(stringAsCharArr);
        int[]  charAmountArr = countChars(usedCharArr ,stringAsCharArr);
        sort(charAmountArr,usedCharArr);
        histogramm(20,usedCharArr, charAmountArr);
    }
    public static char[] normalize(char[] workerArray){
        for(int i=0; i<workerArray.length;i++)
            workerArray[i]=Character.toLowerCase(workerArray[i]);
        return workerArray;
    }
    public static void histogramm(int width, char[] availableChars, int[] charAmount){
        int max = maximum(charAmount);
        for(int i=0;i<availableChars.length;i++) {
            System.out.print("\""+availableChars[i]+"\": ");
            if(max>width) // scale if max width is exceeded
                for(int j=0;j<scale(charAmount[i],max,width);j++) System.out.print("#");
            else
                for(int j=0;j<charAmount[i];j++) System.out.print("#");
            System.out.println("x"+charAmount[i]);
        }
    }
    public static int scale(int value, int oldMax, int newMax){
        double calc = (double) value/oldMax*newMax;
        return (int) calc;
    }
    // Bubble Sort
    public static void sort(int[] amounts, char[] chars){
       for(int i=amounts.length-1;i>0; i--)
           for(int j=0;j<i;j++)
               if(amounts[j]<amounts[j+1])
                   swapItems(amounts,chars,j,j+1);
    }
    public static void swapItems(int[] amounts, char[] chars, int index1, int index2){
        char memC = chars[index1];
        int memI = amounts[index1];
        chars[index1] = chars[index2];
        amounts[index1] = amounts[index2];
        chars[index2] = memC;
        amounts[index2] = memI;
    }
    public static int maximum(int[] inputArray){
        int largestNumber = inputArray[0];
        for(int i=1;i<inputArray.length;i++)
            if(inputArray[i]>largestNumber)
                largestNumber = inputArray[i];
        return largestNumber;
    }
    public static int[] countChars(char[] availableChars, char[] scannedArray){
        int[] occurrences = new int[availableChars.length];
        // Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0.
        for(char currChar:scannedArray)
            for(int i=0;i<availableChars.length;i++)
                if(availableChars[i]==currChar)
                    occurrences[i]++;
        return occurrences;
    }
    public static char[] usedChars(char[] inputArray){
        char[] workerArray = inputArray.clone(); // System.out.println(inputArray.hashCode()); // Get the hashcode of the array
        // workingArray gets an new hashcode after modifying, so its okay using it
        // now we remove all characters that occur multiple times
        int counter=0; // we also count how many different chars we found
        for(int i=0; i<workerArray.length;i++){
            if((int)workerArray[i]!=0) { //there is no need to run if the char is already the null character
                counter++; // found a new one -> increment counter
                // now remove all similar chars from that point till the end of the array
                for (int j = i + 1; j < workerArray.length; j++) // loop through starting from current pos.
                    if (workerArray[i] == workerArray[j])  // check if char already exists
                        workerArray[j] = (char) 0; // set the char to 	"Null character"
            }
        }
        char[] targetArray = new char[counter]; // create new
        int targetPosition = 0; // initiate counter for the Target Array
        for(char currChar:workerArray) // seek through all chars
            if((int)currChar!=0){ // if its not a null character it is a new char
                targetArray[targetPosition] = currChar; // add the current char
                targetPosition++; // we added a new char, so we need to increment for the next one
            }
        return targetArray;
    }
}
