← Vissza
A feladat forráskódja:
package tanciskola;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
class Dance {
private String dance;
private String woman;
private String man;
public Dance(String dance, String woman, String man) {
this.dance = dance;
this.woman = woman;
this.man = man;
}
public String getDance() {
return dance;
}
public String getWoman() {
return woman;
}
public String getMan() {
return man;
}
}
/**
*
* @author comtur
*/
public class Tanciskola {
private static List<Dance> danceList;
private static List<String> menList;
private static List<String> womenList;
public static void main(String[] args) {
task1();
task2();
task3();
task4();
task5();
task6();
task7();
}
private static void task1() {
taskNum(1);
System.out.println("(Adatok beolvasása)");
try (BufferedReader br = new BufferedReader(new FileReader("tancrend.txt"))) {
String line = null;
String man = null;
String woman = null;
danceList = new ArrayList<>();
while ((line = br.readLine()) != null) {
woman = br.readLine();
man = br.readLine();
Dance dance = new Dance(line, woman, man);
danceList.add(dance);
}
} catch (IOException ex) {
Logger.getLogger(Tanciskola.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static void taskNum(int i) {
System.out.println("\n" + i + ". feladat");
System.out.println("----------");
}
private static void task2() {
taskNum(2);
System.out.printf("Az elsőként bemutatott tánc neve: %s%n",
danceList.get(0).getDance());
System.out.printf("Az utolsóként bemutatott tánc neve: %s%n",
danceList.get(danceList.size() - 1).getDance());
}
private static void task3() {
taskNum(3);
int samba = 0;
for (Dance dance : danceList) {
if (dance.getDance().equals("samba")) {
samba++;
}
}
System.out.printf("A sambát %d pár mutatta be.%n", samba);
}
private static void task4() {
taskNum(4);
Set<String> vilmaDance = new HashSet<>();
for (Dance dance : danceList) {
if (dance.getWoman().equals("Vilma")) {
vilmaDance.add(dance.getDance());
}
}
System.out.println("Vilma ezekben a táncolban szerepelt:");
for (String string : vilmaDance) {
System.out.println(" - " + string);
}
}
private static void task5() {
taskNum(5);
Scanner sc = new Scanner(System.in);
System.out.print("Kérem egy tánc nevét: ");
String danceName = sc.nextLine().toLowerCase();
String man = null;
for (Dance dance : danceList) {
if (dance.getDance().equals(danceName) && dance.getWoman().equals("Vilma")) {
man = dance.getMan();
break;
}
}
if (man != null) {
System.out.printf("A %s bemutatóján Vilma párja %s volt.%n",
danceName, man);
} else {
System.out.printf("Vilma nem táncolt %s-t.%n", danceName);
}
}
private static void task6() {
taskNum(6);
System.out.println("(állomány kiíratása)");
Set<String> men = new HashSet<>();
Set<String> women = new HashSet<>();
for (Dance dance : danceList) {
men.add(dance.getMan());
women.add(dance.getWoman());
}
menList = new ArrayList<>();
men.forEach((t) -> {menList.add(t);});
womenList = new ArrayList<>();
women.forEach((t) -> {womenList.add(t);});
try (PrintWriter pw = new PrintWriter("szereplok.txt")) {
pw.print("Lányok: ");
for (int i = 0; i < womenList.size(); i++) {
pw.print(womenList.get(i) + (i == womenList.size() - 1 ? "\r\n" : ", "));
}
pw.print("Fiúk: ");
for (int i = 0; i < menList.size(); i++) {
pw.print(menList.get(i) + (i == menList.size() - 1 ? "\r\n" : ", "));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Tanciskola.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static void task7() {
taskNum(7);
int[] boys = new int[menList.size()];
int[] girls = new int[womenList.size()];
for (Dance dance : danceList) {
for (int i = 0; i < menList.size(); i++) {
if (dance.getMan().equals(menList.get(i))) {
boys[i]++;
break;
}
}
for (int i = 0; i < womenList.size(); i++) {
if (dance.getWoman().equals(womenList.get(i))) {
girls[i]++;
break;
}
}
}
int boysMax = searchMax(boys);
int girlsMax = searchMax(girls);
System.out.println("A lányok közül legtöbbet szerepelt:");
for (int i = 0; i < girls.length; i++) {
if (girls[i] == girlsMax) {
System.out.println(" - " + womenList.get(i));
}
}
System.out.println("A fiúk közül legtöbbet szerepelt:");
for (int i = 0; i < boys.length; i++) {
if (boys[i] == boysMax) {
System.out.println(" - " + menList.get(i));
}
}
}
private static int searchMax(int[] gender) {
int max = gender[0];
for (int i : gender) {
if (i > max) {
max = i;
}
}
return max;
}
}