Skip to content

Instantly share code, notes, and snippets.

@Lvl4Sword
Created March 21, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lvl4Sword/0fc014777c56f3575b8e354136a16d79 to your computer and use it in GitHub Desktop.
Save Lvl4Sword/0fc014777c56f3575b8e354136a16d79 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import com.opencsv.CSVReader ;
import com.opencsv.CSVWriter ;
import java.net.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
class SumIntSess{
static int[] shift_hours;
static ArrayList<Person> staff;
static HashMap<String,Shift_Person> shift_staff;
static int staff_for_swift;
public static void main(String[] args)throws Exception{
String nameFile = "";
JFileChooser fileChooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("CSV Files","csv");
fileChooser.setFileFilter(filter);
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
nameFile = selectedFile.getAbsolutePath();
}
int min_for_swift = Integer.parseInt(JOptionPane.showInputDialog("How many people minimum for a shift?", null));
staff_for_swift = Integer.parseInt(JOptionPane.showInputDialog("How many people maximum for a shift?", null));
CSVReader reader = new CSVReader(new FileReader(nameFile));
ArrayList<String[]> all_csv = (ArrayList)reader.readAll();
CSVWriter writer = new CSVWriter(new FileWriter(nameFile.replace(".csv", "_NEW.csv")));
ArrayList<String[]> new_csv = new ArrayList<String[]>();
String[] old_headers = all_csv.get(0);
String[] new_headers = new String[144];
new_headers[0] = "Full name";
new_headers[1] = "Rank";
new_headers[2] = "Actual allotted hours";
new_headers[3] = "Preferred number of hours";
shift_hours = new int[140];
int tot_hours = calc_hours(Arrays.copyOfRange(old_headers, 7, old_headers.length));
for(int i = 7; i<old_headers.length; i++){
new_headers[i-3] = old_headers[i].substring(old_headers[i].indexOf(",")+2);
}
int max_hours_available = 0;
staff = new ArrayList<Person>();
for(int i = 1; i<all_csv.size(); i++){
String[] a_line = all_csv.get(i);
SumIntSess.Person a_person = new SumIntSess().new Person(a_line[1] + " " + a_line[2], Integer.parseInt(a_line[5]), a_line[3]);
staff.add(a_person);
max_hours_available += Integer.parseInt(a_line[5]);
}
int ratio_employ = tot_hours*staff_for_swift*100/max_hours_available;
if(ratio_employ>100){
staff_for_swift = staff_for_swift * 100 / ratio_employ;
ratio_employ = 100;
}
shift_staff = new HashMap<String, Shift_Person>();
for(String a_shift: Arrays.copyOfRange(new_headers, 4, new_headers.length)){
String day_hour_shift = a_shift;
int hours_of_work = calc_hours_shift(a_shift);
ArrayList<Person> a_staff_person = new ArrayList<Person>();
Collections.sort(staff);
for(int i = 0; i<staff_for_swift; i++){
for(Person a_staff: staff){
if(a_staff_person.size()<=0 && (a_staff.hours_worked+hours_of_work)<a_staff.hours_available){
staff.get(staff.indexOf(a_staff)).add_hours_worked(hours_of_work);
a_staff.add_shift(a_shift);
a_staff_person.add(a_staff);
}else{
ArrayList<String> level_occ = new ArrayList<String>();
for(Person tmp: a_staff_person){
level_occ.add(tmp.rank);
}
if(!level_occ.contains(a_staff.rank) && (a_staff.hours_worked+hours_of_work)<a_staff.hours_available){
staff.get(staff.indexOf(a_staff)).add_hours_worked(hours_of_work);
a_staff.add_shift(a_shift);
a_staff_person.add(a_staff);
}
}
}
}
Person[] arr = a_staff_person.toArray(new Person[0]);
SumIntSess.Shift_Person a_shift_person = new SumIntSess().new Shift_Person(a_shift, hours_of_work, arr);
shift_staff.put(a_shift, a_shift_person);
}
ArrayList<String[]> new_shifts = new ArrayList<String[]>();
new_shifts.add(new_headers);
for(Person ap: staff){
String[] a_line = new String[144];
a_line[0] = ap.get_full_name();
a_line[1] = ""+ap.get_rank();
a_line[2] = ""+ap.get_hours_worked();
a_line[3] = ""+ap.get_max_hours_available();
for(int i = 4; i<144; i++){
a_line[i] = "Not Scheduled";
if(ap.get_shifts_worked().contains(new_headers[i])){
a_line[i] = "Scheduled";
}
}
new_shifts.add(a_line);
}
Collections.reverse(new_shifts);
new_shifts.add(0, new_headers);
new_shifts.remove(new_shifts.size()-1);
writer.writeAll(new_shifts);
writer.close();
}
public class Shift_Person{
String shift;
int hours_shift;
Person[] staff_person = new Person[staff_for_swift];
public Shift_Person(String shift, int hours_shift, Person[] staff_person){
this.shift = shift;
this.hours_shift = hours_shift;
this.staff_person = staff_person;
}
public String get_shift(){
return shift;
}
public int get_hours_shift(){
return hours_shift;
}
public Person[] get_staff(){
return staff_person;
}
public int available_employ(){
int available = -1;
for(int i=staff_person.length-1; i>=0; i--){
if(staff_person[i]==null){
available = i;
}
}
return available;
}
public void add_person(Person new_person){
if(this.available_employ()>=0){
staff_person[this.available_employ()] = new_person;
}
}
}
public class Person implements Comparable<Person>{
String full_name;
int hours_available;
int hours_worked;
String rank;
ArrayList<String> shift_work;
public Person(String full_name, int hours_available, String rank){
this.full_name = full_name;
this.hours_available = hours_available;
this.rank = rank;
this.hours_worked = 0;
this.shift_work = new ArrayList<String>();
}
public String get_full_name(){
return full_name;
}
public int get_max_hours_available(){
return hours_available;
}
public String get_rank(){
return rank;
}
public int get_hours_worked(){
return hours_worked;
}
public ArrayList<String> get_shifts_worked(){
return shift_work;
}
public void add_hours_worked(int to_add){
this.hours_worked = hours_worked + to_add;
}
public void add_shift(String a_shift){
this.shift_work.add(a_shift);
}
public int compareTo(Person p1){
int sComp = this.rank.compareTo(p1.get_rank());
if (sComp != 0){
return sComp;
}
else{
double ratio1 = (double)this.hours_worked/(double)this.hours_available;
double ratio2 = (double)p1.hours_worked/(double)p1.hours_available;
if(ratio1>ratio2){return 1;}
if(ratio1<ratio2){return -1;}
}
return 0;
}
}
static int calc_hours_shift(String a_shift){
int hours_shift = 0;
String str_from = a_shift.substring(a_shift.indexOf("[")+1, a_shift.indexOf(" - "));
int int_from = Integer.parseInt(str_from.substring(0, str_from.indexOf("M")-1));
String str_to = a_shift.substring(a_shift.indexOf(" - ")+3, a_shift.indexOf("]"));
int int_to = Integer.parseInt(str_to.substring(0, str_to.lastIndexOf("M")-1));
if(str_from.indexOf("12AM")>=0){
int_from = 0;
}
if(str_from.indexOf("PM")>=0 && str_from.indexOf("12PM")<0){
int_from += 12;
}
if(str_to.indexOf("12AM")>=0){
int_to = 24;
}
if(str_to.indexOf("PM")>=0 && str_to.indexOf("12PM")<0){
int_to += 12;
}
if(str_from.indexOf("PM")>=0 && str_to.indexOf("AM")>=0 && int_to<24){
int_to += 24;
}
hours_shift = (int_to-int_from);
return hours_shift;
}
static int calc_hours(String[] shifts){
int hours_shifts = 0;
for(String a_shift: shifts){
hours_shifts += calc_hours_shift(a_shift);
}
return hours_shifts;
}
static void p(Object l, boolean newLine){
if(newLine){
System.out.println(l.toString());
}
else System.out.print(l.toString());
}
static void p(Object l){
System.out.println(l.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment