import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Clook{
public static ArrayList<String> order = new ArrayList<>();
public static ArrayList<Integer> fragments = new ArrayList<>();
public static int cylinder = 200;
public static int headstart = 53;
public static int current;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("\n===== C-Look Algorithm =====\n");
System.out.println(" [1] Predefined \n [2] User-Defined");
int status = in.nextInt(); in.nextLine();
//int status =1;
switch(status){
case 1:
fragments.add(98);
fragments.add(183);
fragments.add(37);
fragments.add(122);
fragments.add(14);
fragments.add(124);
fragments.add(65);
fragments.add(67);
fragments.add(headstart);
fragments.add(0);
fragments.add(cylinder-1);
goingUp();
goingDown();
break;
case 2:
System.out.println("Cylinder Size: ");
cylinder = in.nextInt();in.nextLine();
System.out.println("Number of Fragments: ");
int fragSize = in.nextInt();in.nextLine();
System.out.println("Headstart: ");
headstart = in.nextInt();in.nextLine();
fragments.add(headstart);
for(int i = 1; i <= fragSize; i++){
System.out.println("Enter fragment's address " + i + " : " );
int frag = in.nextInt();in.nextLine();
if(frag>cylinder-1){
System.out.println("This address exceeded the cylinder size, enter valid address.");
i = i-1;
}
else if(fragments.contains(frag)){
System.out.println("This address has already been requested, enter a valid address.");
i=i-1;
}
else
fragments.add(frag);
}
fragments.add(0);
fragments.add(cylinder-1);
goingUp();
goingDown();
break;
}
}
static void header(){
Collections.sort(fragments);
//System.out.print("0" + " \t");
for(int i = 0; i < fragments.size(); i++){
System.out.print(fragments.get(i) + " \t");
order.add(" ");
}
//System.out.print(cylinder-1 + " \t");
}
static void goingDown(){
System.out.println("\n======= GOING DOWN =======");
header();
ArrayList<Integer> distances = new ArrayList<>();
ArrayList<Integer> orderArr = new ArrayList<>();
current = fragments.indexOf(headstart);
for(int i = 0; i < fragments.size()-2; i++){
if(current < 1){
current = fragments.size()-2;
}
order.set(current, Integer.toString(i+1));
System.out.println("\n");
//System.out.print(" " + "\t");
for (int j = 0; j < order.size(); j++) {
System.out.print(order.get(j) + " \t");
}
int distance;
if(current < fragments.size()-2){
distance = fragments.get(current) - fragments.get(current+1);
}
else
distance = fragments.get(current) - fragments.get(1);
distances.add(i, Math.abs(distance));
orderArr.add(fragments.get(current));
order.set(current, " ");
current--;
}
distances.remove(0);
System.out.println("\n\n Distances: " + distances.toString());
int thm = 0;
for(int i = 0; i < distances.size(); i++){
thm = thm + distances.get(i);
}
System.out.println("Total Head Movement: " + thm + "\n\n");
GraphClook.createAndShowGui(fragments,orderArr,thm,distances);
System.out.println(orderArr.toString());
}
static void goingUp(){
System.out.println("\n======= GOING UP =======");
header();
ArrayList<Integer> distances = new ArrayList<>();
ArrayList<Integer> orderArr = new ArrayList<>();
current = fragments.indexOf(headstart);
for(int i = 0; i < fragments.size()-2; i++){
if(current > fragments.size()-2){
current = 1;
}
order.set(current, Integer.toString(i+1));
System.out.println("\n");
for (int j = 0; j < order.size(); j++) {
System.out.print(order.get(j) + " \t");
}
int distance;
if(current > 1){
distance = fragments.get(current) - fragments.get(current-1);
}
else
distance = fragments.get(current) - fragments.get(fragments.size()-2);
distances.add(i, Math.abs(distance));
orderArr.add(fragments.get(current));
order.set(current, " ");
current++;
}
distances.remove(0);
System.out.println("\n\n Distances: " + distances.toString());
int thm = 0;
for(int i = 0; i < distances.size(); i++){
thm = thm + distances.get(i);
}
System.out.println("Total Head Movement: " + thm + "\n\n");
GraphClook.createAndShowGui(fragments,orderArr,thm,distances);
}
}