add files

This commit is contained in:
s490081 2024-06-12 09:02:48 +02:00
parent ce5d396c37
commit 0dae2bdd8d
8 changed files with 550 additions and 0 deletions

33
.gitignore vendored Normal file
View File

@ -0,0 +1,33 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
/src/jtds-1.3.1.jar
/src/mssql-jdbc-12.6.1.jre8.jar
/src/mssql-jdbc-12.6.1.jre11.jar
/BD.iml

262
src/Main.java Normal file
View File

@ -0,0 +1,262 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
static boolean tableExistsSQL(Connection connection, String tableName) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT TOP 1 count(*) "
+ "FROM information_schema.tables "
+ "WHERE table_name = ?"
+ ";");
preparedStatement.setString(1, tableName);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
return resultSet.getInt(1) != 0;
}
public static void addTask(Connection conn, Task task) {
if (conn != null) {
try {
String sql = "INSERT INTO TODO (task, deadline, waga) VALUES (?, ?, ?)";
PreparedStatement insert = conn.prepareStatement(sql);
insert.setString(1, task.getTask());
insert.setString(2, task.getDeadline());
insert.setInt(3, task.getWaga());
insert.execute();
} catch (SQLException e) {
throw new RuntimeException("Error adding task: " + e.getMessage(), e);
}
}
}
public static void show(List<Task> tasks) {
tasks.forEach(System.out::println);
}
public static void deleteTask(Connection conn, int id) {
try {
String sql = "DELETE FROM TODO WHERE id = ?";
PreparedStatement psDel = conn.prepareStatement(sql);
psDel.setInt(1, id);
psDel.execute();
} catch (SQLException e) {
throw new RuntimeException("Error deleting task: " + e.getMessage(), e);
}
}
public static List<Task> filter(List<Task> tasks, String like) {
return tasks.stream()
.filter(task -> task.getTask().contains(like))
.collect(Collectors.toList());
}
public static List<Task> getTasks(Connection conn) throws SQLException {
if (conn != null) {
if (tableExistsSQL(conn,"TODO")) {
try {
String sql = "SELECT * FROM TODO;";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
List<Task> tasks = new ArrayList<>();
while (rs.next()) {
Task task = new Task();
task.setId(rs.getInt("id"));
task.setTask(rs.getString("task"));
task.setDeadline(rs.getString("deadline"));
task.setWaga(rs.getInt("waga"));
tasks.add(task);
}
return tasks;
} catch (SQLException e) {
throw new RuntimeException("Error retrieving tasks: " + e.getMessage(), e);
}
}
else{
try {
String sql = "CREATE TABLE TODO " +
" ( " +
" id int IDENTITY(1,1) NOT NULL CONSTRAINT TODO_PK PRIMARY KEY," +
" task VARCHAR (40) NOT NULL, " +
" deadline DATETIME," +
" waga INT" +
" );";
PreparedStatement psDel = conn.prepareStatement(sql);
psDel.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Error creating table: " + e.getMessage(), e);
}
}
}
return Collections.emptyList();
}
public static Connection connect() {
Connection conn = null;
String configPath = "src/config.properties";
try (FileInputStream propInput = new FileInputStream(configPath)) {
Properties properties = new Properties();
properties.load(propInput);
String url = "jdbc:sqlserver://" + properties.getProperty("DB_URL") + ";database=" + properties.getProperty("DB_DB");
String user = properties.getProperty("DB_USER");
String password = properties.getProperty("DB_PASSWORD");
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException | IOException e) {
throw new RuntimeException("Error connecting to database: " + e.getMessage(), e);
}
return conn;
}
public static void layout() {
List<String> labels = Arrays.asList(
"[1] Dodaj task",
"[2] Usuń task",
"[3] Sortuj (data)",
"[4] Sortuj (waga)",
"[5] Filtruj",
"[6] Generuj HTML",
"[7] Generuj Prolog",
"[0] Wyjdź"
);
labels.forEach(System.out::println);
}
public static void genHTML(List<Task> tasks) {
try {
File template = new File("template.html");
Scanner reader = new Scanner(template);
File outFile = new File("todo.html");
if (outFile.createNewFile()) {
System.out.println("Nowy plik: " + outFile.getName());
}
FileWriter writer = new FileWriter(outFile);
String line;
while (reader.hasNextLine() && !(line = reader.nextLine()).trim().equals("<!-- content -->")) {
writer.write(line + System.lineSeparator());
}
for (Task task : tasks) {
writer.write("<tr>" + System.lineSeparator());
writer.write("<td>" + task.getId() + "</td>" + System.lineSeparator());
writer.write("<td>" + task.getTask() + "</td>" + System.lineSeparator());
writer.write("<td>" + task.getDeadline() + "</td>" + System.lineSeparator());
writer.write("<td>" + task.getWaga() + "</td>" + System.lineSeparator());
writer.write("</tr>" + System.lineSeparator());
}
while (reader.hasNextLine()) {
writer.write(reader.nextLine() + System.lineSeparator());
}
reader.close();
writer.close();
System.out.println("Wygenerowano.");
} catch (IOException e) {
System.out.println("An error occurred while generating HTML.");
e.printStackTrace();
}
}
public static void genProlog(List<Task> tasks){
try{
File outFile = new File("todo.pl");
if (outFile.createNewFile()) {
System.out.println("Nowy plik: " + outFile.getName());
}
FileWriter writer = new FileWriter(outFile);
tasks.forEach(task -> {
try {
writer.write(
"task(\'" + task.getTask() + "\', date(" + task.getDeadline().substring(0, 4) + "," + task.getDeadline().substring(5, 7) + "," + task.getDeadline().substring(8, 10) + ")" + "," + task.getWaga() + ")." + System.lineSeparator());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
writer.write("spoznione(X) :- task(X,Y,_),date_time_stamp(Y,S),get_time(T),S<T."+System.lineSeparator());
writer.write("najwazniejsze(X,Y,Z) :- task(X, Y, Z), \\+ (task(_, _, V), V < Z)."+System.lineSeparator());
writer.close();
System.out.println("Wygenerowano.");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void menu() throws SQLException {
Scanner input = new Scanner(System.in);
Connection conn = connect();
if (conn != null) {
int option;
List<Task> tasks = getTasks(conn);
while (true) {
show(tasks);
layout();
option = input.nextInt();
switch (option) {
case 1:
System.out.println("Task: ");
String taskName = input.next();
System.out.println("Deadline (yyyy-mm-dd): ");
String deadline = input.next();
System.out.println("Waga: ");
int waga = input.nextInt();
addTask(conn, new Task(taskName, deadline, waga));
tasks = getTasks(conn);
break;
case 2:
System.out.println("ID: ");
int id = input.nextInt();
deleteTask(conn, id);
tasks = getTasks(conn);
break;
case 3:
tasks.sort(Comparator.comparing(Task::getDeadline));
break;
case 4:
tasks.sort(Comparator.comparing(Task::getWaga));
break;
case 5:
System.out.println("Filtruj: ");
String like = input.next();
tasks = filter(getTasks(conn), like);
break;
case 6:
genHTML(tasks);
break;
case 7:
genProlog(tasks);
break;
case 0:
conn.close();
return;
default:
System.out.println("Invalid option.");
break;
}
}
}
}
public static void main(String[] args) throws SQLException {
menu();
}
}

57
src/Task.java Normal file
View File

@ -0,0 +1,57 @@
public class Task{
int id;
private String task;
private String deadline;
private int waga;
public Task() {
}
public Task( String task, String deadline, int waga) {
this.task = task;
this.deadline = deadline;
this.waga = waga;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTask() {
return task;
}
@Override
public String toString() {
return "[\t" +
"id=" + id +
",\t" + task +
",\t" + deadline +
",\t" + waga +
"\t]";
}
public void setTask(String task) {
this.task = task;
}
public String getDeadline() {
return deadline;
}
public void setDeadline(String deadline) {
this.deadline = deadline;
}
public int getWaga() {
return waga;
}
public void setWaga(int waga) {
this.waga = waga;
}
}

4
src/config.properties Normal file
View File

@ -0,0 +1,4 @@
DB_URL=mssql-2017.labs.wmi.amu.edu.pl
DB_DB=
DB_USER=
DB_PASSWORD=

102
src/style.css Normal file
View File

@ -0,0 +1,102 @@
body {
padding: 0;
background-color: white;
margin: 0;
}
.container {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
margin: 0;
}
.topbar {
padding: 5px;
background-color: rgb(0, 183, 255);
border-bottom-right-radius: 20px;
border-bottom-left-radius: 20px;
text-align: center;
color: darkblue;
}
.btn {
height: 20px;
width: 170px;
text-align: center;
padding: 10px 20px;
background-color: rgb(0, 183, 255);
border-radius: 20px;
color: white;
font-weight: bolder;
border: 2px white solid;
}
a {
text-decoration: none;
}
.content {
margin: 0 auto;
padding: 0% 1vw;
}
.wrapper {
text-align: justify;
width: 60vw;
margin: 0 auto;
padding: 1vh 18vw;
align-items: center;
}
p {
vertical-align: top;
height: 100%;
padding: 1vw;
background-color: #eee;
border-radius: 30px;
}
img {
vertical-align: top;
padding-top: 1vh;
margin: 1vh 1vw;
border-radius: 30px;
}
table {
border-collapse: collapse;
margin: 0 auto;
padding-top: 2vh;
}
hr {
width: 55vw;
height: 1px;
background-color: rgb(0, 183, 255);
}
td,
th {
border: 1px solid #ddd;
padding: 8px;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
th {
padding-top: 10px;
padding-bottom: 10px;
text-align: left;
background-color: darkblue;
color: white;
}
#gallery>img {
border-radius: 0;
}

35
template.html Normal file
View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Trasy</title>
</head>
<body>
<div class="container">
<div class="topbar">
<h1>TODO</h1>
</div>
<div class="content">
<table>
<thead>
<tr>
<th>ID</th>
<th>Task</th>
<th>Deadline</th>
<th>Waga</th>
</tr>
</thead>
<tbody>
<!-- content -->
</tbody>
</table>
</div>
</div>
</body>
</html>

52
todo.html Normal file
View File

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Trasy</title>
</head>
<body>
<div class="container">
<div class="topbar">
<h1>TODO</h1>
</div>
<div class="content">
<table>
<thead>
<tr>
<th>ID</th>
<th>Task</th>
<th>Deadline</th>
<th>Waga</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>kolokwium</td>
<td>2024-06-17 00:00:00.0</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>projekt_paradygmaty</td>
<td>2024-06-16 00:00:00.0</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>projekt_BD</td>
<td>2024-06-20 00:00:00.0</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>

5
todo.pl Normal file
View File

@ -0,0 +1,5 @@
task('kolokwium', date(2024,06,17),1).
task('projekt_paradygmaty', date(2024,06,16),0).
task('projekt_BD', date(2024,06,20),2).
spoznione(X) :- task(X,Y,_),date_time_stamp(Y,S),get_time(T),S<T.
najwazniejsze(X,Y,Z) :- task(X, Y, Z), \+ (task(_, _, V), V < Z).