Guest User

Untitled

a guest
Dec 14th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.StringTokenizer;
  8.  
  9. public class Adresse {
  10.  
  11. private String rue, ville, codePostal;
  12.  
  13. public Adresse(String rue, String ville, String codePostal) throws CodePostalException {
  14. super();
  15.  
  16. if (codePostal.length() != 5) {
  17. throw new CodePostalException(codePostal);
  18. }
  19.  
  20. for (int i = 0; i < codePostal.length(); i++) {
  21. char x = codePostal.charAt(i);
  22. if (x < '0' || x > '9') { // verification par le code ascii entre 0 et 9 (une lettre)
  23. throw new CodePostalException(codePostal);
  24. }
  25. }
  26.  
  27. this.rue = rue;
  28. this.ville = ville;
  29. this.codePostal = codePostal;
  30.  
  31. }
  32.  
  33. public Adresse() {
  34. super();
  35. }
  36.  
  37. public String getRue() {
  38. return rue;
  39. }
  40.  
  41. public void setRue(String rue) {
  42. this.rue = rue;
  43. }
  44.  
  45. public String getVille() {
  46. return ville;
  47. }
  48.  
  49. public void setVille(String ville) {
  50. this.ville = ville;
  51. }
  52.  
  53. public String getCodePostal() {
  54. return codePostal;
  55. }
  56.  
  57. public void setCodePostal(String codePostal) {
  58. this.codePostal = codePostal;
  59. }
  60.  
  61. public void EnregistrerAdresse() {
  62.  
  63. try {
  64. BufferedWriter bw = null;
  65. bw = new BufferedWriter(new FileWriter(("fichier.txt"), true));
  66.  
  67. bw.write(this.rue + " " + this.ville + " " + this.codePostal + "\n");
  68.  
  69. bw.close();
  70.  
  71. } catch (FileNotFoundException e) {
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76.  
  77. }
  78.  
  79. public static boolean ChercheCodePostal(String cp) { // static car c'est une methode de classe qui n'utilise aucun
  80. // objet
  81.  
  82. try {
  83. BufferedReader br1 = null;
  84. String lignebr1;
  85. StringTokenizer s;
  86. br1 = new BufferedReader(new FileReader("fichier.txt"));
  87.  
  88. while ((lignebr1 = br1.readLine()) != null) {
  89. s = new StringTokenizer(lignebr1);
  90.  
  91. String rue = s.nextToken(); // 1er mot
  92. String ville = s.nextToken(); // 2e mot
  93.  
  94. if (cp.equals(s.nextToken())) { // vérifie si cp == au 3e mot de la ligne (3e s.nextToken())
  95. System.out.println("Le code postal " + cp + " est dans le fichier");
  96. br1.close();
  97. return true;
  98. //return new Adresse(rue, ville, cp); // création de l'objet retourné si OK
  99. }
  100. }
  101.  
  102. br1.close();
  103.  
  104. } catch (FileNotFoundException e) {
  105. e.printStackTrace();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. return false;
  110.  
  111.  
  112. }
  113.  
  114. }
Add Comment
Please, Sign In to add comment