-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManipulator.java
More file actions
62 lines (52 loc) · 2.25 KB
/
FileManipulator.java
File metadata and controls
62 lines (52 loc) · 2.25 KB
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
package org.example;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FileManipulator {
private final File file;
String fileName;
int number;
public FileManipulator(String fileName, int number) {
this.fileName = fileName;
this.number = number;
file = new File(this.fileName);
}
public void manipulate() {
try {
if (file.isFile()) {
System.out.println("Dokumenti u gjet.");
if (number == 0) {
throw new ArithmeticException();
} else if (number > 10 || number < 0) {
throw new InputMismatchException();
}
int randNum = (int) (Math.random() * (number * 10));
double result = (double) randNum / number;
System.out.println("Numri i zgjedhur: " + number + " | Numri random: " + randNum);
System.out.println("Po kryhet funksioni... \n...pjesetim i numrit random me numrin e zgjedhur...");
System.out.println("Vlerat ne dokument: ");
try (FileWriter writer = new FileWriter(file, true)) {
writer.append("Numer random ").append(String.valueOf(randNum)).append(" pjesetim me ").append(String.valueOf(number)).append(" eshte ").append(String.valueOf(result)).append("\n");
}
try (Scanner fileScanner = new Scanner(file)) {
while (fileScanner.hasNextLine()) {
System.out.println(fileScanner.nextLine());
}
}
} else {
throw new FileNotFoundException();
}
} catch (FileNotFoundException fe) {
System.out.println("Dokumenti nuk u gjet! " + fe);
} catch (InputMismatchException i) {
System.out.println("Input i pavlefshem! Number duhet te jete pozitiv dhe/ose me i vogel se 10! " + i);
} catch (ArithmeticException ae) {
System.out.println("Pjesetim me 0! " + ae);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}