Created
September 18, 2017 15:00
-
-
Save SKocur/966d2ff81fb1588b8a59751a2511c8f3 to your computer and use it in GitHub Desktop.
Zadanie z informatyki na 20.09.2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.*; | |
class Main { | |
static List<WinPOJO> wins; | |
public static void main(String[] args){ | |
wins = new ArrayList<WinPOJO>(); | |
try{ | |
File file = new File("losowania.txt"); | |
Scanner scanner = new Scanner(file); | |
scanner.nextLine(); | |
while(scanner.hasNext()){ | |
String[] winData = scanner.nextLine().split("\\s+"); | |
WinPOJO win = new WinPOJO(); | |
win.setDate(winData[0]); | |
int[] nums = new int[6]; | |
for(int i = 1; i <= 6; i++){ | |
nums[i-1] = Integer.parseInt(winData[i]); | |
} | |
win.setNums(nums); | |
//Check if city's name consist of one or two or three words | |
if(winData.length == 10){ | |
win.setCity(winData[7]); | |
win.setPrize(Float.parseFloat(winData[8].replace(',', '.'))); | |
win.setLottery(winData[9]); | |
} else if(winData.length == 11) { | |
win.setCity(winData[7] + " " + winData[8]); | |
win.setPrize(Float.parseFloat(winData[9].replace(',', '.'))); | |
win.setLottery(winData[10]); | |
} else { | |
win.setCity(winData[7] + " " + winData[8] + " " + winData[9]); | |
win.setPrize(Float.parseFloat(winData[10].replace(',', '.'))); | |
win.setLottery(winData[11]); | |
} | |
wins.add(win); | |
} | |
//taskA(); | |
//taskB(); | |
//taskC(); | |
//taskD(); | |
} catch(FileNotFoundException e){ | |
System.out.println("File not found!"); | |
} | |
} | |
private static void taskD(){ | |
int sum = 0; | |
for(WinPOJO win : wins){ | |
String[] yearS = win.getDate().split("-"); | |
int year = Integer.parseInt(yearS[0]); | |
if(year >= 2000 && year <= 2003){ | |
sum += win.getPrize(); | |
} | |
} | |
System.out.println("Sum: " + sum); | |
} | |
private static void taskC(){ | |
Map<String, Integer> numbers = new HashMap<String, Integer>(); | |
for(WinPOJO win : wins){ | |
String[] nums = win.getNums().split(","); | |
for(int i = 0; i < nums.length; i++){ | |
if(numbers.get(nums[i]) == null) { | |
numbers.put((String) nums[i], 1); | |
} else { | |
numbers.replace((String) nums[i], numbers.get((String) nums[i]) + 1); | |
} | |
} | |
} | |
String num = ""; | |
int quantity = 0; | |
Iterator it = numbers.entrySet().iterator(); | |
while(it.hasNext()){ | |
Map.Entry pair = (Map.Entry) it.next(); | |
if((Integer) pair.getValue() > quantity){ | |
num = (String) pair.getKey(); | |
quantity = (Integer) pair.getValue(); | |
} | |
} | |
System.out.println("Num: " + num + " Quantity: " + quantity); | |
} | |
private static void taskB(){ | |
Map<String, Integer> cities = new HashMap<String, Integer>(); | |
for(WinPOJO win : wins){ | |
if(cities.get(win.getCity()) == null){ | |
cities.put(win.getCity(), 1); | |
} else { | |
cities.replace(win.getCity(), cities.get(win.getCity()) + 1); | |
} | |
} | |
Iterator it = cities.entrySet().iterator(); | |
while(it.hasNext()){ | |
Map.Entry pair = (Map.Entry) it.next(); | |
if((Integer) pair.getValue() > 10) | |
System.out.println(pair.getKey()); | |
it.remove(); | |
} | |
} | |
private static void taskA(){ | |
String cityName = "A"; | |
float cityPrize = 0.0f; | |
for(WinPOJO win : wins){ | |
if(win.getPrize() > cityPrize){ | |
cityName = win.getCity(); | |
cityPrize = win.getPrize(); | |
} | |
} | |
System.out.println(cityName); | |
} | |
private static class WinPOJO { | |
String date = ""; | |
int num1 = 0; | |
int num2 = 0; | |
int num3 = 0; | |
int num4 = 0; | |
int num5 = 0; | |
int num6 = 0; | |
String city = ""; | |
float prize = 0.0f; | |
String lottery = ""; | |
private void setDate(String date){ | |
this.date = date; | |
} | |
private String getDate(){ | |
return this.date; | |
} | |
private void setNums(int[] nums){ | |
this.num1 = nums[0]; | |
this.num2 = nums[1]; | |
this.num3 = nums[2]; | |
this.num4 = nums[3]; | |
this.num5 = nums[4]; | |
this.num6 = nums[5]; | |
} | |
private String getNums(){ | |
return this.num1 + "," | |
+ this.num2 + "," | |
+ this.num3 + "," | |
+ this.num4 + "," | |
+ this.num5 + "," | |
+ this.num6; | |
} | |
private void setCity(String city){ | |
this.city = city; | |
} | |
private String getCity(){ | |
return this.city; | |
} | |
private void setPrize(float prize){ | |
this.prize = prize; | |
} | |
private float getPrize(){ | |
return this.prize; | |
} | |
private void setLottery(String lottery){ | |
this.lottery = lottery; | |
} | |
private String getLottery(){ | |
return this.lottery; | |
} | |
public String toString(){ | |
return "Date: " + this.date + "\n" | |
+ "Nums: " + this.num1 + "," + this.num2 + "," + this.num3 + "," + this.num4 + "," + this.num5 + "," + this.num6 + "\n" | |
+ "City: " + this.city + "\n" | |
+ "Prize: " + this.prize + "\n" | |
+ "Lottery: " + this.lottery + "\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment