aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Model.java
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/example/portfolio2/Model.java
parent9d419988fa797ca431387db6a77e23832c11c6b3 (diff)
split classes into separate files
Diffstat (limited to 'src/com.example.portfolio2/com/example/portfolio2/Model.java')
-rw-r--r--src/com.example.portfolio2/com/example/portfolio2/Model.java38
1 files changed, 38 insertions, 0 deletions
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");
+ }
+}