package day28_TotalStream;
import java.text.DecimalFormat;
public class DTO {
DecimalFormat df = new DecimalFormat("#,##0.0");
int ban,number;
String name;
int kor,eng,mat;
int tot;
double avg;
String hak;
public DTO() {}
public DTO(int ban, int number, String name, int kor, int eng, int mat, int tot, double avg, String hak) {
super();
this.ban = ban;
this.number = number;
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
this.tot = tot;
this.avg = avg;
this.hak = hak;
}
public int getBan() {
return ban;
}
public void setBan(int ban) {
this.ban = ban;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public String getHak() {
return hak;
}
public void setHak(String hak) {
this.hak = hak;
}
@Override
public String toString() {
return "ban=" + ban + ", number=" + number + ", name=" + name + ", kor=" + kor + ", eng=" + eng + ", mat="
+ mat + ", tot=" + tot + ", avg=" + df.format(avg) + ", hak=" + hak +",";
}
}
package day28_TotalStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
DAO dao = new DAO();
while(true) {
System.out.println("[0]종료\t[1]입력\t[2]출력");
System.out.println("[3]반별 오름차순 정렬");
System.out.println("[4]반별 오름...총점 오름 정렬");
System.out.println("[5]반별 오름...총점 내림 정렬");
System.out.println("[6]반별 내림...총점 오름 정렬");
System.out.println("[7]반별 내림...총점 내림 정렬");
System.out.print("원하시는 작업번호");
int n = sc.nextInt();
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
case 3:dao.banasc();break;
case 4:dao.asc1();break;
case 5:dao.asc2();break;
case 6:dao.asc3();break;
case 7:dao.asc4();break;
}
}
}
}
package day28_TotalStream;
import java.text.ChoiceFormat;
public class Process {
public int tot(int kor,int eng,int mat) {
return kor+eng+mat;
}
public double avg(int tot) {
return (double)tot/3;
}
public String hak(double avg) {
double [] avg1 = {0,60,70,80,90};
String [] hak1 = {"F","D","C","B","A"};
ChoiceFormat hakjum = new ChoiceFormat(avg1,hak1);
return hakjum.format(avg);
}
}
package day28_TotalStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DAO {
ArrayList<DTO> list = new ArrayList<DTO>();
Process pr = new Process();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01111.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
DTO dto = new DTO();
dto.setBan(Integer.parseInt(str.substring(0, 1)));
dto.setNumber(Integer.parseInt(str.substring(1, 3)));
dto.setName(str.substring(3, 6));
dto.setKor(Integer.parseInt(str.substring(6, 9)));
dto.setEng(Integer.parseInt(str.substring(9, 12)));
dto.setMat(Integer.parseInt(str.substring(12, 15)));
dto.setTot(pr.tot(dto.getKor(), dto.getEng(), dto.getMat()));
dto.setAvg(pr.avg(dto.getTot()));
dto.setHak(pr.hak(dto.getAvg()));
list.add(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
List<DTO> gr = list.stream().sorted(Comparator.comparing(DTO::getTot).reversed()).collect(Collectors.toList());
int rank=0;
for(DTO a:gr) {
rank++;
System.out.print(a.toString());
System.out.println(" rank="+rank);
}
}
public void banasc() {
System.out.println("반 오름차순정렬\n================");
List<DTO> banasc = list.stream().sorted(Comparator.comparing(DTO::getName)).collect(Collectors.toList());
banasc.forEach(s->System.out.println(s.toString()));
}
public void asc1() {
System.out.println("반 오름...총점별 오름\\n===============");
Stream<DTO> newlist1 = list.stream().sorted(Comparator.comparing(DTO::getBan).thenComparing(Comparator.comparing(DTO::getTot)));
newlist1.forEach(s->System.out.println(s.toString()));
}
public void asc2() {
System.out.println("반 오름...총점별 내림\\n===============");
Stream<DTO> newlist2 = list.stream().sorted(Comparator.comparing(DTO::getBan).thenComparing(Comparator.comparing(DTO::getTot).reversed()));
newlist2.forEach(s->System.out.println(s.toString()));
}
public void asc3() {
System.out.println("반 내림...총점별 오름\\n===============");
Stream<DTO> newlist3 = list.stream().sorted(Comparator.comparing(DTO::getBan).reversed().thenComparing(Comparator.comparing(DTO::getTot)));
newlist3.forEach(s->System.out.println(s.toString()));
}
public void asc4() {
System.out.println("반 내림...총점별 내림\\n===============");
Stream<DTO> newlist4 = list.stream().sorted(Comparator.comparing(DTO::getBan).reversed().thenComparing(Comparator.comparing(DTO::getTot).reversed()));
newlist4.forEach(s->System.out.println(s.toString()));
}
}
package day28_TotalStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DAO {
ArrayList<DTO> list = new ArrayList<DTO>();
Process pr = new Process();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01111.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
DTO dto = new DTO();
dto.setBan(Integer.parseInt(str.substring(0, 1)));
dto.setNumber(Integer.parseInt(str.substring(1, 3)));
dto.setName(str.substring(3, 6));
dto.setKor(Integer.parseInt(str.substring(6, 9)));
dto.setEng(Integer.parseInt(str.substring(9, 12)));
dto.setMat(Integer.parseInt(str.substring(12, 15)));
dto.setTot(pr.tot(dto.getKor(), dto.getEng(), dto.getMat()));
dto.setAvg(pr.avg(dto.getTot()));
dto.setHak(pr.hak(dto.getAvg()));
list.add(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
List<DTO> gr = list.stream().sorted(Comparator.comparing(DTO::getTot).reversed()).collect(Collectors.toList());
int rank=0;
for(DTO a:gr) {
rank++;
System.out.print(a.toString());
System.out.println(" rank="+rank);
}
}
public void kwatotal() {
DecimalFormat df = new DecimalFormat("#,##0.0");
int kortot = list.stream().mapToInt(DTO::getKor).sum();
int engtot = list.stream().mapToInt(DTO::getEng).sum();
int mattot = list.stream().mapToInt(DTO::getMat).sum();
double koravg = list.stream().mapToInt(DTO::getKor).average().getAsDouble();
double engavg = list.stream().mapToInt(DTO::getEng).average().getAsDouble();
double matavg = list.stream().mapToInt(DTO::getMat).average().getAsDouble();
System.out.println("국어총점 : "+kortot+"\t영어총점 : "+engtot+"\t수학총점 : "+mattot);
System.out.println("국어평균 : "+df.format(koravg)+"\t영어평균 : "+df.format(engavg)+"\t수학평균 : "+df.format(matavg));
double totavg = list.stream().mapToDouble(DTO::getAvg).average().getAsDouble();
System.out.println("전체평균 : "+df.format(totavg));
}
}
package day28_TotalStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DAO {
ArrayList<DTO> list = new ArrayList<DTO>();
Process pr = new Process();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01111.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
DTO dto = new DTO();
dto.setBan(Integer.parseInt(str.substring(0, 1)));
dto.setNumber(Integer.parseInt(str.substring(1, 3)));
dto.setName(str.substring(3, 6));
dto.setKor(Integer.parseInt(str.substring(6, 9)));
dto.setEng(Integer.parseInt(str.substring(9, 12)));
dto.setMat(Integer.parseInt(str.substring(12, 15)));
dto.setTot(pr.tot(dto.getKor(), dto.getEng(), dto.getMat()));
dto.setAvg(pr.avg(dto.getTot()));
dto.setHak(pr.hak(dto.getAvg()));
list.add(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
List<DTO> gr = list.stream().sorted(Comparator.comparing(DTO::getTot).reversed()).collect(Collectors.toList());
int rank=0;
System.out.println("반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점\t등수");
for(DTO a:gr) {
rank++;
System.out.print(a.toString());
System.out.println("\t"+rank);
}
}
public void kwatotal() {
DecimalFormat df = new DecimalFormat("#,##0.0");
int kortot = list.stream().mapToInt(DTO::getKor).sum();
int engtot = list.stream().mapToInt(DTO::getEng).sum();
int mattot = list.stream().mapToInt(DTO::getMat).sum();
double koravg = list.stream().mapToInt(DTO::getKor).average().getAsDouble();
double engavg = list.stream().mapToInt(DTO::getEng).average().getAsDouble();
double matavg = list.stream().mapToInt(DTO::getMat).average().getAsDouble();
System.out.println("국어총점 : "+kortot+"\t영어총점 : "+engtot+"\t수학총점 : "+mattot);
System.out.println("국어평균 : "+df.format(koravg)+"\t영어평균 : "+df.format(engavg)+"\t수학평균 : "+df.format(matavg));
double totavg = list.stream().mapToDouble(DTO::getAvg).average().getAsDouble();
System.out.println("전체평균 : "+df.format(totavg));
}
//필터예제 . filter..걸러낸다..if..조건문
public void filterTest() {
long a = list.stream().filter(s->s.getKor()>=70).count();
int b = list.stream().filter(s->s.getKor()>=70).mapToInt(DTO::getKor).sum();
System.out.println("국어70점이상 인원수 : "+a+" 합 : "+b);
long c= list.stream().filter(s->s.getTot()<=100 || s.getTot()>=290).count();
int d = list.stream().filter(s->s.getTot()<=100 || s.getTot()>=290).mapToInt(DTO::getTot).sum();
List<DTO> e= list.stream().filter(s->s.getTot()<=100 || s.getTot()>=290).collect(Collectors.toList());
System.out.println("반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점");
e.forEach(s->System.out.println(s.toString()));
System.out.println("총점 100이하290이상 인원수 :"+c+" 총점합 :"+d);
//총점이 200점대인 자료만 골라서 반,번호 오름차순으로 정렬 출력
Stream<DTO> newlist = list.stream().filter(s->s.getTot()>=200 && s.getTot()<=299).sorted(Comparator.comparing(DTO::getBan).thenComparing(Comparator.comparing(DTO::getNumber)));
System.out.println("반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점");
newlist.forEach(s->System.out.println(s.toString()));
//반이 1반만 이름순 정렬 목록 출력
Stream<DTO> newlist1 = list.stream().filter(s->s.getBan()==1).sorted(Comparator.comparing(DTO::getName));
System.out.println("반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점");
newlist1.forEach(s->System.out.println(s.toString()));
Stream<DTO> newlist2 = list.stream().filter(s->s.getBan()==2).sorted(Comparator.comparing(DTO::getName));
System.out.println("\n반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점");
newlist2.forEach(s->System.out.println(s.toString()));
}
}
package day28_TotalStream;
public class Father {
String sb,address;
public Father() {
super();
}
public Father(String sb) {
super();
this.sb = sb;
this.address = address;
}
public String getSb() {
return sb;
}
public void setSb(String sb) {
this.sb = sb;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package day28_TotalStream;
import java.text.DecimalFormat;
public class DTO extends Father{
DecimalFormat df = new DecimalFormat("#,##0.0");
int ban,number;
String name;
int kor,eng,mat;
int tot;
double avg;
String hak;
public DTO() {}
public DTO(String sb, int ban, int number, String name, int kor, int eng, int mat, int tot, double avg,
String hak) {
super(sb);
this.ban = ban;
this.number = number;
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
this.tot = tot;
this.avg = avg;
this.hak = hak;
}
public int getBan() {
return ban;
}
public void setBan(int ban) {
this.ban = ban;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public String getHak() {
return hak;
}
public void setHak(String hak) {
this.hak = hak;
}
@Override
public String toString() {
return ban+"\t"+number+"\t"+name+"\t"+kor+"\t"+eng+"\t"+mat+"\t"+tot+"\t"+df.format(avg)+"\t"+hak+"\t"+sb+"\t"+address;
}
// @Override
// public String toString() {
//// "반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점\n"
// return ban+"\t"+number+"\t"+name+"\t"+kor+"\t"+eng+"\t"+mat+"\t"+tot+"\t"+df.format(avg)+"\t"+hak;
// return "ban=" + ban + ", number=" + number + ", name=" + name + ", kor=" + kor + ", eng=" + eng + ", mat="
// + mat + ", tot=" + tot + ", avg=" + df.format(avg) + ", hak=" + hak +",";
// }
}
package day28_TotalStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DAO {
enum sb {m("남자"),f("여자");
private String value;
private sb(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
enum ad {se("서울"),sw("수원"),ki("경기도");
private String value;
private ad(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
ArrayList<DTO> list = new ArrayList<DTO>();
Process pr = new Process();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/01111.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
DTO dto = new DTO();
dto.setBan(Integer.parseInt(str.substring(0, 1)));
dto.setNumber(Integer.parseInt(str.substring(1, 3)));
dto.setName(str.substring(3, 6));
dto.setKor(Integer.parseInt(str.substring(6, 9)));
dto.setEng(Integer.parseInt(str.substring(9, 12)));
dto.setMat(Integer.parseInt(str.substring(12, 15)));
dto.setSb(sb.valueOf(str.substring(15, 16)).getValue());
dto.setAddress(ad.valueOf(str.substring(16, 18)).getValue());
dto.setTot(pr.tot(dto.getKor(), dto.getEng(), dto.getMat()));
dto.setAvg(pr.avg(dto.getTot()));
dto.setHak(pr.hak(dto.getAvg()));
list.add(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
List<DTO> gr = list.stream().sorted(Comparator.comparing(DTO::getTot).reversed()).collect(Collectors.toList());
int rank=0;
System.out.println("반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점\t성별\t주소\t등수");
for(DTO a:gr) {
rank++;
System.out.print(a.toString());
System.out.println("\t"+rank);
}
}
public void kwatotal() {
DecimalFormat df = new DecimalFormat("#,##0.0");
int kortot = list.stream().mapToInt(DTO::getKor).sum();
int engtot = list.stream().mapToInt(DTO::getEng).sum();
int mattot = list.stream().mapToInt(DTO::getMat).sum();
double koravg = list.stream().mapToInt(DTO::getKor).average().getAsDouble();
double engavg = list.stream().mapToInt(DTO::getEng).average().getAsDouble();
double matavg = list.stream().mapToInt(DTO::getMat).average().getAsDouble();
System.out.println("국어총점 : "+kortot+"\t영어총점 : "+engtot+"\t수학총점 : "+mattot);
System.out.println("국어평균 : "+df.format(koravg)+"\t영어평균 : "+df.format(engavg)+"\t수학평균 : "+df.format(matavg));
double totavg = list.stream().mapToDouble(DTO::getAvg).average().getAsDouble();
System.out.println("전체평균 : "+df.format(totavg));
}
//필터예제 . filter..걸러낸다..if..조건문
public void filterTest() {
// Map<Integer, List<DTO>> map = list.stream().collect(Collectors.groupingBy(DTO::getBan));
// Set<Integer> kset = map.keySet();
// Iterator<Integer> it = kset.iterator();
// System.out.println("반\t번호\t이름\t국어\t영어\t수학\t합\t평균\t학점");
// while(it.hasNext()) {
// int ban = it.next();
// Stream<DTO> newlist = list.stream().filter(s->s.getBan()==ban).sorted(Comparator.comparing(DTO::getName));
// System.out.println(ban+"==========");
// newlist.forEach(s->System.out.println(s.toString()));
// }
Map<Integer, List<DTO>> map = list.stream().collect(Collectors.groupingBy(DTO::getBan));
Set<Integer> kset = map.keySet();
Iterator<Integer> it = kset.iterator();
while(it.hasNext()) {
int ban = it.next();
int kortot = list.stream().filter(s->s.getBan()==ban).mapToInt(DTO::getKor).sum();
int engtot = list.stream().filter(s->s.getBan()==ban).mapToInt(DTO::getEng).sum();
int mattot = list.stream().filter(s->s.getBan()==ban).mapToInt(DTO::getMat).sum();
System.out.println(ban+"반의 국어점수합 :"+kortot+" 영어점수합 :"+engtot+" 수학점수합 :"+mattot);
}
}
}
'JAVA' 카테고리의 다른 글
230110_Stream (0) | 2023.01.10 |
---|---|
230109_Stream (0) | 2023.01.09 |
230106_Map (0) | 2023.01.06 |
230105_Map (0) | 2023.01.05 |
230104_Map (0) | 2023.01.04 |
댓글