本文共 2423 字,大约阅读时间需要 8 分钟。
import java.text.SimpleDateFormat;import java.util.Calendar;public class Test { public static int dayForWeek(String pTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if(c.get(Calendar.DAY_OF_WEEK) == 1){ dayForWeek = 7; }else{ dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } return dayForWeek; } public String generateSummary(String input){ String[] arr = input.split(" "); int peopleNum = Integer.parseInt(arr[2]); int yard = 0; int T = peopleNum / 6; int X = peopleNum % 6; if(T == 0 && X < 4){ yard = 0; }else if(T == 0 && X >= 4){ yard = 1; }else if(T == 1){ yard = 2; }else if((T == 2 || T == 3) && X >= 4){ yard = T + 1; }else { yard = T; } int weekday = -1; try { weekday = dayForWeek(arr[0]); } catch (Exception e) { e.printStackTrace(); } //System.out.println(weekday); int[][] charge = new int[8][24]; for(int i = 1; i <= 5; i++){ charge[i][9] = 30; charge[i][10] = 30; charge[i][11] = 30; charge[i][12] = 50; charge[i][13] = 50; charge[i][14] = 50; charge[i][15] = 50; charge[i][16] = 50; charge[i][17] = 50; charge[i][18] = 80; charge[i][19] = 80; charge[i][20] = 60; charge[i][21] = 60; } for(int i = 6; i <= 7; i++){ charge[i][9] = 40; charge[i][10] = 40; charge[i][11] = 40; charge[i][12] = 50; charge[i][13] = 50; charge[i][14] = 50; charge[i][15] = 50; charge[i][16] = 50; charge[i][17] = 50; charge[i][18] = 60; charge[i][19] = 60; charge[i][20] = 60; charge[i][21] = 60; } int[] timezone = new int[2]; String[] tempsplit = arr[1].split("~"); timezone[0] = Integer.parseInt(tempsplit[0].split(":")[0]); timezone[1] = Integer.parseInt(tempsplit[1].split(":")[0]); //System.out.println(timezone[0]+"--"+timezone[1]); int income = peopleNum * 30; int outcome = 0; for(int i = timezone[0]; i < timezone[1]; i++) { outcome += charge[weekday][i]; } outcome *= yard; if(yard == 0){ income = 0; } char lastchar = ((income - outcome)>0) ? '+' : '-'; int lastval = Math.abs(income - outcome); String output = arr[0] + " " + arr[1] + " " + "+" + income + " " + "-" + outcome + " " + lastchar + lastval; System.out.println(output); return output; } public static void main(String[] args) { Test t = new Test(); t.generateSummary("2016-06-02 20:00~22:00 7"); }}