package ucsbCurriculum.Utility;

import java.util.ArrayList;
import ucsbCurriculum.Utility.Time;

import ucsbCurriculum.Utility.Time;

public class Util {
	
	// return true if those two Time have conflicts
	public static boolean have_conflict(Time t1, Time t2){
		return t1.start_time > t2.start_time ? (t1.start_time > t2.end_time) : (t2.start_time > t1.start_time);
	}

//  save for later, this can be used for compare when adding classes
	public static boolean compare(ArrayList<Time> time1, ArrayList<Time> time2){
		
		for(int i = 0; i < time1.size(); i++){
			for(int j = 0; j < time2.size(); j++){
				if(have_conflict(time1.get(i), time2.get(j))){
					// TO-DO: delete that element in the second list
					time2.remove(j);
				}
			}
		}
	
		// TO-DO: check the size of time2, if it's 0, return false, otherwise return true
//		return false; 
		if(time2.size()==0){
			return false;
		}
			return true;
	}
	
	public Time converts_to_minute(String daym, String timm){
        int[] t = new int[2];
        int time = 0;
        char a_char;
        char b_char;
        char c_char;
        char d_char;
        int addedmin;
        int minutes;
        int firstmin;
        int more;
        String hi;
        //String hello = " 9:30am - 12:33am ";
        String[] tab = timm.split("-");
        int timesforday=0;
    //converts day to int
        String daytomin = daym;
        if(daytomin.contains("M"))
            timesforday=0;
        if(daytomin.contains("T"))
            timesforday=(24*60);
        if(daytomin.contains("W"))
            timesforday=(48*60);
        if(daytomin.contains("R"))
            timesforday=(72*60);
        if(daytomin.contains("F"))
            timesforday=(96*60);
        //converts time to int
        for(int i=0; i<tab.length; i++){
            if(tab[i].contains("pm")){
                if(tab[i].length()==8){
                hi = tab[i];
                a_char = hi.charAt(1);
                b_char = hi.charAt(3);
                c_char = hi.charAt(4);
                addedmin = Character.getNumericValue(a_char);
                minutes = Character.getNumericValue(b_char);
                more = Character.getNumericValue(c_char);
                time = (12*60) + (addedmin * 60) + (minutes*10) + (more);
                t[i]=time;
                }
                else if(tab[i].length() == 9){
                hi = tab[i];
                a_char = hi.charAt(1);
                d_char = hi.charAt(2);
                b_char = hi.charAt(4);
                c_char = hi.charAt(5);
                addedmin = Character.getNumericValue(a_char);
                firstmin = Character.getNumericValue(d_char);
                minutes = Character.getNumericValue(b_char);
                more = Character.getNumericValue(c_char);
                    if((addedmin==1)&&(firstmin==2)){
                        time = ((12 * 60) + (minutes*10) + (more));
                        t[i]=time;
                    }
                    else{
                        time = ((12*60) + ((10 + firstmin) * 60) + (minutes*10) + (more));
                        t[i]=time;
                    }
                
                }                
            }
            else if(tab[i].contains("am")){
                if(tab[i].length()==8){
                hi = tab[i];
                a_char = hi.charAt(1);
                b_char = hi.charAt(3);
                c_char = hi.charAt(4);
                addedmin = Character.getNumericValue(a_char);
                minutes = Character.getNumericValue(b_char);
                more = Character.getNumericValue(c_char);
                time =(addedmin * 60) + (minutes*10) + (more);
                t[i]=time;
                }
                else if(tab[i].length() == 9){
                hi = tab[i];
                a_char = hi.charAt(1);
                d_char = hi.charAt(2);
                b_char = hi.charAt(4);
                c_char = hi.charAt(5);
                addedmin = Character.getNumericValue(a_char);
                firstmin = Character.getNumericValue(d_char);
                minutes = Character.getNumericValue(b_char);
                more = Character.getNumericValue(c_char);
                    if((addedmin==1)&&(firstmin==2)){
                        time = 0 +(minutes*10)+more;
                        t[i]=time;
                    }
                    else{
                        time = (((10 + firstmin) * 60) + (minutes*10) + (more));
                        t[i]=time;
                    }
                
                }                
            }
 //return t;           
}
        
        
        Time ti= new Time();
        ti.starttime=timesforday+t[0];
        ti.endtime=timesforday+t[1];
        
        return ti;
 }
 
public static String convert_to_string(int starttime, int endtime){
        String[] names = {"M","T","W","R","F"};
        //starttime
        int startday = (starttime / (24*60));
        String strday1 = names[startday];
        
        String strtime;
        //starttime
        int fhour;
        int fdays = (starttime/(24*60));
        if(fdays==0){
            fhour = (starttime/(60))-12;
        }
        else
            fhour = (starttime-(fdays*24*60))/60-12;
        int ftenminute = (starttime-(fdays*24*60)-(fhour*60+(12*60)))/10;
        int foneminute = (starttime-(fdays*24*60)-(fhour*60+(12*60)))%10;
        //endtime 
        int shour;
        int sdays = (endtime/(24*60));
        if(sdays==0){
            shour = (endtime/(60))-12;
        }
        else
            shour = (endtime-(sdays*24*60))/60-12;
        int stenminute = (endtime-(sdays*24*60)-(shour*60+(12*60)))/10;
        int soneminute = (endtime-(sdays*24*60)-(shour*60+(12*60)))%10;
        strtime = strday1 + " " + Integer.toString(fhour)+":"+Integer.toString(ftenminute)+ Integer.toString(foneminute) + " - " + Integer.toString(shour)+":"+Integer.toString(stenminute)+ Integer.toString(soneminute)+ " ";
        return strtime;
    
}

}

@@ -367,552 +367,8 @@
 ;%0A%09%7D
-%0A%0A//  save for later, this can be used for compare when adding classes%0A%09public static boolean compare(ArrayList%3CTime%3E time1, ArrayList%3CTime%3E time2)%7B%0A%09%09%0A%09%09for(int i = 0; i %3C time1.size(); i++)%7B%0A%09%09%09for(int j = 0; j %3C time2.size(); j++)%7B%0A%09%09%09%09if(have_conflict(time1.get(i), time2.get(j)))%7B%0A%09%09%09%09%09// TO-DO: delete that element in the second list%0A%09%09%09%09%09time2.remove(j);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%0A%09%09// TO-DO: check the size of time2, if it's 0, return false, otherwise return true%0A//%09%09return false; %0A%09%09if(time2.size()==0)%7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09%09return true;%0A%09%7D
 %0A%09%0A%09

Update Util.java