java小程序之随机任务发布器
需求
- 自定义任务
- 随机定时
- 随机排序
- 集合输出
搭建
- 简单项目,所以我使用java 的maven来搭建一个控制台程序,并且使用excel来记录任务
- 创建任务类(
sub
)
package com.micah;
import lombok.Data;
@Data
public class Sub {
private String name;
private Integer id;
private Integer studentTime;
public Sub(String name, Integer id, Integer studentTime) {
this.name = name;
this.id = id;
this.studentTime = studentTime;
}
}
- 使用poi-ooxml包实现读取excel文件
- 导入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
下面是工具类
package com.micah;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
public class ExcelUtil {
private XSSFSheet sheet;
/**
* 构造函数,初始化excel数据
* @param filePath excel路径
* @param sheetName sheet表名
*/
ExcelUtil(String filePath,String sheetName){
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
//获取sheet
sheet = sheets.getSheet(sheetName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据行和列的索引获取单元格的数据
* @param row
* @param column
* @return
*/
public String getExcelDateByIndex(int row,int column){
XSSFRow row1 = sheet.getRow(row);
String cell = row1.getCell(column).toString();
return cell;
}
/**
* 根据某一列值为“******”的这一行,来获取该行第x列的值
* @param caseName
* @param currentColumn 当前单元格列的索引
* @param targetColumn 目标单元格列的索引
* @return
*/
public String getCellByCaseName(String caseName,int currentColumn,int targetColumn){
String operateSteps="";
//获取行数
int rows = sheet.getPhysicalNumberOfRows();
for(int i=0;i<rows;i++){
XSSFRow row = sheet.getRow(i);
String cell = row.getCell(currentColumn).toString();
if(cell.equals(caseName)){
operateSteps = row.getCell(targetColumn).toString();
break; }
}
return operateSteps;
}
public Integer getRow(){
return sheet.getPhysicalNumberOfRows();
}
//打印excel数据
public void readExcelData(){
//获取行数
int rows = sheet.getPhysicalNumberOfRows();
for(int i=0;i<rows;i++){
//获取列数
XSSFRow row = sheet.getRow(i);
int columns = row.getPhysicalNumberOfCells();
for(int j=0;j<columns;j++){
String cell = row.getCell(j).toString();
System.out.println(cell);
}
}
}
}
- 使用demo
package com.micah;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Demo {
@Test
public void test() {
ExcelUtil sheet1 = new ExcelUtil("src/main/resources/micah学习日程.xlsx", "study");
//获取第二行第4列
int arr[]={30,60,90,120};
Random random=new Random();
List<Sub> list=new ArrayList<>();
for (int i = 0; i < sheet1.getRow()-1; i++) {
list.add(new Sub(sheet1.getExcelDateByIndex(i+1, 1),i,arr[random.nextInt(3)]));
}
//根据第3列值为“customer23”的这一行,来获取该行第2列的值
// String cell3 = sheet1.getCellByCaseName("customer23", 2,1);
Collections.shuffle(list);
azaString(list);
// System.out.println(cell3);
}
public void azaString(List<Sub> list){
for (Sub sub:list) {
System.out.println("接下来要干的事是: "+sub.getName()+" 时间是:"+sub.getStudentTime());
}
}
}
-
excel定义
待完成学习项目 待完成学习项目 网络安全 网络安全 绘画+板绘 绘画+板绘 xxx xxx