diff --git a/.gitignore b/.gitignore
index 1847a1b..938e292 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,4 +27,5 @@
hs_err_pid*
replay_pid*
-# End of https://www.toptal.com/developers/gitignore/api/java
\ No newline at end of file
+# End of https://www.toptal.com/developers/gitignore/api/java
+/target/
diff --git a/.vscode/settings.json b/.vscode/settings.json
deleted file mode 100644
index e112a70..0000000
--- a/.vscode/settings.json
+++ /dev/null
@@ -1,7 +0,0 @@
-{
- "java.project.sourcePaths": ["src"],
- "java.project.outputPath": "bin",
- "java.project.referencedLibraries": [
- "lib/**/*.jar"
- ]
-}
diff --git a/db/user.csv b/db/user.csv
deleted file mode 100644
index 6ae1a25..0000000
--- a/db/user.csv
+++ /dev/null
@@ -1,2 +0,0 @@
-Andres;a;aae9de2837a66715c291a3c2cea9930cbd48d0e9ce07dbd905a82435da5182ea7363eb51d219ee56fe5e0d8acd573916a930cdd3732e1bc1a6bbff965efcfb00
-Ruben;43h;aae9de2837a66715c291a3c2cea9930cbd48d0e9ce07dbd905a82435da5182ea7363eb51d219ee56fe5e0d8acd573916a930cdd3732e1bc1a6bbff965efcfb00
diff --git a/filedbejer4 b/filedbejer4
new file mode 100644
index 0000000..2778e68
--- /dev/null
+++ b/filedbejer4
@@ -0,0 +1 @@
+Manu�|caca|75858548574
diff --git a/nbactions.xml b/nbactions.xml
new file mode 100644
index 0000000..63ec457
--- /dev/null
+++ b/nbactions.xml
@@ -0,0 +1,55 @@
+
+
+
+ run
+
+ jar
+
+
+ process-classes
+ org.codehaus.mojo:exec-maven-plugin:3.0.0:exec
+
+
+
+ ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}
+
+ com.curso.baptisterio.java.App
+ java
+
+
+
+ debug
+
+ jar
+
+
+ process-classes
+ org.codehaus.mojo:exec-maven-plugin:3.0.0:exec
+
+
+ -agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}
+ ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}
+
+ com.curso.baptisterio.java.App
+ java
+ true
+
+
+
+ profile
+
+ jar
+
+
+ process-classes
+ org.codehaus.mojo:exec-maven-plugin:3.0.0:exec
+
+
+
+ ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}
+ com.curso.baptisterio.java.App
+ java
+
+
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..2d4d57c
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,14 @@
+
+
+ 4.0.0
+ com.curso
+ Baptisterio-Java
+ 1.0-SNAPSHOT
+ jar
+
+ UTF-8
+ 19
+ 19
+ com.curso.baptisterio.java.BaptisterioJava
+
+
\ No newline at end of file
diff --git a/src/User.java b/src/User.java
deleted file mode 100644
index 5a66dc4..0000000
--- a/src/User.java
+++ /dev/null
@@ -1,37 +0,0 @@
-public class User {
- // dni, name
- private String dni;
- private String name;
-
- public User(String name, String dni){
- this.name = name;
- this.dni = dni;
- }
-
- public String getDni() {
- return dni;
- }
-
- public String getName() {
- return name;
- }
-
- public void setDni(String dni) {
- this.dni = dni;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
-
- User o = (User) obj;
- return o.getDni().equals(dni) && o.getName().equals(name);
- }
-
-}
diff --git a/src/App.java b/src/main/java/com/curso/baptisterio/java/App.java
similarity index 83%
rename from src/App.java
rename to src/main/java/com/curso/baptisterio/java/App.java
index 5ed47ef..eb35a52 100644
--- a/src/App.java
+++ b/src/main/java/com/curso/baptisterio/java/App.java
@@ -1,3 +1,5 @@
+
+package com.curso.baptisterio.java;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
@@ -46,8 +48,14 @@ public class App {
static Baptisterio baptisterio = new Baptisterio();
public static void main(String[] args) throws Exception {
+ ///////////////////////auth creation
+
+ AuthSystem auth= new AuthSystem();
+ User logged= auth.run();
+
+ /////////////////////
System.out.print("¿¿¿A quién no le va a gustar un buen baptisterio???");
-
+ System.out.println("HOLA "+logged.getUserName());
var option = MenuOptions.NOOP;
Scanner scan = new Scanner(System.in);
Exception error = null;
@@ -80,9 +88,10 @@ public static void main(String[] args) throws Exception {
}
switch (option) {
+
case BOOK -> {
try {
- book(scan);
+ book(scan,logged);
}
catch (DateTimeException e) {
error = e;
@@ -91,7 +100,7 @@ public static void main(String[] args) throws Exception {
error = e;
}
}
- case CANCEL -> cancel(scan);
+ case CANCEL -> cancel(scan,logged);
case EXIT -> System.out.println("¡Hasta luego!");
case LIST -> baptisterio.printReservations();
case NOOP -> {}
@@ -118,21 +127,21 @@ private static LocalDateWrapper readDate(Scanner scan) throws DateTimeException{
}
- private static User readUser(Scanner scan){
- System.out.println("¿Cuál es tu nombre?");
- System.out.print("> ");
- String name = scan.nextLine();
-
- System.out.println("¿Cuál es tu DNI?");
- System.out.print("> ");
- String dni = scan.nextLine();
-
- User user = new User(name, dni);
- return user;
- }
+// private static User readUser(Scanner scan){
+// System.out.println("¿Cuál es tu nombre?");
+// System.out.print("> ");
+// String name = scan.nextLine();
+//
+// System.out.println("¿Cuál es tu DNI?");
+// System.out.print("> ");
+// String dni = scan.nextLine();
+//
+// User user = new User(name, dni);
+// return user;
+// }
- private static void book (Scanner scan) throws Exception, DateTimeException {
+ private static void book (Scanner scan, User logged) throws Exception, DateTimeException {
LocalDateWrapper date = null;
try {
@@ -149,8 +158,9 @@ private static void book (Scanner scan) throws Exception, DateTimeException {
throw new Exception("La fecha introducida no está disponible");
}
- User user = readUser(scan);
-
+ //User user = readUser(scan);
+ User user=logged;
+
int round = 0;
int remaining = baptisterio.PRICE;
ArrayList coins = new ArrayList();
@@ -182,7 +192,7 @@ private static void book (Scanner scan) throws Exception, DateTimeException {
Thread.sleep(2000);
}
- private static void cancel (Scanner scan) throws InterruptedException {
+ private static void cancel (Scanner scan, User logged) throws InterruptedException {
LocalDateWrapper date = null;
try {
@@ -192,7 +202,8 @@ private static void cancel (Scanner scan) throws InterruptedException {
throw e;
}
- User user = readUser(scan);
+ //User user = readUser(scan);
+ User user = logged;
if (baptisterio.cancel(user, date)) {
System.out.println("Su cita se ha cancelado con éxito!");
diff --git a/src/main/java/com/curso/baptisterio/java/AuthSystem.java b/src/main/java/com/curso/baptisterio/java/AuthSystem.java
new file mode 100644
index 0000000..0bb5e44
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/AuthSystem.java
@@ -0,0 +1,102 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+import static com.curso.baptisterio.java.Constants.*;
+
+/**
+ *
+ * @author dpadilla
+ */
+public class AuthSystem {
+
+ UserInterface ui;
+ DB db;
+ User loggedU;
+ boolean loggedCorrect;
+
+ public AuthSystem() {
+ ui = new UserInterface();
+ db = new DB(FILEDATABASE);
+ loggedCorrect = false;
+ }
+
+ public User run() {
+ boolean exit = false;
+ int option = DEFAULTOPTION;
+ db.start();
+
+ while (!exit) {
+ System.out.println(MENU);
+ option = ui.show();
+
+ switch (option) {
+ case LOGIN:
+ login();
+ break;
+ case REGISTER:
+ register();
+ break;
+ default:
+ System.out.println("Invalid option");
+ }
+
+ if (loggedCorrect) {
+ exit = true;
+ }
+
+ }
+ return loggedU;
+
+ }
+
+ private void register() {
+ User user = null;
+ boolean canRegister = false;
+
+ while (!canRegister) {
+ user = ui.askName();
+ if (!db.exist(user)) {
+ canRegister = true;
+ } else {
+ System.out.println("User already taken");
+ }
+ }
+ user.setDni(ui.askDNI());
+ user.setPass(ui.askPasword());
+
+ db.add(user);
+
+ System.out.println("Register succesfull");
+ }
+
+ private void login() {
+ User user = null;
+ boolean canLogin = false;
+ boolean correct = true;
+ String pasword;
+
+ user = ui.askName();
+ if (!db.exist(user)) {
+ correct = false;
+ }
+
+ pasword = ui.askPasword();
+
+ if (correct) {
+ correct = db.check(user, pasword);
+ }
+
+ if (correct) {
+ loggedU = db.getUser(user.getUserName());
+ System.out.println(USERWELCOME);
+ loggedCorrect = true;
+ } else {
+ System.out.println(USERERROR);
+ }
+
+ }
+
+}
diff --git a/src/Baptisterio.java b/src/main/java/com/curso/baptisterio/java/Baptisterio.java
similarity index 94%
rename from src/Baptisterio.java
rename to src/main/java/com/curso/baptisterio/java/Baptisterio.java
index 3caa624..9c050a7 100644
--- a/src/Baptisterio.java
+++ b/src/main/java/com/curso/baptisterio/java/Baptisterio.java
@@ -1,3 +1,4 @@
+package com.curso.baptisterio.java;
import java.util.ArrayList;
import java.util.HashMap;
@@ -53,7 +54,7 @@ public void printReservations() {
for (var date : reservations.keySet()) {
System.out.println(date + " -> "
- + reservations.get(date).getName()
+ + reservations.get(date).getUserName()
+ " ("+reservations.get(date).getDni() + ")"
);
}
diff --git a/src/CoinEuro.java b/src/main/java/com/curso/baptisterio/java/CoinEuro.java
similarity index 97%
rename from src/CoinEuro.java
rename to src/main/java/com/curso/baptisterio/java/CoinEuro.java
index 45f49fb..8476348 100644
--- a/src/CoinEuro.java
+++ b/src/main/java/com/curso/baptisterio/java/CoinEuro.java
@@ -1,3 +1,4 @@
+package com.curso.baptisterio.java;
import java.util.ArrayList;
public enum CoinEuro { // En céntimos
diff --git a/src/main/java/com/curso/baptisterio/java/Constants.java b/src/main/java/com/curso/baptisterio/java/Constants.java
new file mode 100644
index 0000000..882e599
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/Constants.java
@@ -0,0 +1,21 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+/**
+ *
+ * @author dpadilla
+ */
+public class Constants {
+ public static final String SEPARE = "|";
+ public static final String FILEDATABASE = "filedbejer4";
+ public static final String MENU = "1-> Login\n2-> Register\n";
+ public static final String USERERROR = "User or pasword not correct";
+ public static final String USERWELCOME = "You have been logged correctly =D";
+ public static final int DEFAULTOPTION = 0;
+ public static final int LOGIN = 1;
+ public static final int REGISTER = 2;
+ public static final int EXIT = 3;
+}
diff --git a/src/main/java/com/curso/baptisterio/java/DB.java b/src/main/java/com/curso/baptisterio/java/DB.java
new file mode 100644
index 0000000..8351244
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/DB.java
@@ -0,0 +1,64 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author dpadilla
+ */
+public class DB {
+ HashMap db;
+ Reader reader;
+ Writer writer;
+
+ public DB(String fileName) {
+ db = new HashMap<>();
+ reader = new Reader(fileName);
+ writer = new Writer(fileName);
+ }
+
+ public void start(){
+ ArrayList users=null;
+ try {
+ users = reader.readFile();
+ } catch (FileNotFoundException ex) {
+ Logger.getLogger(DB.class.getName()).info("Database reader, file not found");
+ }
+
+ for (User user : users) {
+ db.put(user.getUserName(), user);
+ }
+ }
+
+ public boolean exist(User s){
+ return db.containsKey(s.getUserName());
+ }
+
+ public void add(User s){
+ db.put(s.getUserName(), s);
+ try {
+ writer.writeFile(db);
+ } catch (IOException ex) {
+ Logger.getLogger(DB.class.getName()).info("Database writer, file not found");
+ }
+ }
+
+ public boolean check(User user, String pasword) {
+ return pasword.equals(db.get(user.getUserName()).getPass());
+ }
+
+ public User getUser(String name){
+ return db.get(name);
+ }
+
+
+}
diff --git a/src/LocalDateWrapper.java b/src/main/java/com/curso/baptisterio/java/LocalDateWrapper.java
similarity index 97%
rename from src/LocalDateWrapper.java
rename to src/main/java/com/curso/baptisterio/java/LocalDateWrapper.java
index 0159a49..46e156a 100644
--- a/src/LocalDateWrapper.java
+++ b/src/main/java/com/curso/baptisterio/java/LocalDateWrapper.java
@@ -1,3 +1,4 @@
+package com.curso.baptisterio.java;
import java.time.LocalDate;
public class LocalDateWrapper {
diff --git a/src/Payment.java b/src/main/java/com/curso/baptisterio/java/Payment.java
similarity index 93%
rename from src/Payment.java
rename to src/main/java/com/curso/baptisterio/java/Payment.java
index bdf3b78..888d726 100644
--- a/src/Payment.java
+++ b/src/main/java/com/curso/baptisterio/java/Payment.java
@@ -1,3 +1,4 @@
+package com.curso.baptisterio.java;
import java.util.ArrayList;
public class Payment {
diff --git a/src/main/java/com/curso/baptisterio/java/Reader.java b/src/main/java/com/curso/baptisterio/java/Reader.java
new file mode 100644
index 0000000..bbe981a
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/Reader.java
@@ -0,0 +1,61 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+import static com.curso.baptisterio.java.Constants.*;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Scanner;
+
+
+/**
+ *
+ * @author dpadilla
+ */
+public class Reader {
+
+ private Scanner reader;
+ private String fileName;
+
+ public Reader(String file) {
+ this.fileName = file;
+ }
+
+ public ArrayList readFile() throws FileNotFoundException {
+ ArrayList fileUser = new ArrayList<>();
+
+ File file = new File(fileName);
+ reader = new Scanner(file);
+
+ String line;
+ String[] words;
+
+ while (reader.hasNext()) {
+ line = reader.nextLine();
+
+ if (line != null) {
+ words = line.split("\\Q" + SEPARE);
+
+ /////////////////
+ String pass = words[1];
+ String userName = words[0];
+ String dni = words[2];
+
+ User toAdd = new User(userName, pass,dni);
+ fileUser.add(toAdd);
+ }
+
+ }
+ reader.close();
+ return fileUser;
+ }
+
+ public void setFileName(String fileName) {
+ this.fileName = fileName;
+ }
+
+
+}
diff --git a/src/TransactionRecord.java b/src/main/java/com/curso/baptisterio/java/TransactionRecord.java
similarity index 92%
rename from src/TransactionRecord.java
rename to src/main/java/com/curso/baptisterio/java/TransactionRecord.java
index 5bda193..eb6fe2c 100644
--- a/src/TransactionRecord.java
+++ b/src/main/java/com/curso/baptisterio/java/TransactionRecord.java
@@ -1,3 +1,5 @@
+package com.curso.baptisterio.java;
+
public class TransactionRecord {
private User user;
private LocalDateWrapper date;
diff --git a/src/main/java/com/curso/baptisterio/java/User.java b/src/main/java/com/curso/baptisterio/java/User.java
new file mode 100644
index 0000000..90bc08d
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/User.java
@@ -0,0 +1,61 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+import static com.curso.baptisterio.java.Constants.*;
+
+/**
+ *
+ * @author dpadilla
+ */
+public class User {
+ private String userName;
+ private String pass;
+ private String dni;
+
+ public User(String userName, String pass, String dni) {
+ this.userName = userName;
+ this.pass = pass;
+ this.dni=dni;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ public String getPass() {
+ return pass;
+ }
+
+ public void setPass(String pass) {
+ this.pass = pass;
+ }
+
+ public String getDni() {
+ return dni;
+ }
+
+ public void setDni(String dni) {
+ this.dni = dni;
+ }
+
+
+
+ @Override
+ public String toString() {
+ return this.getUserName() + SEPARE + this.getPass() + SEPARE + this.getDni() + "\n";
+ }
+
+
+
+
+
+
+
+}
diff --git a/src/main/java/com/curso/baptisterio/java/UserInterface.java b/src/main/java/com/curso/baptisterio/java/UserInterface.java
new file mode 100644
index 0000000..095f857
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/UserInterface.java
@@ -0,0 +1,80 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+import static com.curso.baptisterio.java.Constants.*;
+import java.util.Scanner;
+
+/**
+ *
+ * @author dpadilla
+ */
+public class UserInterface {
+
+ private Scanner keyboard;
+
+ public UserInterface() {
+ this.keyboard = new Scanner(System.in);
+ }
+
+ public int show() {
+ //variable creation
+ boolean choosed = false;
+ int option = DEFAULTOPTION;
+ while (!choosed) {
+ System.out.println("Choose an option: ");
+ try {
+ option = Integer.parseInt(keyboard.nextLine());
+ choosed = true;
+
+ } catch (NumberFormatException e) {
+ choosed = true;
+ System.out.println("Option format not valid");
+
+ }
+ }
+
+ return option;
+ }
+
+ public User askName() {
+ //variable creation
+ String line = "|";
+ while (line.contains(SEPARE)) {
+ System.out.println("Insert a valid name: ");
+ line = keyboard.nextLine();
+
+ }
+
+ return new User(line,"","");
+
+ }
+
+ public String askDNI() {
+ //variable creation
+ String line = "|";
+ while (line.contains(SEPARE)) {
+ System.out.println("Insert a valid dni: ");
+ line = keyboard.nextLine();
+
+ }
+
+ return line;
+
+ }
+
+ public String askPasword() {
+ //variable creation
+ String line = "|";
+ while (line.contains(SEPARE)) {
+ System.out.println("Insert your pasword: ");
+ line = keyboard.nextLine();
+
+ }
+
+ return line;
+ }
+
+}
diff --git a/src/main/java/com/curso/baptisterio/java/Writer.java b/src/main/java/com/curso/baptisterio/java/Writer.java
new file mode 100644
index 0000000..67a9c4b
--- /dev/null
+++ b/src/main/java/com/curso/baptisterio/java/Writer.java
@@ -0,0 +1,46 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.curso.baptisterio.java;
+
+import static com.curso.baptisterio.java.Constants.*;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Scanner;
+
+/**
+ *
+ * @author dpadilla
+ */
+public class Writer {
+ private FileWriter writer;
+ private String fileName;
+
+ public Writer(String file) {
+ this.fileName = file;
+ }
+
+ public void writeFile(HashMap db) throws IOException {
+
+ File file = new File(fileName);
+ writer = new FileWriter(file);
+ String toWrite;
+
+ for (Map.Entry entry : db.entrySet()) {
+ String key = entry.getKey();
+ User val = entry.getValue();
+
+ toWrite = val.toString();
+ writer.write(toWrite);
+ }
+
+
+ writer.close();
+
+ }
+}