java面试题
某公司的java面试题(本人菜鸟一枚 发出来集思广益 供交流参考 第一次自己写 有点小激动 还请大佬留下宝贵建议)
package day14;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
/**
* @author Tony
* @create 2021-08-20 17:07
*/
public class Work1 {
public String generateSummary(String input) {
int totalIncome = 0;//总收入
int dayIncome = 0;//当天收入
int payMent = 0;//总支出
int daypayment = 0;//当天支出
int profit = 0;//总结余
int dayprofit = 0;//当天结余
StringBuilder totalDayInfo = new StringBuilder();//输出总的信息
String regex = " ";
String[] info = input.split(regex);
ArrayList<String> date = new ArrayList<String>();//存储日期
ArrayList<String> hour = new ArrayList<String>();//存储时间段
ArrayList<String> numofpeople = new ArrayList<String>();//存储人数
for (int i = 0; i < info.length; i++) {
if ((i + 1) % 3 == 0) {
numofpeople.add(info[i]);
} else if ((i + 1) % 3 == 2) {
hour.add(info[i]);
} else if ((i + 1) % 3 == 1) {
date.add(info[i]);
}
}
//获取每天人数 转化为int类型方便后面使用
int[] daynumpeople = new int[numofpeople.size()];
for (int i = 0; i < daynumpeople.length; i++) {
daynumpeople[i] = Integer.parseInt(numofpeople.get(i));
}
//方法内部类
class Method {
//订场方法
public int bookPolicy(int numpeople) {
int T = 0;
int X = numpeople % 6;
if (numpeople < 4) {
} else if (numpeople <= 6) {
T = 1;
} else if ( numpeople <= 12) {
T = 2;
} else if (numpeople / 6 == 2 || numpeople / 6 == 3) {
if (X >= 4 && numpeople / 6 == 2) {
T = 3;
} else if (X >= 4 && numpeople / 6 == 3) {
T = 4;
} else if (numpeople / 6 == 2) {
T = 2;
} else if (numpeople / 6 == 3) {
T = 3;
}
} else if (numpeople / 6 > 3) {
T = numpeople / 6;
}
return T;
}
//收费方法
public int charge(int dayvalue, int starthour, int endhour) {
int chargesite = 0;//每台收的钱
int chargesite1 = 0;
int chargesite2 = 0;
if (dayvalue >= 1 && dayvalue <= 5) {
if (starthour >= 9 && endhour <= 12) {
chargesite = (endhour - starthour) * 30;
} else if (starthour >= 12 && endhour <= 18) {
chargesite = (endhour - starthour) * 40;
} else if (starthour >= 18 && endhour <= 20) {
chargesite = (endhour - starthour) * 80;
} else if (starthour >= 20 && endhour <= 22) {
chargesite = (endhour - starthour) * 60;
} else {
if(starthour<12){
chargesite1 = (12 - starthour) * 30;
chargesite2 = (endhour - 12) * 40;
chargesite = chargesite1 + chargesite2;
}else if (starthour<18){
chargesite1 = (18 - starthour) * 40;
chargesite2 = (endhour - 18) * 80;
chargesite = chargesite1 + chargesite2;
}else if(starthour<20){
chargesite1 = (20 - starthour) * 80;
chargesite2 = (endhour - 20) * 60;
}
}
} else {
if (starthour >= 9 && endhour <= 12) {
chargesite = (endhour - starthour) * 40;
} else if (starthour >= 12 && endhour <= 18) {
chargesite = (endhour - starthour) * 50;
} else if (starthour >= 16 && endhour <= 22) {
chargesite = (endhour - starthour) * 60;
} else {
if(starthour<12&&endhour>12){
chargesite1 = (12 - starthour) * 40;
chargesite2 = (endhour - 12) * 50;
chargesite = chargesite1 + chargesite2;
}else if(starthour<18&&endhour>18){
chargesite1 = (18 - starthour) * 50;
chargesite2 = (endhour - 18) * 60;
}
}
}
return chargesite;
}
}
Method m = new Method();//方法对象
String regex1 = " ";
String regex2 = "~";
StringBuilder newHour = new StringBuilder();
for (String s : hour) {
String temp = s + " ";
newHour.append(temp);
}
String[] splitHour = newHour.toString().split(regex1);
for (int i = 0; i < splitHour.length; i++) {//每天的信息
String[] split = splitHour[i].split(regex2);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String temp = date.get(i) + " " + split[0];
String temp1 = date.get(i) + " " + split[1];
TemporalAccessor parse = pattern.parse(temp);//将日期与小时段拼接 方便获取计算
TemporalAccessor parse1 = pattern.parse(temp1);
LocalDateTime from = LocalDateTime.from(parse);
LocalDateTime from1 = LocalDateTime.from(parse1);
int dayvalue = from.getDayOfWeek().getValue();//获取星期几
dayIncome = daynumpeople[i] * 30;
int i1 = m.bookPolicy(daynumpeople[i]);
int charge = m.charge(dayvalue, from.getHour(), from1.getHour());
if (daynumpeople[i] < 4) {
dayIncome = 0;
dayprofit = 0;
daypayment = 0;
String dayinfo = "" + date.get(i) + " " + hour.get(i) + " +" + dayIncome + " -" + daypayment + " " + dayprofit + "!";
totalDayInfo.append(dayinfo);
continue;
}
daypayment = i1 * charge;
totalIncome = totalIncome + dayIncome;
payMent = payMent + daypayment;
profit = totalIncome - payMent;
dayprofit = dayIncome - daypayment;
if(dayprofit<0){
String dayinfo = "" + date.get(i) + " " + hour.get(i) + " +" + dayIncome + " -" + daypayment + " " + dayprofit + "!";
totalDayInfo.append(dayinfo);
continue;
}
if(i==splitHour.length-1){//防止在最后一次拼接的时候+上"!"导致后面切割出现换行
String dayinfo = "" + date.get(i) + " " + hour.get(i) + " +" + dayIncome + " -" + daypayment + " -" + dayprofit;
totalDayInfo.append(dayinfo);//将每天信息拼接
break;
}
String dayinfo = "" + date.get(i) + " " + hour.get(i) + " +" + dayIncome + " -" + daypayment + " -" + dayprofit + "!";
totalDayInfo.append(dayinfo);//将每天信息拼接
}
totalDayInfo.append("!").append("Total Income:").append(totalIncome).append("!").append("Total Payment:").append(payMent).append("!").append("Profit:").append(profit);
return totalDayInfo.toString();
}
@Test
//单元测试方法
public void test1() {
String s = generateSummary(
"2016-06-02 20:00~22:00 7 " +
"2016-06-03 09:00~12:00 14 " +
"2016-06-04 14:00~17:00 22 " +
"2016-06-05 19:00~22:00 3 " +
"2016-06-06 12:00~15:00 15 " +
"2016-06-07 15:00~17:00 12 " +
"2016-06-08 10:00~13:00 19 " +
"2016-06-09 16:00~18:00 16 " +
"2016-06-10 20:00~22:00 5 " +
"2016-06-11 13:00~15:00 11 ");
String regex = "!";
String[] split = s.split(regex);
for (String s1 : split) {
System.out.println(s1);
}
}
}