-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKontrolaWejścia.java
180 lines (133 loc) · 4.83 KB
/
KontrolaWejścia.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package planista;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author konrad
*/
public class KontrolaWejścia {
private File file;
private Scanner sc;
public KontrolaWejścia(String[] args) throws FileNotFoundException {
this.sc = new Scanner(System.in);
if (args.length > 0) {
this.file = new File(args[0]);
this.sc = new Scanner(file);
}
else
System.err.println("Plik z danymi nie jest dostępny.");
}
private void wypiszBłąd (int numerLinii, String wiadomość) {
System.err.println("Błąd w linii " + numerLinii + ": " + wiadomość);
}
public ArrayList<Proces> wczytaj () {
ArrayList<Proces> wejście = new ArrayList<>();
String l = sc.nextLine();
Scanner scc = new Scanner(l);
int n;
if (scc.hasNextInt()) {
n = scc.nextInt();
if (scc.hasNext()) {
wypiszBłąd(1, "Za dużo danych.");
return new ArrayList<>();
}
}
else {
if (!scc.hasNext())
wypiszBłąd(1, "Za mało danych.");
else
wypiszBłąd(1, "Zły typ danych.");
return new ArrayList<>();
}
for (int i = 0; i < n; ++i) {
l = sc.nextLine();
scc = new Scanner(l);
int x, y;
if (scc.hasNextInt()) {
x = scc.nextInt();
if (x < 0) {
wypiszBłąd(i+2, "Zła wartość.");
return new ArrayList<>();
}
if (scc.hasNextInt()) {
y = scc.nextInt();
if (y < 1) {
wypiszBłąd(i+2, "Zła wartość.");
return new ArrayList<>();
}
if (scc.hasNext()) {
wypiszBłąd(i+2, "Za dużo danych.");
return new ArrayList<>();
}
wejście.add(new Proces(i+1, x, y));
}
else if (scc.hasNext()) {
wypiszBłąd(i+2, "Zły typ danych.");
return new ArrayList<>();
}
else {
wypiszBłąd(i+2, "Za mało danych.");
return new ArrayList<>();
}
}
else if (scc.hasNext()) {
wypiszBłąd(i+2, "Zły typ danych.");
return new ArrayList<>();
}
else {
wypiszBłąd(i+2, "Za mało danych.");
return new ArrayList<>();
}
}
return wejście;
}
public ArrayList<Integer> wczytajRR (int numerLinii) {
ArrayList<Integer> wejście = new ArrayList<>();
String l = sc.nextLine();
Scanner scc = new Scanner(l);
int n;
if (scc.hasNextInt()) {
n = scc.nextInt();
if (scc.hasNext()) {
wypiszBłąd(numerLinii, "Za dużo danych.");
return new ArrayList<Integer>();
}
}
else {
if (!scc.hasNext())
wypiszBłąd(numerLinii, "Za mało danych.");
else
wypiszBłąd(numerLinii, "Zły typ danych.");
return new ArrayList<Integer>();
}
l = sc.nextLine();
scc = new Scanner(l);
int q;
for (int i = 0; i < n; ++i) {
if (scc.hasNextInt()) {
q = scc.nextInt();
wejście.add(q);
}
else if (scc.hasNext()) {
wypiszBłąd(3 + numerLinii, "Zły typ danych.");
return new ArrayList<Integer>();
}
else {
wypiszBłąd(3 + numerLinii, "Za mało danych.");
return new ArrayList<Integer>();
}
}
if (scc.hasNext()) {
wypiszBłąd(3 + numerLinii, "Za dużo danych.");
return new ArrayList<Integer>();
}
return wejście;
}
}