Skip to content

Instantly share code, notes, and snippets.

@coldcue
Created April 23, 2013 14:16
Show Gist options
  • Save coldcue/5443915 to your computer and use it in GitHub Desktop.
Save coldcue/5443915 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package futar;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author admin
*/
public class Main {
private static List<Fuvar> fuvars = new ArrayList();
private static PrintStream ps;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
//UTF8 kimenet
ps = new PrintStream(System.out, true, "UTF8");
Excercise1();
Excercise2();
Excercise3();
Map<Integer, Integer> fvrperday = Excercise4();
Excercise5(fvrperday);
Excercise6();
Excercise7();
Excercise8();
Excercise9();
}
private static void Excercise1() throws FileNotFoundException, IOException {
ps.print("1. feladat: beolvasás... ");
BufferedReader file = new BufferedReader(new FileReader("tavok.txt"));
String line;
while ((line = file.readLine()) != null) {
String[] vals = line.split(" ");
Fuvar fvr = new Fuvar(Integer.parseInt(vals[0]), Integer.parseInt(vals[1]), Integer.parseInt(vals[2]));
fuvars.add(fvr);
}
Collections.sort(fuvars);
ps.println("OK");
}
private static void Excercise2() {
ps.println("\n2. feladat");
ps.println(Collections.max(fuvars).getDist() + " km volt a hét első útja!");
}
private static void Excercise3() {
ps.println("\n3. feladat");
ps.println(Collections.min(fuvars).getDist() + " km volt a hét utolsó útja!");
}
private static Map<Integer, Integer> Excercise4() {
ps.println("\n4. feladat");
Map<Integer, Integer> days = new HashMap();
for (int i = 1; i <= 7; i++) {
days.put(i, 0);
}
for (Fuvar fvr : fuvars) {
int count = days.get(fvr.getDay()) + 1;
days.remove(fvr.getDay());
days.put(fvr.getDay(), count);
}
ps.print("A ");
boolean first = true;
for (Map.Entry<Integer, Integer> e : days.entrySet()) {
if (e.getValue() == 0) {
if (first) {
first = false;
} else {
ps.print("és ");
}
ps.print(e.getKey() + ". ");
}
}
ps.println("napon nem dolgozott.");
return days;
}
private static void Excercise5(Map<Integer, Integer> fvrperday) {
ps.println("\n5. feladat");
Map.Entry<Integer, Integer> max = null;
boolean first = true;
for (Map.Entry<Integer, Integer> e : fvrperday.entrySet()) {
if (first) {
max = e;
first = false;
continue;
}
if (e.getValue() > max.getValue()) {
max = e;
}
}
ps.println("A " + max.getKey() + ". napon volt a legtöbb fuvar.");
}
private static void Excercise6() {
ps.println("\n6. feladat");
int[] kms = new int[7];
for (Fuvar fvr : fuvars) {
kms[fvr.getDay() - 1] += fvr.getDist();
}
for (int i = 0; i < 7; i++) {
ps.println((i + 1) + ". nap: " + kms[i] + " km");
}
}
private static void Excercise7() {
ps.println("\n7. feladat: Kérem írjon be egy távolságot!");
Scanner scn = new Scanner(System.in);
try {
ps.println(new Fuvar(0, 0, Integer.parseInt(scn.nextLine())).getPrice() + " Ft jár érte!");
} catch (IllegalArgumentException e) {
ps.println("Hiba! "+e.getMessage());
Excercise7();
}
}
private static void Excercise8() throws FileNotFoundException {
ps.print("\n8. feladat: dijazas.txt írása...");
PrintWriter file = new PrintWriter("dijazas.txt");
for (Fuvar fvr : fuvars) {
file.println(fvr.getDay() + ". nap " + fvr.getId() + ". út: " + fvr.getPrice() + " Ft");
}
file.flush();
ps.println(" OK");
}
private static void Excercise9() {
ps.println("\n9. feladat");
int priceSum = 0;
for (Fuvar fvr : fuvars) {
priceSum += fvr.getPrice();
}
ps.println("Az egész heti munkájáért " + priceSum + " Ft összeget kap!");
}
}
/**
*
* @author admin
*/
public class Fuvar implements Comparable<Fuvar> {
//1-7
private int day;
//1-40
private int id;
//max 30
private int dist;
public Fuvar(int day, int id, int dist) throws IllegalArgumentException {
if (!(dist <= 30)) {
throw new IllegalArgumentException("helytelen távolság");
}
this.day = day;
this.id = id;
this.dist = dist;
}
@Override
public int compareTo(Fuvar o) {
int daycomp = Integer.valueOf(getDay()).compareTo(Integer.valueOf(o.getDay()));
if (daycomp == 0) {
return Integer.valueOf(getId()).compareTo(Integer.valueOf(o.getId()));
}
return daycomp;
}
public int getPrice() {
if (dist >= 1 && dist <= 2) {
return 500;
} else if (dist >= 3 && dist <= 5) {
return 700;
} else if (dist >= 6 && dist <= 10) {
return 900;
} else if (dist >= 11 && dist <= 20) {
return 1400;
} else if (dist >= 21 && dist <= 30) {
return 2000;
}
return 0;
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @return the dist
*/
public int getDist() {
return dist;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment