initial commit

This commit is contained in:
minhtrannhat 2024-07-20 20:14:24 -04:00
commit 2a77c8e75a
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
10 changed files with 342 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@ -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

8
.idea/.gitignore generated vendored Normal file
View File

@ -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

17
.idea/dataSources.xml generated Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="@localhost" uuid="ab0522bd-1cfb-4e09-a121-85218f5838de">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:32768</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

7
.idea/discord.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

7
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

7
.idea/sqldialects.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/main/sql/main.sql" dialect="MySQL" />
<file url="PROJECT" dialect="MySQL" />
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

17
pom.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>comp353_assignment2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

221
src/main/sql/main.sql Normal file
View File

@ -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);