aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2025-04-28 16:31:25 +0200
committerJonas Smedegaard <dr@jones.dk>2025-04-28 16:39:21 +0200
commit9df1ad472f57adecb59768ffe7c847174c86ccc7 (patch)
tree6b9c460d7ccdd2dcff1f83461e069b68d8678f39 /src/com.example.portfolio2/com
parent9d419988fa797ca431387db6a77e23832c11c6b3 (diff)
split classes into separate files
Diffstat (limited to 'src/com.example.portfolio2/com')
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/Controller.java53
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java165
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/Model.java38
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/MyDb.java86
4 files changed, 177 insertions, 165 deletions
diff --git a/src/com.example.portfolio2/com/example/portfolio2/Controller.java b/src/com.example.portfolio2/com/example/portfolio2/Controller.java
new file mode 100644
index 0000000..1e6c002
--- /dev/null
+++ b/src/com.example.portfolio2/com/example/portfolio2/Controller.java
@@ -0,0 +1,53 @@
+package com.example.portfolio2;
+
+import javafx.scene.control.ComboBox;
+import javafx.scene.control.Label;
+import javafx.scene.control.TextArea;
+
+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
+ 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
+ area.clear(); // Clear text area
+ select.getItems().addAll(model.selectProgram((String) combo.getValue())); // Fill activity box using model method
+ }
+ 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
+ // users can choose from the ComboBox, string (activity) and the area would update.
+ }
+ 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())) {
+ subject2.getItems().remove(subject1.getValue());
+ } else if (!sub.equals(subject1.getValue()) && !subject2.getItems().contains(sub)) {
+ subject2.getItems().add(sub);
+ }
+ }
+ }
+ void addActivity(String s, TextArea textArea) { // Calls on model method to add participation in the database
+ model.addParticipation(model.getActivityIndeks(s));
+ }
+ void updateArea(ComboBox combo, TextArea textArea) { // Clears the text area and adds all activity names from activities in participation
+ 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
+ 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 300f626..eb4b49d 100644
--- a/src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java
+++ b/src/com.example.portfolio2/com/example/portfolio2/HelloApplication.java
@@ -10,8 +10,6 @@ import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
-import java.sql.*;
-import java.util.ArrayList;
import java.util.List;
public class HelloApplication extends Application {
@@ -153,166 +151,3 @@ public class HelloApplication extends Application {
}
}
}
-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
- 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
- area.clear(); // Clear text area
- select.getItems().addAll(model.selectProgram((String) combo.getValue())); // Fill activity box using model method
- }
- 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
- // users can choose from the ComboBox, string (activity) and the area would update.
- }
- 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())) {
- subject2.getItems().remove(subject1.getValue());
- } else if (!sub.equals(subject1.getValue()) && !subject2.getItems().contains(sub)) {
- subject2.getItems().add(sub);
- }
- }
- }
- void addActivity(String s, TextArea textArea) { // Calls on model method to add participation in the database
- model.addParticipation(model.getActivityIndeks(s));
- }
- void updateArea(ComboBox combo, TextArea textArea) { // Clears the text area and adds all activity names from activities in participation
- 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
- ectslabel.setText("ECTS: "+model.getSumEcts(comboBox.getValue()));
- }
- void fillElective(ComboBox<String> electiveBox) {
- electiveBox.getItems().addAll(model.getAllActivities());
- }
-}
-
-class Model{ // The model handles all interactions with the database
- MyDB db=new MyDB();
- Model(){}
- void initialize() { // When running the program, it clears the participation database
- clearParticipation();
- }
- int getActivityIndeks(String name) { // Returns the integer value of the activity's index
- 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
- db.cmd("insert into participation values(123, "+activityIndex+");");
- }
- ArrayList<String> getParticipation(String program) { // Returns all activity names from activities currently in participation
- 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
- db.cmd("delete from participation");
- }
- ArrayList<String> selectProgram(String program) { // Returns an arraylist of activities within the given 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
- 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";
- return result.getFirst();
- }
- ArrayList<String> getAllActivities() {
- return db.query("select name from activity;", "name");
- }
-}
-
-class MyDB { // MyDB is all standard Database configuration that was gotten from Mads
- Connection conn = null;
-
- MyDB() {
- if (conn == null) open();
- }
-
- public void open() {
- try {
- String url = "jdbc:sqlite:identifier.sqlite";
- conn = DriverManager.getConnection(url);
- } catch (SQLException e) {
- System.out.println("cannot open");
- if (conn != null) close();
- throw new RuntimeException(e);
- }
- ;
- }
-
- public void close() {
- try {
- if (conn != null) conn.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- conn = null;
- }
- public void cmd(String sql) {
- if (conn == null) open();
- if (conn == null) {
- System.out.println("No connection");
- return;
- }
- Statement stmt = null;
- try {
- stmt = conn.createStatement();
- stmt.executeUpdate(sql);
- } catch (SQLException e) {
- System.out.println("Error in statement " + sql);
- throw new RuntimeException(e);
- }
- try {
- if (stmt != null) {
- stmt.close();
- }
- } catch (SQLException e) {
- System.out.println("Error in statement " + sql);
- throw new RuntimeException(e);
- }
- }
-
- public ArrayList<String> query(String query, String fld) {
- ArrayList<String> res = new ArrayList<>();
- if (conn == null) open();
- if (conn == null) {
- System.out.println("No connection");
- throw new RuntimeException("No connection");
- }
- Statement stmt = null;
- try {
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) {
- String name = rs.getString(fld);
- res.add(name);
- }
- } catch (SQLException e) {
- System.out.println("Error in statement " + query + " " + fld);
- throw new RuntimeException(e);
- }
- try {
- if (stmt != null) {
- stmt.close();
- }
- } catch (SQLException e) {
- System.out.println("Error in statement " + query + " " + fld);
- throw new RuntimeException(e);
- }
- return res;
- }
-}
-
diff --git a/src/com.example.portfolio2/com/example/portfolio2/Model.java b/src/com.example.portfolio2/com/example/portfolio2/Model.java
new file mode 100644
index 0000000..950b4f4
--- /dev/null
+++ b/src/com.example.portfolio2/com/example/portfolio2/Model.java
@@ -0,0 +1,38 @@
+package com.example.portfolio2;
+
+import java.util.ArrayList;
+
+class Model{ // The model handles all interactions with the database
+ MyDB db=new MyDB();
+ Model(){}
+ void initialize() { // When running the program, it clears the participation database
+ clearParticipation();
+ }
+ int getActivityIndeks(String name) { // Returns the integer value of the activity's index
+ 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
+ db.cmd("insert into participation values(123, "+activityIndex+");");
+ }
+ ArrayList<String> getParticipation(String program) { // Returns all activity names from activities currently in participation
+ 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
+ db.cmd("delete from participation");
+ }
+ ArrayList<String> selectProgram(String program) { // Returns an arraylist of activities within the given 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
+ 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";
+ return result.getFirst();
+ }
+ ArrayList<String> getAllActivities() {
+ return db.query("select name from activity;", "name");
+ }
+}
diff --git a/src/com.example.portfolio2/com/example/portfolio2/MyDb.java b/src/com.example.portfolio2/com/example/portfolio2/MyDb.java
new file mode 100644
index 0000000..f85f391
--- /dev/null
+++ b/src/com.example.portfolio2/com/example/portfolio2/MyDb.java
@@ -0,0 +1,86 @@
+package com.example.portfolio2;
+
+import java.sql.*;
+import java.util.ArrayList;
+
+class MyDB { // MyDB is all standard Database configuration that was gotten from Mads
+ Connection conn = null;
+
+ MyDB() {
+ if (conn == null) open();
+ }
+
+ public void open() {
+ try {
+ String url = "jdbc:sqlite:identifier.sqlite";
+ conn = DriverManager.getConnection(url);
+ } catch (SQLException e) {
+ System.out.println("cannot open");
+ if (conn != null) close();
+ throw new RuntimeException(e);
+ }
+ ;
+ }
+
+ public void close() {
+ try {
+ if (conn != null) conn.close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ conn = null;
+ }
+ public void cmd(String sql) {
+ if (conn == null) open();
+ if (conn == null) {
+ System.out.println("No connection");
+ return;
+ }
+ Statement stmt = null;
+ try {
+ stmt = conn.createStatement();
+ stmt.executeUpdate(sql);
+ } catch (SQLException e) {
+ System.out.println("Error in statement " + sql);
+ throw new RuntimeException(e);
+ }
+ try {
+ if (stmt != null) {
+ stmt.close();
+ }
+ } catch (SQLException e) {
+ System.out.println("Error in statement " + sql);
+ throw new RuntimeException(e);
+ }
+ }
+
+ public ArrayList<String> query(String query, String fld) {
+ ArrayList<String> res = new ArrayList<>();
+ if (conn == null) open();
+ if (conn == null) {
+ System.out.println("No connection");
+ throw new RuntimeException("No connection");
+ }
+ Statement stmt = null;
+ try {
+ stmt = conn.createStatement();
+ ResultSet rs = stmt.executeQuery(query);
+ while (rs.next()) {
+ String name = rs.getString(fld);
+ res.add(name);
+ }
+ } catch (SQLException e) {
+ System.out.println("Error in statement " + query + " " + fld);
+ throw new RuntimeException(e);
+ }
+ try {
+ if (stmt != null) {
+ stmt.close();
+ }
+ } catch (SQLException e) {
+ System.out.println("Error in statement " + query + " " + fld);
+ throw new RuntimeException(e);
+ }
+ return res;
+ }
+}