Bonjour,
je veux mettre ce code dans un comportement d'agent
package pack;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class A1 {
static String Newligne = System.getProperty("line.separator");
public static void main(String[] args) throws Exception {
Class.forName("org.relique.jdbc.csv.CsvDriver");
String csvFile = "d:/fichier.txt";
String csvFile1 = "C:\\csv\\csv.txt";
List<LineFile> list;
try {
list = ListFromTXT(csvFile);
TXTFromList(list, csvFile1);
List<LineFile> list1 = ListFromTXT(csvFile1);
for (int i = 0; i < list1.size(); i++) {
LineFile lf = list1.get(i);
System.out.println(lf.getAttribute5() + " + "
+ lf.getAttribute6() + " = " + lf.getAttribute9());
System.out.println(lf.getAttribute5() + " + "
+ lf.getAttribute8() + " = " + lf.getAttribute10());
System.out.println(lf.getAttribute7() + " + "
+ lf.getAttribute9() + " = " + lf.getAttribute11());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void TXTFromList(List<LineFile> liste_files, String file_name)
throws IOException {
File fichier_clt = new File(file_name);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
fichier_clt, true));
bufferedWriter.append("id,origin,destination,capacite,eo,Em,DTM,DTD,lo,ed,ld");
for (LineFile lfile : liste_files) {
String cont = "";
cont = cont + lfile.getAid() + ",";
cont = cont + lfile.getAttribute2() + ",";
cont = cont + lfile.getAttribute3() + ",";
cont = cont + lfile.getAttribute4() + ",";
cont = cont + lfile.getAttribute5() + ",";
cont = cont + lfile.getAttribute6() + ",";
cont = cont + lfile.getAttribute7() + ",";
cont = cont + lfile.getAttribute8() + ",";
cont = cont + lfile.getAttribute9() + ",";
cont = cont + lfile.getAttribute10() + ",";
cont = cont + lfile.getAttribute11();
cont = cont + Newligne;
bufferedWriter.write(cont);
}
bufferedWriter.close();
}
public static List<LineFile> ListFromTXT(String file_name)
throws IOException {
BufferedReader br = null;
String line = "";
ArrayList<LineFile> files = new ArrayList<LineFile>();
try {
br = new BufferedReader(new FileReader(file_name));
int cpt = 0;
while ((line = br.readLine()) != null) {
if (cpt > 0) {
String[] ls = line.split(",");
LineFile lineFile = new LineFile();
lineFile.setAttribute1(ls[0]);
lineFile.setAttribute2(ls[1]);
lineFile.setAttribute3(ls[2]);
lineFile.setAttribute4(ls[3]);
lineFile.setAttribute5(ls[4]);
lineFile.setAttribute6(ls[5]);
lineFile.setAttribute7(ls[6]);
lineFile.setAttribute8(ls[7]);
//
DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH:mm");
DateFormat df1 = new SimpleDateFormat("HH:mm");
Calendar c1 = Calendar.getInstance();
String date1 = lineFile.getAttribute5();
try {
c1.setTime(df.parse(date1));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar c2 = Calendar.getInstance();
String time = lineFile.getAttribute6();
try {
c2.setTime(df1.parse(time));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar c5 = Calendar.getInstance();
String ddd = lineFile.getAttribute7();
try {
c5.setTime(df1.parse(ddd));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar c3 = Calendar.getInstance();
String aaa = lineFile.getAttribute8();
try {
c3.setTime(df1.parse(aaa));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cTotal = (Calendar) c1.clone();
cTotal.add(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
cTotal.add(Calendar.MINUTE, c2.get(Calendar.MINUTE));
String somme = df.format(cTotal.getTime());
System.out.println(somme);
Calendar cTotal1 = (Calendar) c1.clone();
cTotal1.add(Calendar.HOUR_OF_DAY, c3.get(Calendar.HOUR_OF_DAY));
cTotal1.add(Calendar.MINUTE, c3.get(Calendar.MINUTE));
String somme2 = df.format(cTotal1.getTime());
System.out.println(somme2);
lineFile.setAttribute9(somme );
lineFile.setAttribute10(somme2 );
Calendar c9 = Calendar.getInstance();
String uuu = lineFile.getAttribute9();
try {
c9.setTime(df.parse(uuu));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cTotal9 = (Calendar) c9.clone();
cTotal9.add(Calendar.HOUR_OF_DAY, c5.get(Calendar.HOUR_OF_DAY));
cTotal9.add(Calendar.MINUTE, c5.get(Calendar.MINUTE));
String rim = df.format(cTotal9.getTime());
System.out.println(rim);
lineFile.setAttribute11(rim );
files.add(lineFile);
}
cpt++;
}
return files;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}