From b591061ec982ea087488afe39ce55fff6fa4bfa4 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Wed, 30 Apr 2025 22:21:28 +0200 Subject: merge project portfolio2 into bachelorizer, except class myDB --- .../dk/biks/bachelorizer/Database.java | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/dk.biks.bachelorizer/dk/biks/bachelorizer/Database.java (limited to 'src/dk.biks.bachelorizer/dk/biks/bachelorizer/Database.java') diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Database.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Database.java new file mode 100644 index 0000000..f746649 --- /dev/null +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Database.java @@ -0,0 +1,146 @@ +// SPDX-FileCopyrightText: 2025 +// SPDX-FileCopyrightText: 2025 +// SPDX-FileCopyrightText: 2025 Jonas Smedegaard +// SPDX-FileCopyrightText: 2025 +// SPDX-License-Identifier: GPL-3.0-or-later + +package dk.biks.bachelorizer; + +import java.util.ArrayList; +import java.util.List; + +import com.example.portfolio2.MyDB; + +/// Bachelorizer - database model +/// +/// This model handles all interaction with the database. +class Database { + + /// database singleton + private MyDB db = new MyDB(); + + /// default constructor + // (declared explicitly only to silence javadoc) + Database() { } + + /// student object + // TODO: replace this dummy placeholder with database query + private Person student; + + /// clear the participation database at program launch + void initialize() { + clearParticipation(); + } + + /// Add student + /// + /// @param name Name of student + // TODO: replace this dummy placeholder with database query + public final void addStudent(final String name) { + student = new Person(name); + } + + /// Get student name + /// + /// @return name of student + // TODO: replace this dummy placeholder with database query + public final String getStudentName() { + return student.name; + } + + /// resolve activity index from name + /// + /// @param name activity name + /// @return index of activity as integer + int getActivityIndeks(final String name) { + if (name == null) { + return -1; + } + ArrayList result = db.query( + "SELECT indeks FROM activity" + + " WHERE name IS '" + name + "';", + "indeks"); + + return Integer.parseInt(result.getFirst()); + } + + /// insert activity into participation + /// + /// @param activityIndex index of activity + void addParticipation(final int activityIndex) { + db.cmd("INSERT INTO participation VALUES(123, " + + activityIndex + ");"); + } + + /// list currently participating activities + /// + /// @param program programme name + /// @return names of participating activities + ArrayList getParticipation(final String program) { + return db.query( + "SELECT name FROM participation p" + + " INNER JOIN activity a ON p.indeks = a.indeks" + + " WHERE program IS '" + program + "';", + "name"); + } + + /// purge participation database + void clearParticipation() { + db.cmd("DELETE FROM participation"); + } + + /// list activities within a program + /// + /// @param program programme name + /// @return names of contained activities + ArrayList selectProgram(final String program) { + return db.query( + "SELECT name FROM activity" + + " WHERE program IS '" + program + "';", + "name"); + } + + /// sum of ECTS points under the given category + /// + /// @param program programme name + /// @return ECTS points as String + String getSumEcts(final String program) { + if (program == null) { + return "0"; + } + ArrayList 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(); + } + + /// list of available subject modules + /// + /// @return names of all subject modules as list of strings + List getAllSubjects() { + return db.query( + "SELECT DISTINCT program FROM activity" + + " WHERE program NOT IN (" + + " SELECT program from activity" + + " WHERE name LIKE 'BP1 %')", + "program"); + } + + /// list of available activities + /// + /// @return names of all activities as list of strings + ArrayList getAllActivities() { + return db.query("SELECT name FROM activity;", "name"); + } +} -- cgit v1.2.3