要求
1.读取csv文件
2.按照商品名以及日期排序
3.并得到每种商品每天的贩卖合计
4.输出新csv文件
最终代码
package zohing;
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.util.ArrayList;
public class level2 {
public static String addInteger(String s1,String s2) {//整数大数相加
StringBuffer result = new StringBuffer();
//反转字符串,便于从低位相加
s1=new StringBuffer(s1).reverse().toString();
s2=new StringBuffer(s2).reverse().toString();
int len1=s1.length();
int len2=s2.length();
int maxlen=len1>len2?len1:len2;
//不足位补0,对齐数字
if(len1>len2){
for(int i=len2;i<len1;i++)
s2+="0";
}else if (len1<len2) {
for(int i=len1;i<len2;i++)
s1+="0";
}
boolean overflow=false;//溢出标志位
int takeflow=0;//进位标志位
//逐位相加
for(int i=0;i<maxlen;i++){
if(i==maxlen-1){
int sum=Integer.parseInt(s1.charAt(i)+"")+Integer.parseInt(s2.charAt(i)+"")+takeflow;
if(sum>9){
result.append(sum-10);
takeflow=1;
overflow=true;
}else {
result.append(sum);
takeflow=0;
}
}else {
int sum=Integer.parseInt(s1.charAt(i)+"")+Integer.parseInt(s2.charAt(i)+"")+takeflow;
if(sum>9){
result.append(sum-10);
takeflow=1;
}else {
result.append(sum);
takeflow=0;
}
}
}
if(overflow){
result.append(1);
}
return result.reverse().toString();
}
/**小数大数相加:
* 思路:
* 把两个数相加转成两个数的整数部分相加和两个数的小数部分相加*/
static String addDecimal(String s1, String s2) {
int dindex1=s1.indexOf('.');//找出两个数的小数点位置,用于分割数字
int dindex2=s2.indexOf('.');
String z1 = "";//第一个数的整数部分
String z2 = "";//第二个数的整数部分
String d1 = "";//第一个数的小数部分
String d2 = "";
StringBuffer result = new StringBuffer();//存放结果
if(dindex1!=-1){//第一个数没有小数部分
z1=s1.substring(0, dindex1);
d1=s1.substring(dindex1+1);
}else{//有
z1=s1;
d1="";
}
if(dindex2!=-1){
z2=s2.substring(0, dindex2);
d2=s2.substring(dindex2+1);
}else{
z2=s2;
d2="";
}
result.append(addInteger(z1, z2));//两个数的整数部分相加结果
//求出两个数小数部分较长的一个的长度,可用于判断小数部分相加后有无进位
int maxlen=d1.length()>d2.length()?d1.length():d2.length();
for(int i = d1.length();i < maxlen;i++){
d1+="0";
}
for(int i = d2.length();i < maxlen;i++){
d2+="0";
}
String dres = addInteger(d1, d2);//小数部分相加结果
if (maxlen<dres.length()) {//小数部分相加后有进位
result=new StringBuffer(addInteger(result.toString(), "1"));
if (dindex1!=-1||dindex2!=-1) {//两个数中至少有一个带小数
result.append(".");
}
dres=dres.substring(1);
result.append(dres);
} else {//小数部分相加后无进位
if (dindex1!=-1||dindex2!=-1) {
result.append(".");
}
result.append(dres);
}
return result.toString();
}
//快速排序
public static void quick(ArrayList<String[]> pData, int left, int right) {
int i, j;
String[] temp, first;
i = left;
j = right;
first = pData.get(left);
// 一趟快速排序
while (true) {
// 从第二个数开始找大于中枢的数 ,从前面开始找大于pData[left]的数
while ((++i) < right - 1 && pData.get(i)[2].compareTo(first[2]) < 0)
;
// 从最后一个数开始找第一个小于中枢pData[left]的数
while ((--j) > left && pData.get(j)[2].compareTo(first[2]) > 0)
;
if (i >= j)
break;
// 交换两边找到的数
temp = pData.get(i);
pData.set(i, pData.get(j));
pData.set(j, temp);
}
// 交换中枢
pData.set(left, pData.get(j));
pData.set(j, first);
// 递归快排中枢前边的数据
if (left < j)
quick(pData, left, j);
// 递归快排中枢后边的数据
if (right > i)
quick(pData, i, right);
}
//冒泡排序
public static void order(ArrayList<String[]> save){
// 对商品名进行冒泡排序
for (int i = 1; i < save.size(); i++) {
for (int j = 1; j < save.size() - i; j++) {
if (save.get(j)[1].compareTo(save.get(j + 1)[1]) > 0) {
String[] tmp;
tmp = save.get(j);
save.set(j, save.get(j + 1));
save.set(j + 1, tmp);
}
}
}
}
//按商品名販売額合計
public static void nameCostOrder(ArrayList<String[]> save,ArrayList<String[]> save2){
String temp = save.get(1)[1];
String cost = "0";
int left = 1, right = 0;
// 对不同类商品进行販売額合計
for (int i = 1; i < save.size(); i++) {
if (save.get(i)[1].equals(temp)) {
cost = new level2().addDecimal(cost, save.get(i)[4]);
right = i;
} else {
save2.add(new String[] { String.valueOf(save2.size()), temp, cost });
if (left < right)
new level2().quick(save, left, right);
left = i;
temp = save.get(i)[1];
cost = save.get(i)[4];
}
if (i == save.size() - 1) {
save2.add(new String[] { String.valueOf(save2.size()), temp, cost });
if (left < right)
new level2().quick(save, left, right);
}
}
}
//按商品名及日付排序后做販売額合計
public static void sum(ArrayList<String[]> save,ArrayList<String[]> save3){
String temp1 = save.get(1)[1], temp2 = save.get(1)[2];
String cost1 = "0", cost2 = "0";
// 对不同类商品以及不同日付进行排序及販売額合計
for (int i = 1; i < save.size(); i++) {
if (save.get(i)[1].equals(temp1) && save.get(i)[2].equals(temp2)) {// 商品名和日付相同时,販売額合計cost1
cost1 = new level2().addDecimal(cost1, save.get(i)[4]);
} else {
save3.add(new String[] { String.valueOf(save3.size()), temp1, temp2, cost1 });
cost2 = new level2().addDecimal(cost1, cost2);// 所有商品名相同时,販売額合計cost2
if (save.get(i)[1].equals(temp1) == false) {
save3.add(new String[] { String.valueOf(save3.size()), temp1, " ", cost2 });
cost2 = "0";// 商品名不同时,对cost2清零,重新计算下一个商品名同一天販売額合計
}
temp1 = save.get(i)[1];
temp2 = save.get(i)[2];
cost1 = save.get(i)[4];
}
if (i == save.size() - 1) {
save3.add(new String[] { String.valueOf(save3.size()), temp1, temp2, cost1 });
cost2 = new level2().addDecimal(cost1, cost2);
save3.add(new String[] { String.valueOf(save3.size()), temp1, " ", cost2 });
}
}
}
public static void all(){
String rdAdd = "D:\\tempProj\\input333.csv";
String wrAdd = "D:\\tempProj\\output.csv";
File in = new File(rdAdd);
File out = new File(wrAdd);
ArrayList<String[]> save = new ArrayList<String[]>();
try {
BufferedReader reader = new BufferedReader(new FileReader(in));
BufferedWriter writer = new BufferedWriter(new FileWriter(out));
String tempString = "";
// readeLine():一次读取一行。根据\r\n判断是否结束,只返回内容,不返回换行符
while ((tempString = reader.readLine()) != null) {
save.add(tempString.split(","));
}
reader.close();
order(save);
ArrayList<String[]> save2 = new ArrayList<String[]>();
save2.add(new String[] { "NO", "商品名", "販売額合計" });// save2为level1的任务输出,level2排序合計也会用到
ArrayList<String[]> save3 = new ArrayList<String[]>();
save3.add(new String[] { "NO", "商品名", "日付", "販売額合計" });
nameCostOrder(save,save2);
sum(save,save3);
for (int i = 0; i < save3.size(); i++) {
// 写入
for (int j = 0; j < save3.get(i).length; j++) {
writer.write(save3.get(i)[j] + ',');
System.out.println(save3.get(i)[j]);
}
writer.newLine();
}
writer.close();
} catch (FileNotFoundException ex) {
System.out.println("file not found");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
all();
}
}
junit测试
package zohing;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
class level2Test extends level2{
@BeforeEach
void setUp() {
System.out.println("开始单元测试");
}
@AfterEach
void tearDown() {
System.out.println("结束单元测试");
}
@Test
void addInteger() {
assertEquals("3",addInteger("1","2"));
System.out.println("商品总额大数相加测试成功");
}
@Test
void addDecimal() {
assertEquals("3.531",addDecimal("1.201","2.33"));
System.out.println("商品总额大数相加(小数)测试成功");
}
@Test
void quick() {
ArrayList<String[]> p = new ArrayList<String[]>();
p.add(new String[]{"2","1","5"});
p.add(new String[]{"3","1","4"});
p.add(new String[]{"2","3","9"});
level2.quick(p,0,2);
ArrayList<String[]> p1 = new ArrayList<String[]>();
p1.add(new String[]{"3","1","4"});
p1.add(new String[]{"2","1","5"});
p1.add(new String[]{"2","3","9"});
level2.quick(p1,0,2);
for(int i =0;i<2;i++){
assertEquals(Arrays.toString(p.get(i)), Arrays.toString(p1.get(i)));
}
System.out.println("按日期排序调整整行排列测试成功");
}
@Test
void order(){
ArrayList<String[]> p = new ArrayList<String[]>();
p.add(new String[]{"3","1","4"});
p.add(new String[]{"1","3","9"});
p.add(new String[]{"2","5","5"});
p.add(new String[]{"4","4","4"});
level2.order(p);
ArrayList<String[]> p1 = new ArrayList<String[]>();
p1.add(new String[]{"3","1","4"});
p1.add(new String[]{"2","5","5"});
p1.add(new String[]{"4","4","4"});
p1.add(new String[]{"1","3","9"});
level2.order(p1);
for(int i =0;i<2;i++){
assertEquals(Arrays.toString(p.get(i)), Arrays.toString(p1.get(i)));
}
System.out.println("按商品名排序调整整行排列测试成功");
}
@Test
void nameCostOrder(){
ArrayList<String[]> p = new ArrayList<String[]>();
p.add(new String[]{"NO","商品名","日付","販売先","販売額合計"});
p.add(new String[]{"3","1","4","2","1"});
p.add(new String[]{"10","2","9","5","6"});
p.add(new String[]{"22","2","5","4","8"});
p.add(new String[]{"4","4","41","19","9"});
ArrayList<String[]> q = new ArrayList<>();
q.add(new String[]{"NO","商品名","販売額合計"});
level2.nameCostOrder(p,q);
ArrayList<String[]> p1 = new ArrayList<String[]>();
p1.add(new String[]{"NO","商品名","日付","販売先","販売額合計"});
p1.add(new String[]{"3","1","4","2","1"});
p1.add(new String[]{"2","2","15","44","8"});
p1.add(new String[]{"9","2","9","5","6"});
p1.add(new String[]{"1","14","4","11","9"});
ArrayList<String[]> q1 = new ArrayList<>();
q1.add(new String[]{"NO","商品名","販売額合計"});
level2.nameCostOrder(p1,q1);
for(int i =0;i<q.size()-1;i++){
assertEquals(Arrays.toString(q.get(i)), Arrays.toString(q1.get(i)));
}
System.out.println("相同商品名总额合计测试成功");
}
@Test
void sum(){
ArrayList<String[]> p = new ArrayList<String[]>();
p.add(new String[]{"NO","商品名","日付","販売先","販売額合計"});
p.add(new String[]{"3","1","4","2","1"});
p.add(new String[]{"10","2","5","5","6"});
p.add(new String[]{"22","2","5","4","8"});
p.add(new String[]{"4","2","41","19","9"});
ArrayList<String[]> q = new ArrayList<>();
q.add(new String[]{"NO","商品名","日付","販売額合計"});
level2.sum(p,q);
ArrayList<String[]> p1 = new ArrayList<String[]>();
p1.add(new String[]{"NO","商品名","日付","販売先","販売額合計"});
p1.add(new String[]{"3","1","4","2","1"});
p1.add(new String[]{"2","2","5","44","8"});
p1.add(new String[]{"9","2","5","5","6"});
p1.add(new String[]{"1","2","41","11","9"});
ArrayList<String[]> q1 = new ArrayList<>();
q1.add(new String[]{"NO","商品名","日付","販売額合計"});
level2.sum(p1,q1);
for(int i =0;i<q.size()-1;i++){
assertEquals(Arrays.toString(q.get(i)), Arrays.toString(q1.get(i)));
}
System.out.println("按商品名及日付排序合计贩卖额测试成功");
}
@Test
void main() {
}
}```