Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,198 @@
package ru.hse.cs.java2020.task01;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.Collections;

public class Main {
private static File rootLib;
private static int countMax = 5;

static class Returning {
private File nameFile;
private long Filescnt, Folderscnt, totalSize;
private List<File> TopSize;

Returning() {
nameFile = null;
Filescnt = 0;
Folderscnt = 0;
totalSize = 0;
TopSize = new ArrayList<>();
}

Returning(File f) {
nameFile = f;
Filescnt = 0;
Folderscnt = 0;
totalSize = 0;
TopSize = new ArrayList<>();
}

Returning(long s, File f) {
nameFile = f;
Filescnt = -1;
Folderscnt = -1;
totalSize = s;
TopSize = null;
}
}

static class FileSizeComparator implements Comparator<File> {
public int compare(File f1, File f2) {
if (f1.length() != f2.length()) {
return Long.compare(f1.length(), f2.length()) * -1;
}
return 0;
}
}
static class FinalSizeComparator implements Comparator<Returning> {
public int compare(Returning r1, Returning r2) {
if (r1.totalSize != r2.totalSize) {
return Long.compare(r1.totalSize, r2.totalSize) * -1;
}
return 0;
}
}

private static int setPathAndNumMax(String[] args) {
if (args.length == 0) {
System.err.println("Too little amount of args");
return 1;
}
rootLib = new File(args[0]);
if (args.length > 1) {
try {
countMax = Integer.parseInt(args[1]);
if (Integer.parseInt(args[1]) < 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
System.err.println("Number Format Exception caught");
return 1;
}
}

if (rootLib.isDirectory() && rootLib.exists()) {
return 0;
}
System.err.println("The path is incorrect");
return 1;
}

private static Returning getContaining(File f) {
Queue<File> allFiles = new PriorityQueue<>();
Returning r = new Returning(f);

Collections.addAll(allFiles, f.listFiles());

while (!allFiles.isEmpty()) {
File fCur = allFiles.remove();

if (!fCur.isDirectory()) {
if (countMax > 0) {
if (r.TopSize.size() < countMax) {
r.TopSize.add(fCur);
} else if (r.TopSize.get(countMax - 1).length() < fCur.length()) {
r.TopSize.set(countMax - 1, fCur);
r.TopSize.sort(new FileSizeComparator());
}
}
r.totalSize += fCur.length();
r.Filescnt++;
} else {
if (fCur.listFiles() != null) {
r.Folderscnt++;
Collections.addAll(allFiles, fCur.listFiles());
}
}
}
return r;
}



public static void main(String[] args) {
System.out.println("Hello World");
final long startTime = System.currentTimeMillis();
Queue<File> allFiles = new PriorityQueue<>();
List<File> biggestFiles = new ArrayList<>();
List<Returning> resFiles = new ArrayList<>();

if (setPathAndNumMax(args) != 0) {
return;
}

Collections.addAll(allFiles, rootLib.listFiles());

long totalSize = getContaining(rootLib).totalSize;

while (!allFiles.isEmpty()) {
File currentFile = allFiles.remove();

if (currentFile.isDirectory()) {
File[] tmp = currentFile.listFiles();
if (tmp != null) {
resFiles.add(getContaining(currentFile));
}
} else {
if (countMax > 0) {
if (biggestFiles.size() < countMax) {
biggestFiles.add(currentFile);
} else if (biggestFiles.get(countMax - 1).length() < currentFile.length()) {
biggestFiles.set(countMax - 1, currentFile);
biggestFiles.sort(new FileSizeComparator());
}
}
resFiles.add(new Returning(currentFile.length(), currentFile));
}

}

resFiles.sort(new FinalSizeComparator());
final int thousand24 = 1024;
final int hundred = 100;
final int thousand = 1000;


System.out.printf("%7s | %-50s | %14s | %15s | %12s | %14s | %12s %n",
"Number", "Path", "Size (kbytes)", "Size (percents)", "Files count", "Folders count", "All count");
for (int i = 0; i < resFiles.size(); i++) {
Returning r = resFiles.get(i);

if (r.nameFile.isDirectory()) {
System.out.printf("%7d | %-50s | %7d kbytes | %13f %% | %12d | %14d | %12d %n", i + 1,
r.nameFile.getPath().substring(args[0].length(), r.nameFile.getPath().length()),
r.totalSize / thousand24, (double) r.totalSize / totalSize * hundred,
r.Filescnt, r.Folderscnt, r.Filescnt + r.Folderscnt);
if (r.TopSize != null) {
biggestFiles.addAll(r.TopSize);
}
} else {
System.out.printf("%7d | %-50s | %7d kbytes | %13f %% | %12s | %14s | %12s %n", i + 1,
r.nameFile.getPath().substring(args[0].length(), r.nameFile.getPath().length()),
r.totalSize / thousand24, (double) r.totalSize / totalSize * hundred, "-", "-", "-");
}
}
biggestFiles.sort(new FileSizeComparator());

if (resFiles.size() < countMax) {
countMax = resFiles.size();
}
if (countMax > 0) {
System.out.println("\n");
System.out.printf("%7s | %14s | %-120s %n", "Number", "Size", "Path");
for (int i = 0; i < countMax; i++) {
System.out.printf("%7d | %7d kbytes | %-120s %n",
i + 1, biggestFiles.get(i).length() / thousand24, biggestFiles.get(i).getPath());
}
}

System.out.println("\n");
System.out.println("Total folder size: " + totalSize / thousand24 + " kbytes");
System.out.println("Total time: " + ((System.currentTimeMillis() - startTime) / thousand) + "c "
+ ((System.currentTimeMillis() - startTime) % thousand) + "ms");
}
}
}
2 changes: 2 additions & 0 deletions task-03-telegram-random-coffee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ version '1.0'
mainClassName = 'ru.hse.cs.java2020.task03.Main'

dependencies {
compile group: 'org.telegram', name: 'telegrambots', version: '4.8.1'
implementation 'org.postgresql:postgresql:42.2.10'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.hse.cs.java2020.task03;

public class BDInfo {
public BDInfo(String tkn, String orgId, String name) {
this.token = tkn;
this.org = orgId;
this.username = name;
}

public String getToken() {
return token;
}

public String getOrg() {
return org;
}

public String getUsername() {
return username;
}

private final String token;
private final String org;
private final String username;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ru.hse.cs.java2020.task03;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

import java.util.ArrayList;
import java.util.List;

public class Bot extends TelegramLongPollingBot {

private static final List<ArrayList<String>> updates = new ArrayList<ArrayList<String>>();
private static String TOKEN = null;
private static String USERNAME = null;

// public Bot(String token, String usrname) {
// TOKEN = token;
// USERNAME = usrname;
//
// ApiContextInitializer.init();
// TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
// }

public static Bot Builder(String token, String usrname) {
TOKEN = token;
USERNAME = usrname;

ApiContextInitializer.init();
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
Bot bot = new Bot();

try {
telegramBotsApi.registerBot(bot);
} catch (TelegramApiRequestException e) {
System.out.println("err");
}
return bot;
}

public List<ArrayList<String>> getUpdates() {
List<ArrayList<String>> buffer = new ArrayList<ArrayList<String>>(updates);
updates.clear();
return buffer;
}

private synchronized void sendMsg(String chatId, String s) {
SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(chatId);
sendMessage.setText(s);

try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage()) {
ArrayList<String> message = new ArrayList<>();
message.add(update.getMessage().getChatId().toString());
message.add(update.getMessage().getText());
updates.add(message);
sendMsg(update.getMessage().getChatId().toString(),
update.getMessage().getChatId().toString() + " your request is being proceeded");
}
}

@Override
public String getBotUsername() {
return USERNAME;
}

@Override
public String getBotToken() {
return TOKEN;
}
}
Loading