aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-28 17:33:25 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-28 17:33:25 +0200
commit53c13beb456dd5ba599ef4dbaadfa94f91b20979 (patch)
tree885661df21ca6b062850004f1337ad93008cd0a4 /src/com.example.portfolio2
parent7edc10541a273acebf185ee486a133205a176bef (diff)
move comments above code
Diffstat (limited to 'src/com.example.portfolio2')
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/Controller.java42
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java19
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/Model.java32
3 files changed, 71 insertions, 22 deletions
diff --git a/src/com.example.portfolio2/com/example/portfolio2/Controller.java b/src/com.example.portfolio2/com/example/portfolio2/Controller.java
index 7dafe72..cf98c3c 100644
--- a/src/com.example.portfolio2/com/example/portfolio2/Controller.java
+++ b/src/com.example.portfolio2/com/example/portfolio2/Controller.java
@@ -13,24 +13,41 @@ import java.util.List;
class Controller{
private Model model;
- private HelloApplication view; // We do this without using the view directly like this, instead passing options.
- void initialize() { // calls on the database
+
+ // We do this without using the view directly like this, instead passing options.
+ private HelloApplication view;
+
+ // calls on the database
+ void initialize() {
model.initialize();
}
+
Controller(Model model, HelloApplication view){
this.model=model; this.view=view;
}
+
void onComboSelected(ComboBox<String> combo, ComboBox<String> select, TextArea area) {
- select.getItems().clear(); // Clear the activity selection box
+
+ // Clear the activity selection box
+ select.getItems().clear();
area.clear(); // Clear text area
- select.getItems().addAll(model.selectProgram((String) combo.getValue())); // Fill activity box using model method
+
+ // Fill activity box using model method
+ select.getItems().addAll(model.selectProgram((String) combo.getValue()));
}
+
void onActivitySelected(ComboBox<String> combo, ComboBox<String> select, TextArea area) {
- addActivity((String) select.getValue(), area); // Passes the value chosen in the box
- updateArea(combo, area); // Updates the text area based on the category choice
+
+ // Passes the value chosen in the box
+ addActivity((String) select.getValue(), area);
+
+ // Updates the text area based on the category choice
// users can choose from the ComboBox, string (activity) and the area would update.
+ updateArea(combo, area);
}
+
void onSubjectModuleSelected(ComboBox<String> subject1, ComboBox<String> subject2, List<String> subjectModules) {
+
// Beautiful loop we've created to remove option chosen in one subject module box from the other
for (String sub : subjectModules) {
if (sub.equals(subject1.getValue())) {
@@ -40,18 +57,25 @@ class Controller{
}
}
}
- void addActivity(String s, TextArea textArea) { // Calls on model method to add participation in the database
+
+ // Calls on model method to add participation in the database
+ void addActivity(String s, TextArea textArea) {
model.addParticipation(model.getActivityIndeks(s));
}
- void updateArea(ComboBox combo, TextArea textArea) { // Clears the text area and adds all activity names from activities in participation
+
+ // Clears the text area and adds all activity names from activities in participation
+ void updateArea(ComboBox combo, TextArea textArea) {
textArea.clear();
for(String s : model.getParticipation((String) combo.getValue())) {
textArea.appendText(s + "\n");
}
}
- void updateEcts(Label ectslabel, ComboBox<String> comboBox) { // Updates the labels with the current ECTS of the program type
+
+ // Updates the labels with the current ECTS of the program type
+ void updateEcts(Label ectslabel, ComboBox<String> comboBox) {
ectslabel.setText("ECTS: "+model.getSumEcts(comboBox.getValue()));
}
+
void fillElective(ComboBox<String> electiveBox) {
electiveBox.getItems().addAll(model.getAllActivities());
}
diff --git a/src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java b/src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java
index 1a79300..1fa5a62 100644
--- a/src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java
+++ b/src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java
@@ -18,9 +18,12 @@ import java.io.IOException;
import java.util.List;
public class HelloApplication extends Application {
+
// Initializing model, controller and database objects
private Model model = new Model();
+
private Controller con = new Controller(model,this);
+
MyDB myDB = new MyDB();
/* Below is the original implementation of the UI elements with
@@ -84,14 +87,16 @@ public class HelloApplication extends Application {
con.updateEcts(col.ectsLabel, col.categoryCombo);
});
- switch (col.name) { // Switch statement handles different category boxes
+ // Switch statement handles different category boxes
+ switch (col.name) {
case "Program" -> {
col.categoryCombo.getItems().addAll("HumTek", "NatBach");
col.categoryCombo.setOnAction(event -> {
con.onComboSelected(col.categoryCombo, col.activitySelect, col.area);
});
}
- case "Subject 1" -> { // We considered using the list for filling the box but couldn't figure it out
+ // We considered using the list for filling the box but couldn't figure it out
+ case "Subject 1" -> {
col.categoryCombo.getItems().addAll("Computer Science", "Informatik", "Astrology");
col.categoryCombo.setOnAction(event -> {
con.onSubjectModuleSelected(col.categoryCombo, columns.get(2).categoryCombo, subjectModules);
@@ -100,14 +105,17 @@ public class HelloApplication extends Application {
}
case "Subject 2" -> {
col.categoryCombo.getItems().addAll("Computer Science", "Informatik", "Astrology");
- col.categoryCombo.setOnAction(event -> { // We figured we have to specify
- // the second column like this but reckon there's a better way.
+ // We figured we have to specify
+ // the second column like this but reckon there's a better way.
+ col.categoryCombo.setOnAction(event -> {
con.onSubjectModuleSelected(col.categoryCombo, columns.get(1).categoryCombo, subjectModules);
con.onComboSelected(col.categoryCombo, col.activitySelect, col.area);
});
}
case "Elective" -> {
- col.categoryCombo.setVisible(false); // Hide useless box
+
+ // Hide useless box
+ col.categoryCombo.setVisible(false);
con.fillElective(col.activitySelect);
}
}
@@ -127,6 +135,7 @@ public class HelloApplication extends Application {
// Class for each column of activities
private class ActivityColumn {
+
// Each column contains these elements
String name;
Label nameLabel;
diff --git a/src/com.example.portfolio2/com/example/portfolio2/Model.java b/src/com.example.portfolio2/com/example/portfolio2/Model.java
index 73f2f6b..bafa3fc 100644
--- a/src/com.example.portfolio2/com/example/portfolio2/Model.java
+++ b/src/com.example.portfolio2/com/example/portfolio2/Model.java
@@ -7,31 +7,47 @@ package com.example.portfolio2;
import java.util.ArrayList;
-class Model{ // The model handles all interactions with the database
+// The model handles all interactions with the database
+class Model{
MyDB db=new MyDB();
+
Model(){}
- void initialize() { // When running the program, it clears the participation database
+
+ // When running the program, it clears the participation database
+ void initialize() {
clearParticipation();
}
- int getActivityIndeks(String name) { // Returns the integer value of the activity's index
+
+ // Returns the integer value of the activity's index
+ int getActivityIndeks(String name) {
if(name ==null) return -1;
ArrayList<String> result = db.query("select indeks from activity a where name is '"+name+"';", "indeks");
return Integer.parseInt(result.getFirst());
}
- void addParticipation(int activityIndex) { // Inserts the given activity into participation using the activity's index
+
+ // Inserts the given activity into participation using the activity's index
+ void addParticipation(int activityIndex) {
db.cmd("insert into participation values(123, "+activityIndex+");");
}
- ArrayList<String> getParticipation(String program) { // Returns all activity names from activities currently in participation
+
+ // Returns all activity names from activities currently in participation
+ ArrayList<String> getParticipation(String program) {
return db.query("select name from participation p inner join activity a on p.indeks = a.indeks where program is '" + program + "';", "name");
}
- void clearParticipation() { // Removes all entries in the participation database
+
+ // Removes all entries in the participation database
+ void clearParticipation() {
db.cmd("delete from participation");
}
- ArrayList<String> selectProgram(String program) { // Returns an arraylist of activities within the given program
+
+ // Returns an arraylist of activities within the given program
+ ArrayList<String> selectProgram(String program) {
return db.query("select name from activity where program is '" + program + "';", "name");
}
- String getSumEcts(String program){ // Returns the sum of ECTS points under the given category from the student as a string
+
+ // Returns the sum of ECTS points under the given category from the student as a string
+ String getSumEcts(String program){
if(program==null)return "0";
ArrayList<String> result = db.query("select sum(activity.ects) as total_ects,student.name from student left outer join participation on student.studid = participation.studid inner join activity on participation.indeks = activity.indeks where program is '"+program+"' group by student.studid ;", "total_ects");
if (result.isEmpty()) return "0";