From a701fe0f29f82cdcf445cc24f79877116ca62ca4 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Mon, 28 Apr 2025 22:49:33 +0200 Subject: rename class Model -> Database; rename variable model -> store --- .../com/example/portfolio2/Controller.java | 24 +++---- .../com/example/portfolio2/Database.java | 83 ++++++++++++++++++++++ .../com/example/portfolio2/Model.java | 83 ---------------------- .../com/example/portfolio2/Window.java | 6 +- 4 files changed, 98 insertions(+), 98 deletions(-) create mode 100644 src/com.example.portfolio2/com/example/portfolio2/Database.java delete mode 100644 src/com.example.portfolio2/com/example/portfolio2/Model.java (limited to 'src/com.example.portfolio2/com/example/portfolio2') diff --git a/src/com.example.portfolio2/com/example/portfolio2/Controller.java b/src/com.example.portfolio2/com/example/portfolio2/Controller.java index 2212396..2457e25 100644 --- a/src/com.example.portfolio2/com/example/portfolio2/Controller.java +++ b/src/com.example.portfolio2/com/example/portfolio2/Controller.java @@ -14,23 +14,23 @@ import java.util.List; /// Bachelorizer - Controller class Controller{ - /// Application model - private Model model; + /// Storage model + private Database store; /// Application view private Window view; /// clear the participation database at program launch void initialize() { - model.initialize(); + store.initialize(); } /// default constructor /// - /// @param model Application model + /// @param store Storage model /// @param view Application view - Controller(Model model, Window view){ - this.model=model; this.view=view; + Controller(Database store, Window view){ + this.store = store; this.view = view; } /// callback when category box is selected @@ -46,8 +46,8 @@ class Controller{ // clear text area area.clear(); - // fill activity box using model method - select.getItems().addAll(model.selectProgram(combo.getValue())); + // fill activity box from data in store + select.getItems().addAll(store.selectProgram(combo.getValue())); } /// callback when activity box is selected @@ -87,7 +87,7 @@ class Controller{ /// @param s activity identifier /// @param textArea whole text area void addActivity(String s, TextArea textArea) { - model.addParticipation(model.getActivityIndeks(s)); + store.addParticipation(store.getActivityIndeks(s)); } /// update text area for an activity box @@ -99,7 +99,7 @@ class Controller{ /// @param textArea whole text area void updateArea(ComboBox combo, TextArea textArea) { textArea.clear(); - for(String s : model.getParticipation(combo.getValue())) { + for(String s : store.getParticipation(combo.getValue())) { textArea.appendText(s + "\n"); } } @@ -108,10 +108,10 @@ class Controller{ /// @param ectslabel text display area for ECTS points /// @param comboBox involved activity box void updateEcts(Label ectslabel, ComboBox comboBox) { - ectslabel.setText("ECTS: "+model.getSumEcts(comboBox.getValue())); + ectslabel.setText("ECTS: "+store.getSumEcts(comboBox.getValue())); } void fillElective(ComboBox electiveBox) { - electiveBox.getItems().addAll(model.getAllActivities()); + electiveBox.getItems().addAll(store.getAllActivities()); } } diff --git a/src/com.example.portfolio2/com/example/portfolio2/Database.java b/src/com.example.portfolio2/com/example/portfolio2/Database.java new file mode 100644 index 0000000..2712e3d --- /dev/null +++ b/src/com.example.portfolio2/com/example/portfolio2/Database.java @@ -0,0 +1,83 @@ +// SPDX-FileCopyrightText: +// SPDX-FileCopyrightText: +// SPDX-FileCopyrightText: +// SPDX-License-Identifier: GPL-3.0-or-later + +package com.example.portfolio2; + +import java.util.ArrayList; + +/// Bachelorizer - database model +/// +/// This model handles all interaction with the database. +class Database { + + /// database singleton + MyDB db = new MyDB(); + + /// default constructor + // (declared explicitly only to silence javadoc) + Database() {} + + /// clear the participation database at program launch + void initialize() { + clearParticipation(); + } + + /// resolve activity index from name + /// + /// @param name activity name + /// @return index of activity as integer + int getActivityIndeks(String name) { + if(name ==null) return -1; + ArrayList result = db.query("select indeks from activity a where name is '"+name+"';", "indeks"); + return Integer.parseInt(result.getFirst()); + + } + + /// insert activity into participation + /// + /// @param activityIndex index of activity + void addParticipation(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(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(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(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(); + } + + /// sum of ECTS points under the given category + /// + /// @return names of all activities as list of String + ArrayList getAllActivities() { + return db.query("select name from activity;", "name"); + } +} diff --git a/src/com.example.portfolio2/com/example/portfolio2/Model.java b/src/com.example.portfolio2/com/example/portfolio2/Model.java deleted file mode 100644 index 8216ccc..0000000 --- a/src/com.example.portfolio2/com/example/portfolio2/Model.java +++ /dev/null @@ -1,83 +0,0 @@ -// SPDX-FileCopyrightText: -// SPDX-FileCopyrightText: -// SPDX-FileCopyrightText: -// SPDX-License-Identifier: GPL-3.0-or-later - -package com.example.portfolio2; - -import java.util.ArrayList; - -/// Bachelorizer - database model -/// -/// This model handles all interaction with the database. -class Model{ - - /// database singleton - MyDB db = new MyDB(); - - /// default constructor - // (declared explicitly only to silence javadoc) - Model(){} - - /// clear the participation database at program launch - void initialize() { - clearParticipation(); - } - - /// resolve activity index from name - /// - /// @param name activity name - /// @return index of activity as integer - int getActivityIndeks(String name) { - if(name ==null) return -1; - ArrayList result = db.query("select indeks from activity a where name is '"+name+"';", "indeks"); - return Integer.parseInt(result.getFirst()); - - } - - /// insert activity into participation - /// - /// @param activityIndex index of activity - void addParticipation(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(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(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(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(); - } - - /// sum of ECTS points under the given category - /// - /// @return names of all activities as list of String - ArrayList getAllActivities() { - return db.query("select name from activity;", "name"); - } -} diff --git a/src/com.example.portfolio2/com/example/portfolio2/Window.java b/src/com.example.portfolio2/com/example/portfolio2/Window.java index 730741a..c02017b 100644 --- a/src/com.example.portfolio2/com/example/portfolio2/Window.java +++ b/src/com.example.portfolio2/com/example/portfolio2/Window.java @@ -30,11 +30,11 @@ public final class Window extends Application { public static final String LABEL_STYLE = "-fx-font-weight: bold;-fx-font-size: 18;-fx-padding: 10"; - /// Application model - private Model model = new Model(); + /// Storage model + private Database store = new Database(); /// Application controller - private Controller con = new Controller(model,this); + private Controller con = new Controller(store,this); /// database singleton MyDB myDB = new MyDB(); -- cgit v1.2.3