package boot.project;
/**
*
* @author Mateus and Bruno
*/
import java.io.*;
import java.util.*;
public class BootProject {
Database D = new Database();
D.Run();
}
}
private final int[] date;
private final int[] time;
public Timestamp(int day,
int month,
int year,
int hours,
int seconds
){ date = new int[3];
date[0] = day;
date[1] = month;
date[2] = year;
time = new int[2];
time[0] = hours;
time[1] = seconds;
}
public int[] getDate(){
return date;
}
public int[] getTime(){
return time;
}
int tdate[] = t.getDate();
for(int i = 0; i < 3; i++)
if(date[i] != tdate[i])
return false;
return true;
}
@Override
return (date[0] + "/" + date[1] + "/" + date[2])
+ " " + (time[0] + ":" + time[1]);
}
}
private HashSet<String> meta_tags;
private HashSet<String> comments;
this.url = url;
this.name = name;
comments = new HashSet<>();
meta_tags = new HashSet<>();
}
public boolean compareName
(String s
){ return name.equals(s);
}
return name;
}
public void setName
(String s
){ name = s;
}
url = s;
}
public boolean compareURL
(String s
){ return url.equals(s);
}
public boolean hasMeta_tag
(String s
){ return meta_tags.contains(s);
}
public void addMeta_tag
(String s
){ if(!hasMeta_tag(s)) {
meta_tags.add(s);
}
}
public void eraseMeta_tag
(String s
){ if(meta_tags.contains(s) && meta_tags.size() == 1){
//nao pode deletar pq tem q ter pelo menos uma
}
else meta_tags.remove(s);
}
public boolean hasComment
(String s
){ return comments.contains(s);
}
public void addComment
(String s
){ if(!hasComment(s)){
comments.add(s);
}
}
public void eraseComment
(String s
){ if(comments.contains(s) && comments.size() == 1){
//nao pode deletar pq tem q ter pelo menos uma
}
else comments.remove(s);
}
public boolean compareLastAcess
(Timestamp t
){ return last_acess.compareDate(t);
}
return creation.compareDate(t);
}
public void print(){
System.
out.
println("Name: " + name
); System.
out.
println("URL: " + url
); System.
out.
println("Creation: " + creation
); System.
out.
println("Last access: " + last_acess
);
System.
out.
println("Coments:"); Iterator<String> it;
it = comments.iterator();
while(it.hasNext()){
System.
out.
println(" " + it.
next()); }
System.
out.
println("Meta tags:"); it = meta_tags.iterator();
while(it.hasNext()){
System.
out.
println(" " + it.
next()); }
}
}
class Database{
private LinkedList<Hyperlink> data;
public Database(){
data = new LinkedList<>();
try{
importFromFile();
System.
out.
println("Data was successfully loaded"); }
System.
out.
println("No data to load"); }
}
while(true){
Hyperlink H;
try{
H = (Hyperlink)filein.readObject();
}
//filein.close();
return;
}
data.add(H);
}
}
private void exportToFile(){
try{
it = data.iterator();
while(it.hasNext()){
fileout.writeObject(it.next());
}
fileout.close();
}
System.
out.
println("salvou nao mansh 1"); }
System.
out.
println("salvou nao mansh 2"); }
}
System.
out.
println("Welcome!"); boolean ended = false;
while(!ended){
int com;
System.
out.
println("Type the number of what you want to do."); System.
out.
println("[0] Add new hyperlink"); System.
out.
println("[1] Edit existing hyperlink"); System.
out.
println("[2] remove existing hyperlink"); System.
out.
println("[3] Search for a hyperlink"); System.
out.
println("[4] Show all hyperlinks stored"); System.
out.
println("[5] Exit"); com
= Integer.
parseInt(input.
readLine()); while(com < 0 || com > 5){
System.
out.
println("Invalid input, try again"); com
= Integer.
parseInt(input.
readLine()); }
if(com == 0){
System.
out.
println("Type the url of the new hyperlink"); url = input.readLine();
System.
out.
println("Type the name of the new hyperlink"); name = input.readLine();
Hyperlink H = new Hyperlink(url, name);
more = "yes";
while(more.equals("yes")){
System.
out.
println("Type a comment for the new hyperlink"); s = input.readLine();
H.addComment(s);
System.
out.
println("Do you want to add more comments? (yes or no)"); more = input.readLine();
while(!more.equals("yes") && !more.equals("no")){
System.
out.
println("Invalid input, try again"); more = input.readLine();
}
}
more = "yes";
while(more.equals("yes")){
System.
out.
println("Type a meta tag for the new hyperlink"); s = input.readLine();
H.addMeta_tag(s);
System.
out.
println("Do you want to add more meta tags? (yes or no)"); more = input.readLine();
while(!more.equals("yes") && !more.equals("no")){
System.
out.
println("Invalid input, try again"); more = input.readLine();
}
}
data.add(H);
}
else if(com == 1 || com == 2 || com == 3){
LinkedList<Hyperlink> candidates;
candidates = new LinkedList<>();
System.
out.
println("How do you want to find the hyperlink?"); int newcom;
System.
out.
println("[0] By name"); System.
out.
println("[1] By URL"); System.
out.
println("[2] By meta tag"); System.
out.
println("[3] By comment"); System.
out.
println("[4] By last access date"); System.
out.
println("[5] By creation date"); newcom
= Integer.
parseInt(input.
readLine()); while(newcom < 0 || newcom > 5){
System.
out.
println("Invalid input, try again"); newcom
= Integer.
parseInt(input.
readLine()); }
if(newcom == 0){
System.
out.
println("Type the name"); s = input.readLine();
candidates = SearchByName(s);
}
else if(newcom == 1){
System.
out.
println("Type the URL"); s = input.readLine();
candidates = SearchByURL(s);
}
else if(newcom == 2){
System.
out.
println("Type the meta tag"); s = input.readLine();
candidates = SearchByMeta_tag(s);
}
else if(newcom == 3){
System.
out.
println("Type the comment"); s = input.readLine();
candidates = SearchByComment(s);
}
System.
out.
println("Hyperlinks found: " + candidates.
size());
int idx = 0;
Iterator<Hyperlink> it;
it = candidates.iterator();
while(it.hasNext()){
System.
out.
println("[" + idx
+ "]" + it.
next().
getName()); idx++;
}
System.
out.
println("Type the number of the selected hyperlink"); int number;
number
= Integer.
parseInt(input.
readLine()); while(number < 0 || number >= candidates.size()){
System.
out.
println("Invalid input, try again"); number
= Integer.
parseInt(input.
readLine()); }
Hyperlink H = candidates.listIterator(number).next();
if(com == 1){
more = "yes";
while(more.equals("yes")){
System.
out.
println("What you want to edit?"); System.
out.
println("[0] Name"); System.
out.
println("[1] URL"); System.
out.
println("[2] Meta tags"); System.
out.
println("[3] Comments"); newcom
= Integer.
parseInt(input.
readLine()); while(newcom < 0 || newcom > 3){
System.
out.
println("Invalid input, try again"); newcom
= Integer.
parseInt(input.
readLine()); }
if(newcom == 0){
System.
out.
println("Type the new name"); H.setName(input.readLine());
}
else if(newcom == 1){
System.
out.
println("Type the new URL"); H.setURL(input.readLine());
}
else if(newcom == 2){
System.
out.
println("What do you want to do with the meta tag list?"); System.
out.
println("[0] Remove meta tag"); System.
out.
println("[1] Add meta tag"); int newcom2;
newcom2
= Integer.
parseInt(input.
readLine()); while(newcom2 < 0 || newcom2 > 1){
System.
out.
println("Invalid input, try again"); newcom2
= Integer.
parseInt(input.
readLine()); }
if(newcom2 == 0){
System.
out.
println("Type the the meta tag you want to remove"); H.eraseMeta_tag(input.readLine());
}
else{
System.
out.
println("Type the meta tag you want to add"); H.addMeta_tag(input.readLine());
}
}
else{
System.
out.
println("What do you want to do with the comment list?"); System.
out.
println("[0] Remove comment"); System.
out.
println("[1] Add comment"); int newcom2;
newcom2
= Integer.
parseInt(input.
readLine()); while(newcom2 < 0 || newcom2 > 1){
System.
out.
println("Invalid input, try again"); newcom2
= Integer.
parseInt(input.
readLine()); }
if(newcom2 == 0){
System.
out.
println("Type the comment you want to remove"); H.eraseMeta_tag(input.readLine());
}
else{
System.
out.
println("Type the comment you want to add"); H.addMeta_tag(input.readLine());
}
}
System.
out.
println("Do you want to change anything else in this hyperlink? (yes or no)"); more = input.readLine();
while(!more.equals("yes") && !more.equals("no")){
System.
out.
println("Invalid input, try again"); more = input.readLine();
}
}
}
else if(com == 2){
data.removeFirstOccurrence(H);
System.
out.
println("The hyperlink has been erased"); }
else if(com == 3){
System.
out.
println("Showing information for the selected hyperlink:"); H.print();
}
}
else if(com == 4){
printAll();
}
else if(com == 5){
ended = true;
}
}
exportToFile();
}
private LinkedList
<Hyperlink
> SearchByURL
(String s
){ LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
Hyperlink H = it.next();
if(H.compareURL(s))
list.add(H);
}
return list;
}
private LinkedList
<Hyperlink
> SearchByName
(String s
){ LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
Hyperlink H = it.next();
if(H.compareName(s))
list.add(H);
}
return list;
}
private LinkedList
<Hyperlink
> SearchByMeta_tag
(String s
){ LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
Hyperlink H = it.next();
if(H.hasMeta_tag(s))
list.add(H);
}
return list;
}
private LinkedList
<Hyperlink
> SearchByComment
(String s
){ LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
Hyperlink H = it.next();
if(H.hasComment(s))
list.add(H);
}
return list;
}
private LinkedList
<Hyperlink
> SearchByLastAcess
(Timestamp t
){ LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
Hyperlink H = it.next();
if(H.compareLastAcess(t))
list.add(H);
}
return list;
}
private LinkedList
<Hyperlink
> SearchByCreation
(Timestamp t
){ LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
Hyperlink H = it.next();
if(H.compareCreation(t))
list.add(H);
}
return list;
}
private void printAll(){
LinkedList<Hyperlink> list = new LinkedList<>();
Iterator<Hyperlink> it;
it = data.iterator();
while(it.hasNext()){
it.next().print();
}
}
}