Advertisement
lvm9

Untitled

Mar 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. package first;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class datosProductos {
  15.  
  16. File archivoOriginal = new File("data.csv");
  17. File archivoTemporal = new File("dataTemporal.csv");
  18. private String key;
  19. private String ProductID;
  20. private String ProductCategory;
  21. private String ProductType;
  22. private String BrandName;
  23. private String ProductName;
  24. private double PricePerUnit;
  25. private int TotalQuantity;
  26.  
  27. public datosProductos(String ProductID, String ProductCategory, String ProductType, String BrandName, String ProductName, double PricePerUnit, int TotalQuantity) {
  28. this.ProductID = ProductID;
  29. this.ProductCategory = ProductCategory;
  30. this.ProductType = ProductType;
  31. this.BrandName = BrandName;
  32. this.ProductName = ProductName;
  33. this.PricePerUnit = PricePerUnit;
  34. this.TotalQuantity = TotalQuantity;
  35. }
  36.  
  37. public String getKey() {
  38. return key;
  39. }
  40.  
  41. public void setKey(String key) {
  42. this.key = key;
  43. }
  44.  
  45. public datosProductos() {
  46.  
  47. }
  48.  
  49. @Override
  50. public String toString() {
  51. return "data{" + "ProductID=" + ProductID + ", ProductCategory=" + ProductCategory + ", ProductType=" + ProductType + ", BrandName=" + BrandName + ", ProductName=" + ProductName + ", PricePerUnit=" + PricePerUnit + ", TotalQuantity=" + TotalQuantity + '}';
  52. }
  53.  
  54. public String getProductID() {
  55. return ProductID;
  56. }
  57.  
  58. public void setProductID(String ProductID) {
  59. this.ProductID = ProductID;
  60. }
  61.  
  62. public String getProductCategory() {
  63. return ProductCategory;
  64. }
  65.  
  66. public void setProductCategory(String ProductCategory) {
  67. this.ProductCategory = ProductCategory;
  68. }
  69.  
  70. public String getProductType() {
  71. return ProductType;
  72. }
  73.  
  74. public void setProductType(String ProductType) {
  75. this.ProductType = ProductType;
  76. }
  77.  
  78. public String getBrandName() {
  79. return BrandName;
  80. }
  81.  
  82. public void setBrandName(String BrandName) {
  83. this.BrandName = BrandName;
  84. }
  85.  
  86. public String getProductName() {
  87. return ProductName;
  88. }
  89.  
  90. public void setProductName(String ProductName) {
  91. this.ProductName = ProductName;
  92. }
  93.  
  94. public double getPricePerUnit() {
  95. return PricePerUnit;
  96. }
  97.  
  98. public void setPricePerUnit(double PricePerUnit) {
  99. this.PricePerUnit = PricePerUnit;
  100. }
  101.  
  102. public int getTotalQuantity() {
  103. return TotalQuantity;
  104. }
  105.  
  106. public void setTotalQuantity(int TotalQuantity) {
  107. this.TotalQuantity = TotalQuantity;
  108. }
  109.  
  110. public void agregar() {
  111.  
  112.  
  113.  
  114.  
  115. try {
  116. // Abrir archivo
  117. FileWriter fw = new FileWriter(archivoOriginal, true);
  118. BufferedWriter bw = new BufferedWriter(fw);
  119. String cad = ProductID + "," + ProductCategory + "," + ProductType + "," + BrandName + "," + ProductName + "," + PricePerUnit + "," + TotalQuantity;
  120.  
  121.  
  122. // getKey();
  123.  
  124. if(!buscar()){
  125. bw.write(cad);
  126. bw.newLine();
  127. bw.close();
  128. fw.close();
  129. JOptionPane.showMessageDialog(null, "Registered!!.");
  130. }
  131. else{
  132. JOptionPane.showMessageDialog(null, "ID Already Registered!!.");
  133. bw.close();
  134. fw.close();
  135. }
  136. } catch (IOException ex) {
  137. System.out.println("Error: " + ex.getMessage());
  138. }
  139.  
  140.  
  141. }
  142.  
  143. public boolean buscar() {
  144.  
  145. boolean res = false;
  146.  
  147. try {
  148. //Abrir Archivo
  149. FileReader fr = new FileReader(archivoOriginal);
  150. BufferedReader br = new BufferedReader(fr);
  151.  
  152. // Leer datos
  153. String leido;
  154. String[] cadenas;
  155. leido = br.readLine();
  156. while (leido != null) {
  157.  
  158. cadenas = leido.split(",");
  159. if (cadenas[0].equals(ProductID)) {
  160. ProductID = cadenas[0];
  161. ProductCategory = cadenas[1];
  162. ProductType = cadenas[2];
  163. BrandName = cadenas[3];
  164. ProductName = cadenas[4];
  165. PricePerUnit = Double.parseDouble(cadenas[5]);
  166. TotalQuantity = Integer.parseInt(cadenas[6]);
  167.  
  168. res = true;
  169. break;
  170. }
  171. leido = br.readLine();
  172.  
  173. }
  174. //Cerrar Archivo
  175.  
  176. br.close();
  177. fr.close();
  178.  
  179. } catch (FileNotFoundException ex) {
  180. System.out.println("Error: " + ex.getMessage());
  181. } catch (IOException ex) {
  182. System.out.println("Error: " + ex.getMessage());
  183. }
  184.  
  185. return res;
  186.  
  187. }
  188.  
  189. public void agregarTemporal() {
  190.  
  191. try {
  192. // Abrir archivo
  193. FileWriter fw = new FileWriter(archivoTemporal, true);
  194. BufferedWriter bw = new BufferedWriter(fw);
  195. String cad = ProductID + "," + ProductCategory + "," + ProductType + "," + BrandName + "," + ProductName + "," + PricePerUnit + "," + TotalQuantity;
  196.  
  197.  
  198. bw.write(cad);
  199. bw.newLine();
  200. bw.close();
  201. fw.close();
  202.  
  203. } catch (IOException ex) {
  204. System.out.println("Error: " + ex.getMessage());
  205. }
  206.  
  207. }
  208.  
  209. public void buscarTemporal() {
  210.  
  211. try {
  212. //Abrir Archivo
  213. FileWriter fw = new FileWriter(archivoTemporal, true);
  214. BufferedWriter bw = new BufferedWriter(fw);
  215.  
  216. String cad = ProductID + "," + ProductCategory + "," + ProductType + "," + BrandName + "," + ProductName + "," + PricePerUnit + "," + TotalQuantity;
  217. bw.write(cad);
  218. bw.newLine();
  219.  
  220. bw.close();
  221. fw.close();
  222.  
  223. } catch (IOException ex) {
  224. Logger.getLogger(datosProductos.class.getName()).log(Level.SEVERE, null, ex);
  225. }
  226.  
  227. }
  228.  
  229. public void editar() {
  230.  
  231. boolean res = false;
  232.  
  233. try {
  234. //Abrir Archivo
  235. FileReader fr = new FileReader(archivoOriginal);
  236. BufferedReader br = new BufferedReader(fr);
  237.  
  238. // Leer datos
  239. String leido;
  240. String[] cadenas;
  241. leido = br.readLine();
  242.  
  243. while (leido != null) {
  244. cadenas = leido.split(",");
  245.  
  246. ProductID = cadenas[0];
  247. ProductCategory = cadenas[1];
  248. ProductType = cadenas[2];
  249. BrandName = cadenas[3];
  250. ProductName = cadenas[4];
  251. PricePerUnit = Double.parseDouble(cadenas[5]);
  252. TotalQuantity = Integer.parseInt(cadenas[6]);
  253.  
  254. if (!ProductID.equals(getKey())) {
  255. buscarTemporal();
  256. }
  257.  
  258.  
  259. leido = br.readLine();
  260.  
  261. }
  262.  
  263. //Cerrar Archivo
  264. br.close();
  265. fr.close();
  266. } catch (FileNotFoundException ex) {
  267. System.out.println("Error " + ex.getMessage());
  268. } catch (IOException ex) {
  269. System.out.println("Error " + ex.getMessage());
  270. }
  271.  
  272. archivoOriginal.delete();
  273. archivoTemporal.renameTo(archivoOriginal);
  274.  
  275. }
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement