Posted By

eiger824 on 01/21/13


Tagged

basic database java in


Versions (?)

Basic Database in Java. Updated Version


 / Published in: Java
 

URL: http://programmingeiger824.blogspot.com

A basic database made in Java. It resembles a mobile phone contacts activity in which user is prompted to introduce some data of new contacts.

  1. package com.santi;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7.  
  8. public class Main {
  9.  
  10.  
  11. public static void main(String[] args) {
  12. //declaration of arraylist-type variables
  13. ArrayList<String> names=new ArrayList<String>();
  14. ArrayList<String> last_names=new ArrayList<String>();
  15. ArrayList<String> phones=new ArrayList<String>();
  16. ArrayList<String> emails=new ArrayList<String>();
  17. //declaration of a Date-type variable
  18. Date date1 = new Date();
  19. //rest of auxiliary variables
  20. int cont=0;
  21. boolean a =true;
  22. //end of variable declarations
  23. System.out.println("Program started on " + date1);
  24. //main while loop
  25. while (a==true){
  26. if(names.size()==0 && last_names.size()==0 && phones.size()==0 && emails.size()==0) System.out.flush();
  27. System.out.println("Please select an option: ");
  28. String option2 = getInput("1. Add contact." +
  29. "2. Show contacts." +
  30. "3. Remove contact. 0. Exit program. : ");
  31. int opt=Integer.parseInt(option2);
  32. switch (opt) {
  33. case 1:
  34. names.add(getInput("Name: "));
  35. last_names.add(getInput("Last name: "));
  36. phones.add(getInput("Phone number: "));
  37. emails.add(getInput("Email: "));
  38.  
  39. break;
  40. case 2:
  41. if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
  42. System.out.println("There are no contacts yet!");
  43. }
  44. else {
  45. for (int i = 0; i < names.size(); i++) {
  46. System.out.println(i+1 + "- ");
  47. String aux_name = names.get(i);
  48. System.out.println(aux_name);
  49. String aux_last_name = last_names.get(i);
  50. System.out.println(aux_last_name);
  51. String aux_phone = phones.get(i);
  52. System.out.println(aux_phone);
  53. String aux_email = emails.get(i);
  54. System.out.println(aux_email);
  55. }
  56.  
  57. }
  58. break;
  59.  
  60. case 3:
  61. if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
  62. System.out.println("There are no contacts yet!");
  63. }
  64. else if(names.size()==1){
  65. names.clear();
  66. last_names.clear();
  67. phones.clear();
  68. emails.clear();
  69. System.out.println("All contacts succesfully erased! No contacts in database anymore.");
  70. break;
  71. }
  72. else {
  73. cont=0;
  74. String option=getInput("1.REMOVE BY NAME. 2.REMOVE ALL. : ");
  75. int option3 = Integer.parseInt(option);
  76. switch (option3){
  77. case 1:
  78. String chc = getInput("Please enter the name of the contact you wish to erase: ");
  79. for (String name : names) {
  80. if (chc.equals(name)) {
  81. //remove all fields of data in that position
  82. names.remove(chc);
  83. last_names.remove(chc);
  84. phones.remove(chc);
  85. emails.remove(chc);
  86. System.out.println("Contact succesfully erased!");
  87. break;
  88. }cont++;
  89. }
  90. if (cont==names.size()) {
  91. System.out.println("WARNING: No matches between provided name and stored names. Remember: this program is case-sensitive!");
  92. }
  93.  
  94.  
  95. break;
  96. case 2:
  97. names.clear();
  98. last_names.clear();
  99. phones.clear();
  100. emails.clear();
  101. System.out.println("All contacts succesfully erased! No contacts in database anymore.");
  102. break;
  103. }
  104. break;
  105. }
  106. break;
  107. case 0:
  108. Date date2 = new Date();
  109. System.out.println("Program ended on " + date2);
  110. System.out.println("Developed by Santi Pagola.");
  111. a=false;
  112. break;
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. private static String getInput(String prompt) {
  120.  
  121. System.out.print(prompt);
  122. System.out.flush();
  123.  
  124. try {
  125. return stdin.readLine();
  126. } catch (Exception e) {
  127. return "Error: " + e.getMessage();
  128. }
  129.  
  130. }
  131.  
  132. }

Report this snippet  

You need to login to post a comment.