我有一个通过多种方式参与的项目,但是我似乎做得不好。它是一个Android应用程序的一部分,该应用程序仅记录时钟打卡并保存到外部存储中的txt文件中。 txt文件的格式如下:
PUNCHIN星期三05-15下午03:50
PUNCHOUT星期三05-15下午03:50
PUNCHIN星期三05-15下午03:50
PUNCHOUT星期三05-15下午03:50
PUNCHIN星期三05-15下午03:50
PUNCHOUT星期三05-15下午03:50
PUNCHIN星期三05-15下午03:50
PUNCHOUT星期三05-15下午03:50
它将记录周日至周六的出入。
应用读取文件时,会创建一个数组列表,其中的每一行都是
。我有另一个数组列表,该列表与定义为浮点数的每个数组元素相对应,该浮点数表示该时刻的工作时间。例如,元素1将为15.83,依此类推。
然后它将调用一种方法,以从奇数元素中减去偶数元素,并给出当天工作了多少小时的总数。
在这一点上,我意识到我的方法有些偏离。我只想获取从文本文件构建的数组列表,然后计算每天要工作多少小时。
每个人对此的最佳方法是什么?
在这种方法下,我有很多数组列表和子字符串字符串,无穷无尽的循环和许多意大利面条式代码。
任何人的前瞻感激。谢谢,乔什!
答案 0 :(得分:0)
这是我用来解决此任务的功能:
public void calculate (View v) {
try {
String view_data = ((TextView) findViewById(R.id.history_view)).getText().toString();
String[] data_array;
data_array = view_data.split("\n");
ArrayList<String> time_punches = new ArrayList<String>();
// create arrayList of full data line
for (int a = 0; a <= data_array.length - 1; a++) {
if (!data_array[a].equals("")) {
if (!data_array[a].equals("UPS")) {
if (!data_array[a].equals("Turkey Hill")) {
time_punches.add(data_array[a]);
}
}
}
}
// create another arrayList of just the times, in string format
ArrayList<String> nextList = new ArrayList<String>();
for (int er = 0; er <= time_punches.size() - 1; er++) {
if (time_punches.get(er).substring(0, 7).equals("PUNCHIN")) {
nextList.add(time_punches.get(er).substring(18, 23));
}
if (time_punches.get(er).substring(0, 8).equals("PUNCHOUT")) {
nextList.add(time_punches.get(er).substring(19, 24));
}
}
// create another arrayList, converting string time format to floats
int num_a;
float num_b;
float num_c;
float num_d;
ArrayList<Float> anotherList = new ArrayList<Float>();
float[] floatArray = new float[nextList.size()];
for (int _x = 0; _x <= nextList.size() - 1; _x++) {
num_a = Integer.parseInt(nextList.get(_x).substring(0, 2));
num_b = Integer.parseInt(nextList.get(_x).substring(3, 5));
num_a = (num_a) + 12;
num_b = (num_b) / 60;
num_c = (num_a) + (num_b);
anotherList.add(num_c);
num_a = 0;
num_b = 0;
num_c = 0;
num_d = 0;
}
// create yet again, another arrayList to calcuate the time punches grouped by two
ArrayList<Float> calcList = new ArrayList<Float>();
float num_e;
int num_f;
for (int _z = 0; _z <= anotherList.size() - 1; _z++) {
num_f = (_z) + 1;
if ((_z % 2) == 0) {
calcList.add(anotherList.get(num_f) - anotherList.get(_z));
}
}
// AT THIS POINT calcList contains the hours worked per day.
// Now do something useful with this data (Hours worked per day)
float all_hours = 0;
for (int _y = 0; _y <= calcList.size() - 1; _y++) {
all_hours += calcList.get(_y);
}
ArrayList<String> pretty_list = new ArrayList<String>();
for (int _a = 0; _a <= time_punches.size() - 1; _a++) {
if ((_a % 2) == 0) {
pretty_list.add(time_punches.get(_a).substring(8, 18));
}
}
ArrayList<String> pretty2 = new ArrayList<String>();
for (int _s = 0; _s <= pretty_list.size() - 1; _s++) {
pretty2.add(pretty_list.get(_s) + ": " + Float.toString(calcList.get(_s)) + "hours");
}
pretty2.add("\nTotal Hours Worked: " + Float.toString(all_hours) + "hours");
String final_output_string = "";
for (int _e = 0; _e <= pretty2.size() - 1; _e++) {
final_output_string = final_output_string + pretty2.get(_e) + "\n";
}
// final_output_string is the end result, time_punches contains the raw data, calclist contains the floats of the raw data,
// allhours is the float sum of all hours worked. Next, just set text to my textview and hope the user doent click the button twice! :)
// oh, yeah. and all of this is assuming that all hours worked are in the PM. heh... version 2 will be better, hehe
((TextView) findViewById(R.id.history_view)).setText(final_output_string);
alert("Punch Log Converted To Calculated Hours");
} catch (Exception e) {
alert("You May Still Be Punched In! Reload Time Punches!");
}
}