commit 2a77c8e75a9d3215a5d02463f1de9eec74073d31 Author: minhtrannhat Date: Sat Jul 20 20:14:24 2024 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..24fa632 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:32768 + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fdc35ea --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..666ac2a --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..49bc456 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + comp353_assignment2 + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/sql/main.sql b/src/main/sql/main.sql new file mode 100644 index 0000000..1f0400e --- /dev/null +++ b/src/main/sql/main.sql @@ -0,0 +1,221 @@ +-- Create the database +CREATE DATABASE nplo_db; +USE nplo_db; + +-- Create Member table +CREATE TABLE Member +( + MemberID INT PRIMARY KEY AUTO_INCREMENT, + FirstName VARCHAR(50) NOT NULL, + LastName VARCHAR(50) NOT NULL, + MiddleInitial CHAR(1), + DateOfBirth DATE, + Address VARCHAR(255), + Gender ENUM ('Male', 'Female', 'Other'), + PhoneNumber VARCHAR(20), + EmailAddress VARCHAR(100), + SocialSecurityNumber VARCHAR(11), + MembershipStartDate DATE, + MemberType ENUM ('Donor', 'Client', 'Employee') NOT NULL +); + +-- Create Donations table +CREATE TABLE Donations +( + DonationID INT PRIMARY KEY AUTO_INCREMENT, + DonorID INT, + DonationDate DATE NOT NULL, + DonationType ENUM ('Money', 'Products') NOT NULL, + DonationAmount DECIMAL(10, 2), + FOREIGN KEY (DonorID) REFERENCES Member (MemberID) +); + +-- Create Products table +CREATE TABLE Products +( + ProductID INT PRIMARY KEY AUTO_INCREMENT, + Description TEXT, + DonationDate DATE, + SellingPrice DECIMAL(10, 2), + Weight DECIMAL(5, 2), + InStock BOOLEAN DEFAULT TRUE, + DonationID INT, + FOREIGN KEY (DonationID) REFERENCES Donations (DonationID) +); + +-- Create Sales table +CREATE TABLE Sales +( + SaleID INT PRIMARY KEY AUTO_INCREMENT, + SaleDate DATE NOT NULL, + TotalAmount DECIMAL(10, 2) NOT NULL, + ClientID INT, + DeliveryFee DECIMAL(10, 2), + FOREIGN KEY (ClientID) REFERENCES Member (MemberID) +); + +-- Create SalesItems table +CREATE TABLE SalesItems +( + SaleItemID INT PRIMARY KEY AUTO_INCREMENT, + SaleID INT, + ProductID INT, + Quantity INT, + FOREIGN KEY (SaleID) REFERENCES Sales (SaleID), + FOREIGN KEY (ProductID) REFERENCES Products (ProductID) +); + +-- Create Employees table +CREATE TABLE Employees +( + EmployeeID INT PRIMARY KEY, + JobTitle ENUM ('President', 'Vice-President', 'Cashier', 'Other') NOT NULL, + Salary DECIMAL(10, 2), + FOREIGN KEY (EmployeeID) REFERENCES Member (MemberID) +); + +-- Create Expenses table +CREATE TABLE Expenses +( + ExpenseID INT PRIMARY KEY AUTO_INCREMENT, + ApprovedByID INT, + PaymentDate DATE NOT NULL, + Amount DECIMAL(10, 2) NOT NULL, + ExpenseType ENUM ('Rent', 'Bill Payment', 'Charity Payment') NOT NULL, + Description TEXT, + FOREIGN KEY (ApprovedByID) REFERENCES Employees (EmployeeID) +); + +-- Add constraints and triggers + +-- Ensure only presidents can approve expenses +CREATE TRIGGER chk_president_approval_insert + BEFORE INSERT + ON Expenses + FOR EACH ROW +BEGIN + DECLARE job_title VARCHAR(255); + + SELECT JobTitle + INTO job_title + FROM Employees + WHERE EmployeeID = NEW.ApprovedByID; + + IF job_title != 'President' THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'ApprovedByID must refer to an employee with the job title President'; +END IF; +END; + +-- Trigger to update product stock status when sold +DELIMITER // +CREATE TRIGGER update_product_stock + AFTER INSERT + ON SalesItems + FOR EACH ROW +BEGIN + UPDATE Products SET InStock = FALSE WHERE ProductID = NEW.ProductID; +END; +// +DELIMITER ; + +-- Trigger to calculate and update delivery fee +DELIMITER // +CREATE TRIGGER calculate_delivery_fee + BEFORE INSERT + ON Sales + FOR EACH ROW +BEGIN + DECLARE total_weight DECIMAL(10, 2); + + SELECT SUM(p.Weight * si.Quantity) + INTO total_weight + FROM SalesItems si + JOIN Products p ON si.ProductID = p.ProductID + WHERE si.SaleID = NEW.SaleID; + + SET NEW.DeliveryFee = total_weight * 6.50; + SET NEW.TotalAmount = NEW.TotalAmount + NEW.DeliveryFee; +END; +// +DELIMITER ; + +-- Insert data into Member table +INSERT INTO Member (FirstName, LastName, MiddleInitial, DateOfBirth, Address, Gender, PhoneNumber, EmailAddress, + SocialSecurityNumber, MembershipStartDate, MemberType) +VALUES ('John', 'Doe', 'A', '1980-05-15', '123 Main St, Anytown, USA', 'Male', '555-123-4567', 'john.doe@email.com', + '123-45-6789', '2022-01-01', 'Donor'), + ('Jane', 'Smith', 'B', '1985-08-22', '456 Elm St, Somewhere, USA', 'Female', '555-987-6543', + 'jane.smith@email.com', '987-65-4321', '2022-02-15', 'Client'), + ('Bob', 'Johnson', 'C', '1975-03-10', '789 Oak St, Nowhere, USA', 'Male', '555-246-8135', + 'bob.johnson@email.com', '246-81-3579', '2020-03-01', 'Employee'), + ('Darton', 'Johnson', 'C', '1972-11-11', '789 Oak St, Nowhere, USA', 'Male', '561-216-8835', + 'darton.johnson@email.com', '148-46-2580', '2020-03-01', 'Employee'), + ('Alice', 'Williams', 'D', '1990-11-30', '321 Pine St, Everywhere, USA', 'Female', '555-369-2580', + 'alice.williams@email.com', '369-25-8014', '2022-04-15', 'Donor'); + +-- Insert data into Donations table +INSERT INTO Donations (DonorID, DonationDate, DonationType, DonationAmount) +VALUES (1, '2023-01-15', 'Money', 500.00), + (4, '2023-02-20', 'Products', NULL), + (1, '2023-03-10', 'Money', 250.00), + (4, '2023-04-05', 'Products', NULL); + +-- Insert data into Products table +INSERT INTO Products (Description, DonationDate, SellingPrice, Weight, InStock, DonationID) +VALUES ('Laptop', '2023-02-20', 300.00, 2.5, TRUE, 2), + ('Chair', '2023-02-20', 50.00, 5.0, TRUE, 2), + ('Bookshelf', '2023-04-05', 100.00, 20.0, TRUE, 4), + ('Microwave', '2023-04-05', 75.00, 10.0, TRUE, 4); + +-- Insert data into Sales table +INSERT INTO Sales (SaleDate, TotalAmount, ClientID, DeliveryFee) +VALUES ('2023-05-01', 350.00, 2, NULL), + ('2023-05-15', 100.00, 2, NULL); + +-- Insert data into SalesItems table +INSERT INTO SalesItems (SaleID, ProductID, Quantity) +VALUES (1, 1, 1), + (1, 2, 1), + (2, 3, 1); + +-- Insert data into Employees table +INSERT INTO Employees (EmployeeID, JobTitle, Salary) +VALUES (3, 'President', 100000.00), + (4, 'Cashier', 30000.00); + +-- Insert data into Expenses table +INSERT INTO Expenses (ApprovedByID, PaymentDate, Amount, ExpenseType, Description) +VALUES (3, '2023-06-01', 2000.00, 'Rent', 'Monthly office rent'), + (3, '2023-06-05', 500.00, 'Bill Payment', 'Electricity bill'), + (3, '2023-06-10', 1000.00, 'Charity Payment', 'Donation to local food bank'); + +-- Part 2 Question 1 +# List the details of all the clients that are not volunteer employees and donors as well. +# Details include client ID, first-name, last-name, middle-initial, date-of-birth, address, +# gender, phone-number, email-address, social-security-number, job-title, and the start date +# of membership +SELECT + m.MemberID AS ClientID, + m.FirstName, + m.LastName, + m.MiddleInitial, + m.DateOfBirth, + m.Address, + m.Gender, + m.PhoneNumber, + m.EmailAddress, + m.SocialSecurityNumber, + e.JobTitle, + m.MembershipStartDate +FROM + Member m + LEFT JOIN Employees e ON m.MemberID = e.EmployeeID +WHERE + m.MemberType = 'Client' + AND m.MemberID IN ( + SELECT DISTINCT DonorID + FROM Donations + WHERE DonorID IS NOT NULL +) + AND (e.EmployeeID IS NULL OR e.Salary > 0);