Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Created May 24, 2014 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayato-p/5ae9d39f0be99abad39d to your computer and use it in GitHub Desktop.
Save ayato-p/5ae9d39f0be99abad39d to your computer and use it in GitHub Desktop.
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: ayato
* Date: 5/24/14
* Time: 4:22 PM
* To change this template use File | Settings | File Templates.
*/
public class ClassPerformance {
enum Subjects {
ENGLISH, MATH, JAPANESE
}
private final String className;
private final List<Performance> performances;
private int english;
private int math;
private int japanese;
public ClassPerformance(String className, List<Performance> performances) {
this.className = className;
this.performances = performances;
english = averageCalculate(Subjects.ENGLISH);
math = averageCalculate(Subjects.MATH);
japanese = averageCalculate(Subjects.JAPANESE);
}
private int averageCalculate(Subjects subject) {
int sum = 0;
switch (subject) {
case ENGLISH:
for (Performance performance : performances) {
sum += performance.english;
}
return sum / performances.size();
case MATH:
for (Performance performance : performances) {
sum += performance.math;
}
return sum / performances.size();
case JAPANESE:
for (Performance performance : performances) {
sum += performance.japanese;
}
return sum / performances.size();
default:
return 0;
}
}
@Override
public String toString() {
return className + "\t" + english + "\t" + math + "\t" + japanese;
}
}
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: ayato
* Date: 5/24/14
* Time: 2:41 PM
* To change this template use File | Settings | File Templates.
*/
public class Main {
public static void main(String[] args) {
List<Performance> performances = new ArrayList();
List<String> classList = new ArrayList();
List<String> lines = fileRead();
for (String line : lines) {
String[] params = line.split(", ");
performances.add(new Performance(params));
String className = params[0];
if (!classList.contains(className)){
classList.add(className);
}
}
Collections.sort(classList);
List<ClassPerformance> classPerformances = new ArrayList();
for (String className : classList) {
classPerformances.add(new ClassPerformance(className, pickClassPerformances(performances, className)));
}
System.out.println("クラス\t英語\t数学\t国語");
System.out.println("--------------------------------");
for (ClassPerformance classPerformance : classPerformances) {
System.out.println(classPerformance);
}
}
private static List<String> fileRead() {
List<String> list = null;
try {
list = Files.readAllLines(Paths.get("performances.csv"), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return list;
}
private static List<Performance> pickClassPerformances(List<Performance> performances, String className) {
List<Performance> classPerformances = new ArrayList();
for (Performance performance : performances) {
if (className.equals(performance.className)) {
classPerformances.add(performance);
}
}
return classPerformances;
}
}
/**
* Created with IntelliJ IDEA.
* User: ayato
* Date: 5/24/14
* Time: 2:43 PM
* To change this template use File | Settings | File Templates.
*/
public class Performance {
public final String className;
public final int number;
public final String name;
public final int english;
public final int math;
public final int japanese;
public Performance(String className, int number, String name, int english, int math, int japanese) {
this.className = className;
this.number = number;
this.name = name;
this.english = english;
this.math = math;
this.japanese = japanese;
}
public Performance(String[] params) {
if(params == null || params.length != 6) throw new IndexOutOfBoundsException();
this.className = params[0];
this.number = Integer.parseInt(params[1]);
this.name = params[2];
this.english = Integer.parseInt(params[3]);
this.math = Integer.parseInt(params[4]);
this.japanese = Integer.parseInt(params[5]);
}
@Override
public String toString() {
return className + ", " + number + ", " + name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment