Lab3 #2

Merged
bytegrip merged 6 commits from lab3 into master 2023-10-17 21:36:00 +00:00
2 changed files with 44 additions and 1 deletions
Showing only changes of commit 8ee8648b2b - Show all commits

View File

@@ -16,6 +16,8 @@ public class CodeFile extends Document{
info.append("Extension: ").append(getExtension().toUpperCase()).append("<br>"); info.append("Extension: ").append(getExtension().toUpperCase()).append("<br>");
info.append("File size: ").append(getFilesizeKB()).append(" KB").append("<br>"); info.append("File size: ").append(getFilesizeKB()).append(" KB").append("<br>");
info.append("Lines: ").append(Utils.countLines(this)).append("<br>"); info.append("Lines: ").append(Utils.countLines(this)).append("<br>");
info.append("Classes: ").append(Utils.countClasses(this)).append("<br>");
info.append("Methods: ").append(Utils.countMethods(this)).append("<br>");
info.append("Created at: ").append(getCreatedTime()).append("<br>"); info.append("Created at: ").append(getCreatedTime()).append("<br>");
info.append("Modified at: ").append(getModificationTime()).append("<br>"); info.append("Modified at: ").append(getModificationTime()).append("<br>");
return info.toString(); return info.toString();

View File

@@ -45,7 +45,48 @@ public class Utils {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static int countClasses(Document doc) {
try {
int classCount = 0;
BufferedReader reader = new BufferedReader(new FileReader(doc));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("class ")) {
classCount++;
}
}
reader.close();
return classCount;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
public static int countMethods(Document doc) {
try {
int methodCount = 0;
BufferedReader reader = new BufferedReader(new FileReader(doc));
String line;
while ((line = reader.readLine()) != null) {
if (line.matches(".*\\b\\w+\\s+\\w+\\(.*\\)\\s*\\{.*")) {
methodCount++;
} else if (line.contains("def ")) {
methodCount++;
}
}
reader.close();
return methodCount;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
} }