fork download
  1. package boot.project;
  2. /**
  3.  *
  4.  * @author Mateus and Bruno
  5.  */
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. public class BootProject {
  11. public static void main(String[] args) throws IOException{
  12. Database D = new Database();
  13. D.Run();
  14. }
  15. }
  16.  
  17. class Timestamp{
  18. private final int[] date;
  19. private final int[] time;
  20.  
  21. public Timestamp(int day, int month, int year, int hours, int seconds){
  22. date = new int[3];
  23. date[0] = day;
  24. date[1] = month;
  25. date[2] = year;
  26.  
  27. time = new int[2];
  28. time[0] = hours;
  29. time[1] = seconds;
  30. }
  31. public int[] getDate(){
  32. return date;
  33. }
  34. public int[] getTime(){
  35. return time;
  36. }
  37. public boolean compareDate(Timestamp t){
  38. int tdate[] = t.getDate();
  39. for(int i = 0; i < 3; i++)
  40. if(date[i] != tdate[i])
  41. return false;
  42. return true;
  43. }
  44. @Override
  45. public String toString(){
  46. return (date[0] + "/" + date[1] + "/" + date[2])
  47. + " " + (time[0] + ":" + time[1]);
  48. }
  49. }
  50.  
  51. class Hyperlink implements java.io.Serializable{
  52. private String url;
  53. private String name;
  54. private HashSet<String> meta_tags;
  55. private HashSet<String> comments;
  56. private Timestamp last_acess;
  57. private Timestamp creation;
  58.  
  59. public Hyperlink(String url, String name){
  60. this.url = url;
  61. this.name = name;
  62. comments = new HashSet<>();
  63. meta_tags = new HashSet<>();
  64. }
  65. public boolean compareName(String s){
  66. return name.equals(s);
  67. }
  68. public String getName(){
  69. return name;
  70. }
  71. public void setName(String s){
  72. name = s;
  73. }
  74. public void setURL(String s){
  75. url = s;
  76. }
  77. public boolean compareURL(String s){
  78. return url.equals(s);
  79. }
  80. public boolean hasMeta_tag(String s){
  81. return meta_tags.contains(s);
  82. }
  83. public void addMeta_tag(String s){
  84. if(!hasMeta_tag(s)) {
  85. meta_tags.add(s);
  86. }
  87. }
  88. public void eraseMeta_tag(String s){
  89. if(meta_tags.contains(s) && meta_tags.size() == 1){
  90. //nao pode deletar pq tem q ter pelo menos uma
  91. }
  92. else meta_tags.remove(s);
  93. }
  94. public boolean hasComment(String s){
  95. return comments.contains(s);
  96. }
  97. public void addComment(String s){
  98. if(!hasComment(s)){
  99. comments.add(s);
  100. }
  101. }
  102. public void eraseComment(String s){
  103. if(comments.contains(s) && comments.size() == 1){
  104. //nao pode deletar pq tem q ter pelo menos uma
  105. }
  106. else comments.remove(s);
  107. }
  108. public boolean compareLastAcess(Timestamp t){
  109. return last_acess.compareDate(t);
  110. }
  111. public boolean compareCreation(Timestamp t){
  112. return creation.compareDate(t);
  113. }
  114. public void print(){
  115. System.out.println("Name: " + name);
  116. System.out.println("URL: " + url);
  117. System.out.println("Creation: " + creation);
  118. System.out.println("Last access: " + last_acess);
  119.  
  120. System.out.println("Coments:");
  121. Iterator<String> it;
  122. it = comments.iterator();
  123. while(it.hasNext()){
  124. System.out.println(" " + it.next());
  125. }
  126.  
  127. System.out.println("Meta tags:");
  128. it = meta_tags.iterator();
  129. while(it.hasNext()){
  130. System.out.println(" " + it.next());
  131. }
  132. }
  133. }
  134.  
  135. class Database{
  136. private LinkedList<Hyperlink> data;
  137. public Database(){
  138. data = new LinkedList<>();
  139. try{
  140. filein = new ObjectInputStream(new FileInputStream("hyperlinks.tmp"));
  141. importFromFile();
  142. System.out.println("Data was successfully loaded");
  143. }
  144. catch (IOException c){
  145. System.out.println("No data to load");
  146. }
  147. }
  148. private void importFromFile() throws IOException{
  149. while(true){
  150. Hyperlink H;
  151. try{
  152. H = (Hyperlink)filein.readObject();
  153. }
  154. //filein.close();
  155. return;
  156. }
  157. data.add(H);
  158. }
  159. }
  160. private void exportToFile(){
  161. try{
  162. fileout = new ObjectOutputStream(new FileOutputStream("hyperlinks.tmp"));
  163. it = data.iterator();
  164. while(it.hasNext()){
  165. fileout.writeObject(it.next());
  166. }
  167. fileout.close();
  168. }
  169. System.out.println("salvou nao mansh 1");
  170. }
  171. catch(IOException c){
  172. System.out.println("salvou nao mansh 2");
  173. }
  174. }
  175. public void Run() throws IOException{
  176. System.out.println("Welcome!");
  177. boolean ended = false;
  178. while(!ended){
  179. int com;
  180. System.out.println("Type the number of what you want to do.");
  181. System.out.println("[0] Add new hyperlink");
  182. System.out.println("[1] Edit existing hyperlink");
  183. System.out.println("[2] remove existing hyperlink");
  184. System.out.println("[3] Search for a hyperlink");
  185. System.out.println("[4] Show all hyperlinks stored");
  186. System.out.println("[5] Exit");
  187. com = Integer.parseInt(input.readLine());
  188. while(com < 0 || com > 5){
  189. System.out.println("Invalid input, try again");
  190. com = Integer.parseInt(input.readLine());
  191. }
  192. if(com == 0){
  193. String name;
  194. String url;
  195. System.out.println("Type the url of the new hyperlink");
  196. url = input.readLine();
  197. System.out.println("Type the name of the new hyperlink");
  198. name = input.readLine();
  199. Hyperlink H = new Hyperlink(url, name);
  200.  
  201. String more;
  202. String s;
  203. more = "yes";
  204. while(more.equals("yes")){
  205. System.out.println("Type a comment for the new hyperlink");
  206. s = input.readLine();
  207. H.addComment(s);
  208. System.out.println("Do you want to add more comments? (yes or no)");
  209. more = input.readLine();
  210. while(!more.equals("yes") && !more.equals("no")){
  211. System.out.println("Invalid input, try again");
  212. more = input.readLine();
  213. }
  214. }
  215. more = "yes";
  216. while(more.equals("yes")){
  217. System.out.println("Type a meta tag for the new hyperlink");
  218. s = input.readLine();
  219. H.addMeta_tag(s);
  220. System.out.println("Do you want to add more meta tags? (yes or no)");
  221. more = input.readLine();
  222. while(!more.equals("yes") && !more.equals("no")){
  223. System.out.println("Invalid input, try again");
  224. more = input.readLine();
  225. }
  226. }
  227. data.add(H);
  228. }
  229. else if(com == 1 || com == 2 || com == 3){
  230. String s;
  231. LinkedList<Hyperlink> candidates;
  232. candidates = new LinkedList<>();
  233. System.out.println("How do you want to find the hyperlink?");
  234. int newcom;
  235. System.out.println("[0] By name");
  236. System.out.println("[1] By URL");
  237. System.out.println("[2] By meta tag");
  238. System.out.println("[3] By comment");
  239. System.out.println("[4] By last access date");
  240. System.out.println("[5] By creation date");
  241. newcom = Integer.parseInt(input.readLine());
  242. while(newcom < 0 || newcom > 5){
  243. System.out.println("Invalid input, try again");
  244. newcom = Integer.parseInt(input.readLine());
  245. }
  246.  
  247. if(newcom == 0){
  248. System.out.println("Type the name");
  249. s = input.readLine();
  250. candidates = SearchByName(s);
  251. }
  252. else if(newcom == 1){
  253. System.out.println("Type the URL");
  254. s = input.readLine();
  255. candidates = SearchByURL(s);
  256. }
  257. else if(newcom == 2){
  258. System.out.println("Type the meta tag");
  259. s = input.readLine();
  260. candidates = SearchByMeta_tag(s);
  261. }
  262. else if(newcom == 3){
  263. System.out.println("Type the comment");
  264. s = input.readLine();
  265. candidates = SearchByComment(s);
  266. }
  267. System.out.println("Hyperlinks found: " + candidates.size());
  268.  
  269. int idx = 0;
  270. Iterator<Hyperlink> it;
  271. it = candidates.iterator();
  272. while(it.hasNext()){
  273. System.out.println("[" + idx + "]" + it.next().getName());
  274. idx++;
  275. }
  276. System.out.println("Type the number of the selected hyperlink");
  277. int number;
  278. number = Integer.parseInt(input.readLine());
  279. while(number < 0 || number >= candidates.size()){
  280. System.out.println("Invalid input, try again");
  281. number = Integer.parseInt(input.readLine());
  282. }
  283. Hyperlink H = candidates.listIterator(number).next();
  284. if(com == 1){
  285. String more;
  286. more = "yes";
  287. while(more.equals("yes")){
  288. System.out.println("What you want to edit?");
  289. System.out.println("[0] Name");
  290. System.out.println("[1] URL");
  291. System.out.println("[2] Meta tags");
  292. System.out.println("[3] Comments");
  293. newcom = Integer.parseInt(input.readLine());
  294. while(newcom < 0 || newcom > 3){
  295. System.out.println("Invalid input, try again");
  296. newcom = Integer.parseInt(input.readLine());
  297. }
  298. if(newcom == 0){
  299. System.out.println("Type the new name");
  300. H.setName(input.readLine());
  301. }
  302. else if(newcom == 1){
  303. System.out.println("Type the new URL");
  304. H.setURL(input.readLine());
  305. }
  306. else if(newcom == 2){
  307. System.out.println("What do you want to do with the meta tag list?");
  308. System.out.println("[0] Remove meta tag");
  309. System.out.println("[1] Add meta tag");
  310. int newcom2;
  311. newcom2 = Integer.parseInt(input.readLine());
  312. while(newcom2 < 0 || newcom2 > 1){
  313. System.out.println("Invalid input, try again");
  314. newcom2 = Integer.parseInt(input.readLine());
  315. }
  316. if(newcom2 == 0){
  317. System.out.println("Type the the meta tag you want to remove");
  318. H.eraseMeta_tag(input.readLine());
  319. }
  320. else{
  321. System.out.println("Type the meta tag you want to add");
  322. H.addMeta_tag(input.readLine());
  323. }
  324. }
  325. else{
  326. System.out.println("What do you want to do with the comment list?");
  327. System.out.println("[0] Remove comment");
  328. System.out.println("[1] Add comment");
  329. int newcom2;
  330. newcom2 = Integer.parseInt(input.readLine());
  331. while(newcom2 < 0 || newcom2 > 1){
  332. System.out.println("Invalid input, try again");
  333. newcom2 = Integer.parseInt(input.readLine());
  334. }
  335. if(newcom2 == 0){
  336. System.out.println("Type the comment you want to remove");
  337. H.eraseMeta_tag(input.readLine());
  338. }
  339. else{
  340. System.out.println("Type the comment you want to add");
  341. H.addMeta_tag(input.readLine());
  342. }
  343. }
  344. System.out.println("Do you want to change anything else in this hyperlink? (yes or no)");
  345. more = input.readLine();
  346. while(!more.equals("yes") && !more.equals("no")){
  347. System.out.println("Invalid input, try again");
  348. more = input.readLine();
  349. }
  350. }
  351. }
  352. else if(com == 2){
  353. data.removeFirstOccurrence(H);
  354. System.out.println("The hyperlink has been erased");
  355. }
  356. else if(com == 3){
  357. System.out.println("Showing information for the selected hyperlink:");
  358. H.print();
  359. }
  360. }
  361. else if(com == 4){
  362. printAll();
  363. }
  364. else if(com == 5){
  365. ended = true;
  366. }
  367. }
  368. exportToFile();
  369. }
  370. private LinkedList<Hyperlink> SearchByURL(String s){
  371. LinkedList<Hyperlink> list = new LinkedList<>();
  372. Iterator<Hyperlink> it;
  373. it = data.iterator();
  374. while(it.hasNext()){
  375. Hyperlink H = it.next();
  376. if(H.compareURL(s))
  377. list.add(H);
  378. }
  379. return list;
  380. }
  381. private LinkedList<Hyperlink> SearchByName(String s){
  382. LinkedList<Hyperlink> list = new LinkedList<>();
  383. Iterator<Hyperlink> it;
  384. it = data.iterator();
  385. while(it.hasNext()){
  386. Hyperlink H = it.next();
  387. if(H.compareName(s))
  388. list.add(H);
  389. }
  390. return list;
  391. }
  392. private LinkedList<Hyperlink> SearchByMeta_tag(String s){
  393. LinkedList<Hyperlink> list = new LinkedList<>();
  394. Iterator<Hyperlink> it;
  395. it = data.iterator();
  396. while(it.hasNext()){
  397. Hyperlink H = it.next();
  398. if(H.hasMeta_tag(s))
  399. list.add(H);
  400. }
  401. return list;
  402. }
  403. private LinkedList<Hyperlink> SearchByComment(String s){
  404. LinkedList<Hyperlink> list = new LinkedList<>();
  405. Iterator<Hyperlink> it;
  406. it = data.iterator();
  407. while(it.hasNext()){
  408. Hyperlink H = it.next();
  409. if(H.hasComment(s))
  410. list.add(H);
  411. }
  412. return list;
  413. }
  414. private LinkedList<Hyperlink> SearchByLastAcess(Timestamp t){
  415. LinkedList<Hyperlink> list = new LinkedList<>();
  416. Iterator<Hyperlink> it;
  417. it = data.iterator();
  418. while(it.hasNext()){
  419. Hyperlink H = it.next();
  420. if(H.compareLastAcess(t))
  421. list.add(H);
  422. }
  423. return list;
  424. }
  425. private LinkedList<Hyperlink> SearchByCreation(Timestamp t){
  426. LinkedList<Hyperlink> list = new LinkedList<>();
  427. Iterator<Hyperlink> it;
  428. it = data.iterator();
  429. while(it.hasNext()){
  430. Hyperlink H = it.next();
  431. if(H.compareCreation(t))
  432. list.add(H);
  433. }
  434. return list;
  435. }
  436. private void printAll(){
  437. LinkedList<Hyperlink> list = new LinkedList<>();
  438. Iterator<Hyperlink> it;
  439. it = data.iterator();
  440. while(it.hasNext()){
  441. System.out.println("");
  442. it.next().print();
  443. System.out.println("");
  444. }
  445. }
  446. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:10: error: class BootProject is public, should be declared in a file named BootProject.java
public class BootProject {
       ^
1 error
stdout
Standard output is empty