Advertisement
s_m4rt

Untitled

Nov 25th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. package baza;
  2.  
  3. import java.awt.HeadlessException;
  4. import java.awt.List;
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.ObjectOutputStream;
  14. import java.io.Writer;
  15. import java.util.*;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18.  
  19. import javax.swing.JOptionPane;
  20. import baza.Dane;
  21.  
  22. public class main {
  23. static int menu;
  24. static ArrayList<Dane> lista = new ArrayList();
  25. static String[] tablica=new String[6];
  26. static ArrayList tab = new ArrayList();
  27. public static void main(String[] args) throws IOException {
  28. boolean wyjscie=false;
  29. while(wyjscie==false){
  30. menu=Integer.parseInt(JOptionPane.showInputDialog(null,"Co chcesz zrobic? \n1. Dodac rekord\n2.Edytowac rekord\n3.Usunac rekord\n4.Posortowac\n5.Zapisac stan bazy do pliku\n6.Odtworzyc stan bazy z pliku\n7.Wyjscie","Baza",1));
  31. switch(menu){
  32.  
  33. case 1: {//dodac rekord
  34. dodajrekord();
  35. wypisz();
  36. break;}
  37. case 2:{ //edytowac rekord
  38. edytujRekord();
  39. break;}
  40. case 3: {// usunac rekord
  41. usunRekord();
  42. wypisz();
  43. break;}
  44. case 4: {//posortowac
  45. sortowanie();
  46. break;}
  47. case 5:{ //zapis do pliku
  48. zapiszPlik();
  49. break;}
  50. case 6:{ //odczyt z pliku
  51. otworzPlik();
  52. break;}
  53. case 7:
  54. wyjscie=true;
  55. System.exit(0);
  56. break;
  57. default: menu=Integer.parseInt(JOptionPane.showInputDialog("Co chcesz zrobic? \n1. Dodac rekord\n2.Edytowac rekord\n3.Usunac rekord\n4.Posortowac\n5.Zapisac stan bazy do pliku\n6.Odtworzyc stan bazy z pliku\n7.Wyjscie"));
  58.  
  59. }
  60.  
  61. }
  62. }
  63. static void dodajrekord(){
  64. Dane tmp = new Dane();
  65. final Pattern pattern = Pattern.compile("^[A-Za-z, ]++$");
  66. tmp.imie = JOptionPane.showInputDialog("Podaj imie: ");
  67. while(!pattern.matcher(tmp.imie).matches()){
  68. tmp.imie = JOptionPane.showInputDialog("Niepoprawne dane. Podaj imie: ");
  69. }
  70. tmp.nazwisko=JOptionPane.showInputDialog("Podaj nazwisko: ");
  71. while(!pattern.matcher(tmp.nazwisko).matches()){
  72. tmp.nazwisko = JOptionPane.showInputDialog("Niepoprawne dane. Podaj nazwisko: ");
  73. }
  74. try {
  75. tmp.wiek=Integer.parseInt(JOptionPane.showInputDialog("Podaj wiek: "));
  76. } catch (NumberFormatException e) {
  77. tmp.wiek=Integer.parseInt(JOptionPane.showInputDialog("Niepoprawne dane. Podaj wiek: "));
  78. e.printStackTrace();
  79. } catch (HeadlessException e) {
  80. tmp.wiek=Integer.parseInt(JOptionPane.showInputDialog("Niepoprawne dane. Podaj wiek: "));
  81. e.printStackTrace();
  82. }
  83. tmp.miasto=JOptionPane.showInputDialog("Podaj miasto: ");
  84. while(!pattern.matcher(tmp.miasto).matches()){
  85. tmp.miasto = JOptionPane.showInputDialog("Niepoprawne dane. Podaj miasto: ");
  86. }
  87. try {
  88. tmp.stankonta=Double.parseDouble(JOptionPane.showInputDialog("Podaj stan konta: "));
  89. } catch (NumberFormatException e) {
  90. tmp.stankonta=Double.parseDouble(JOptionPane.showInputDialog("Niepoprawne dane. Podaj stan konta: "));
  91. e.printStackTrace();
  92. } catch (HeadlessException e) {
  93. tmp.stankonta=Double.parseDouble(JOptionPane.showInputDialog("Niepoprawne dane. Podaj stan konta: "));
  94. e.printStackTrace();
  95. }
  96. lista.add(tmp);
  97.  
  98. }
  99. static void wypisz(){
  100. for (int i=0; i<lista.size(); i++){
  101. System.out.println(lista.get(i).toString(i));
  102. }
  103. }
  104. static void edytujRekord(){
  105. final Pattern pattern = Pattern.compile("^[A-Za-z, ]++$");
  106. int id=Integer.parseInt(JOptionPane.showInputDialog("Podaj ID rekordu do edycji: "));
  107. int ktory =Integer.parseInt(JOptionPane.showInputDialog("Co chcesz edytowac?\n1. Imie\n2.Nazwisko\n3.Wiek\n4.Miasto\n5.Stan konta"));
  108. switch(ktory){
  109. case 1: {
  110. String name=JOptionPane.showInputDialog("Podaj imie: ");
  111. lista.get(id).imie=name;
  112. break;}
  113. case 2:{
  114. String nazwisko=JOptionPane.showInputDialog("Podaj nazwisko: ");
  115. lista.get(id).nazwisko=nazwisko;
  116. break;}
  117. case 3:{
  118. int wiek=Integer.parseInt(JOptionPane.showInputDialog("Podaj wiek: "));
  119. lista.get(id).wiek=wiek;
  120. break;}
  121. case 4:{
  122. String miasto=JOptionPane.showInputDialog("Podaj miasto: ");
  123. lista.get(id).miasto=miasto;
  124. break;}
  125. case 5: {
  126. Double stankonta=Double.parseDouble(JOptionPane.showInputDialog("Podaj imie: "));
  127. lista.get(id).stankonta=stankonta;
  128. break;}
  129. }
  130. }
  131. static void usunRekord(){
  132. int id=Integer.parseInt(JOptionPane.showInputDialog("Podaj ID rekordu do usuniecia: "));
  133. lista.remove(id);
  134. }
  135. static void zapiszPlik() throws IOException{
  136.  
  137. Writer plik = new FileWriter("glupiabaza.txt", false);
  138. String linia = null;
  139. for (int i=0; i<lista.size(); i++){
  140. linia=(i+" "+lista.get(i).imie+" "+lista.get(i).nazwisko+" "+lista.get(i).wiek+" " +lista.get(i).miasto+" " + lista.get(i).stankonta);
  141. plik.write(linia);
  142. if(i<lista.size()-1) plik.write(System.lineSeparator());
  143. }
  144. plik.close();
  145. }
  146. static void otworzPlik() throws IOException{
  147.  
  148. String of = "glupiabaza.txt";
  149. String str = " ";
  150. String[] splited = str.split("\\s");
  151. Dane tmp = new Dane();
  152. final Pattern pattern = Pattern.compile("\\s");
  153. try (BufferedReader bufReader = new BufferedReader(new FileReader(new File(of)))) {
  154. while ( ( str = bufReader.readLine() ) != null ){
  155. if (!str.contains("null")) System.out.println(str);
  156. for (int x=0; x<splited.length; x++){
  157. //System.out.println("linia: "+splited[x]);
  158. tmp.imie=splited[1];
  159. tmp.nazwisko=splited[2];
  160. tmp.wiek=Integer.parseInt(splited[3]);
  161. tmp.miasto=splited[4];
  162. tmp.stankonta=Double.parseDouble(splited[5]);
  163. }
  164. }
  165. lista.add(tmp);
  166. } catch (IOException e) {
  167.  
  168. e.printStackTrace();
  169. }
  170. }
  171. static void sortowanie(){
  172. ArrayList tmp=new ArrayList();
  173. int ktory=Integer.parseInt(JOptionPane.showInputDialog("Po czym chcesz posortowac?\n1.Po imieniu\n2.Po nazwisku\n3.Po wieku\n4.Po miescie\n5.Po stanie konta"));
  174. int what;
  175. switch(ktory){
  176. case 1:{
  177. what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  178. while (what!=1&&what!=2){
  179. if (what!=1&&what!=2) what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  180. }
  181. for (int i=0; i<lista.size(); i++){
  182.  
  183. String a=lista.get(i).imie;
  184. a+=(lista.get(i).nazwisko+" "+lista.get(i).wiek+" " +lista.get(i).miasto+" " + lista.get(i).stankonta);
  185. tmp.add(a);
  186. }
  187. if(what==1){
  188.  
  189. Collections.sort(tmp);
  190.  
  191. System.out.println(tmp);
  192.  
  193. }
  194. if(what==2) { Collections.sort(tmp, Collections.reverseOrder()); System.out.println(tmp);}
  195. break;
  196. }
  197. case 2: {
  198. what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  199. while (what!=1&&what!=2){
  200. if (what!=1&&what!=2) what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  201. }
  202. for (int i=0; i<lista.size(); i++){
  203.  
  204. String a=lista.get(i).nazwisko;
  205. a+=(lista.get(i).imie+" "+lista.get(i).wiek+" " +lista.get(i).miasto+" " + lista.get(i).stankonta);
  206. tmp.add(a);
  207. }
  208. if(what==1){
  209.  
  210. Collections.sort(tmp);
  211.  
  212. System.out.println(tmp);
  213.  
  214. }
  215. if(what==2) { Collections.sort(tmp, Collections.reverseOrder()); System.out.println(tmp);}
  216. break;
  217. }
  218. case 3: {
  219. what=Integer.parseInt(JOptionPane.showInputDialog("1.Rosnaco\n2.Malejaco"));
  220. while (what!=1&&what!=2){
  221. if (what!=1&&what!=2) what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  222. }
  223. for (int i=0; i<lista.size(); i++){
  224.  
  225. int a=lista.get(i).wiek;
  226. tmp.add(a);
  227. }
  228. Collections.sort(tmp);
  229. if(what==1){
  230.  
  231. System.out.println(tmp);
  232.  
  233. }
  234. if(what==2) { Collections.sort(tmp, Collections.reverseOrder()); System.out.println(tmp);}
  235. break;
  236.  
  237. }
  238. case 4: {
  239. what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  240. while (what!=1&&what!=2){
  241. if (what!=1&&what!=2) what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  242. }
  243. for (int i=0; i<lista.size(); i++){
  244.  
  245. String a=lista.get(i).miasto;
  246. a+=(lista.get(i).imie+" "+lista.get(i).nazwisko+" " +lista.get(i).miasto+" " + lista.get(i).stankonta);
  247. tmp.add(a);
  248. }
  249. if(what==1){
  250.  
  251. Collections.sort(tmp);
  252.  
  253. System.out.println(tmp);
  254.  
  255. }
  256. if(what==2) { Collections.sort(tmp, Collections.reverseOrder()); System.out.println(tmp);}
  257. break;
  258. }
  259. case 5:{
  260. what=Integer.parseInt(JOptionPane.showInputDialog("1.Rosnaco\n2.Malejaco"));
  261. while (what!=1&&what!=2){
  262. if (what!=1&&what!=2) what=Integer.parseInt(JOptionPane.showInputDialog("1.Alfabetycznie A-Z\n2.Alfabetycznie Z-A"));
  263. }
  264. for (int i=0; i<lista.size(); i++){
  265. Double a=lista.get(i).stankonta;
  266. tmp.add(a);
  267. Collections.sort(tmp);
  268.  
  269. }
  270. if(what==1){
  271.  
  272. System.out.println(tmp);
  273.  
  274. }
  275. if(what==2) { Collections.sort(tmp, Collections.reverseOrder()); System.out.println(tmp);}
  276. break;
  277. }
  278. }
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement