Advertisement
s_m4rt

Untitled

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