Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package first;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- public class datosProductos {
- File archivoOriginal = new File("data.csv");
- File archivoTemporal = new File("dataTemporal.csv");
- private String key;
- private String ProductID;
- private String ProductCategory;
- private String ProductType;
- private String BrandName;
- private String ProductName;
- private double PricePerUnit;
- private int TotalQuantity;
- public datosProductos(String ProductID, String ProductCategory, String ProductType, String BrandName, String ProductName, double PricePerUnit, int TotalQuantity) {
- this.ProductID = ProductID;
- this.ProductCategory = ProductCategory;
- this.ProductType = ProductType;
- this.BrandName = BrandName;
- this.ProductName = ProductName;
- this.PricePerUnit = PricePerUnit;
- this.TotalQuantity = TotalQuantity;
- }
- public String getKey() {
- return key;
- }
- public void setKey(String key) {
- this.key = key;
- }
- public datosProductos() {
- }
- @Override
- public String toString() {
- return "data{" + "ProductID=" + ProductID + ", ProductCategory=" + ProductCategory + ", ProductType=" + ProductType + ", BrandName=" + BrandName + ", ProductName=" + ProductName + ", PricePerUnit=" + PricePerUnit + ", TotalQuantity=" + TotalQuantity + '}';
- }
- public String getProductID() {
- return ProductID;
- }
- public void setProductID(String ProductID) {
- this.ProductID = ProductID;
- }
- public String getProductCategory() {
- return ProductCategory;
- }
- public void setProductCategory(String ProductCategory) {
- this.ProductCategory = ProductCategory;
- }
- public String getProductType() {
- return ProductType;
- }
- public void setProductType(String ProductType) {
- this.ProductType = ProductType;
- }
- public String getBrandName() {
- return BrandName;
- }
- public void setBrandName(String BrandName) {
- this.BrandName = BrandName;
- }
- public String getProductName() {
- return ProductName;
- }
- public void setProductName(String ProductName) {
- this.ProductName = ProductName;
- }
- public double getPricePerUnit() {
- return PricePerUnit;
- }
- public void setPricePerUnit(double PricePerUnit) {
- this.PricePerUnit = PricePerUnit;
- }
- public int getTotalQuantity() {
- return TotalQuantity;
- }
- public void setTotalQuantity(int TotalQuantity) {
- this.TotalQuantity = TotalQuantity;
- }
- public void agregar() {
- try {
- // Abrir archivo
- FileWriter fw = new FileWriter(archivoOriginal, true);
- BufferedWriter bw = new BufferedWriter(fw);
- String cad = ProductID + "," + ProductCategory + "," + ProductType + "," + BrandName + "," + ProductName + "," + PricePerUnit + "," + TotalQuantity;
- // getKey();
- if(!buscar()){
- bw.write(cad);
- bw.newLine();
- bw.close();
- fw.close();
- JOptionPane.showMessageDialog(null, "Registered!!.");
- }
- else{
- JOptionPane.showMessageDialog(null, "ID Already Registered!!.");
- bw.close();
- fw.close();
- }
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- }
- }
- public boolean buscar() {
- boolean res = false;
- try {
- //Abrir Archivo
- FileReader fr = new FileReader(archivoOriginal);
- BufferedReader br = new BufferedReader(fr);
- // Leer datos
- String leido;
- String[] cadenas;
- leido = br.readLine();
- while (leido != null) {
- cadenas = leido.split(",");
- if (cadenas[0].equals(ProductID)) {
- ProductID = cadenas[0];
- ProductCategory = cadenas[1];
- ProductType = cadenas[2];
- BrandName = cadenas[3];
- ProductName = cadenas[4];
- PricePerUnit = Double.parseDouble(cadenas[5]);
- TotalQuantity = Integer.parseInt(cadenas[6]);
- res = true;
- break;
- }
- leido = br.readLine();
- }
- //Cerrar Archivo
- br.close();
- fr.close();
- } catch (FileNotFoundException ex) {
- System.out.println("Error: " + ex.getMessage());
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- }
- return res;
- }
- public void agregarTemporal() {
- try {
- // Abrir archivo
- FileWriter fw = new FileWriter(archivoTemporal, true);
- BufferedWriter bw = new BufferedWriter(fw);
- String cad = ProductID + "," + ProductCategory + "," + ProductType + "," + BrandName + "," + ProductName + "," + PricePerUnit + "," + TotalQuantity;
- bw.write(cad);
- bw.newLine();
- bw.close();
- fw.close();
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- }
- }
- public void buscarTemporal() {
- try {
- //Abrir Archivo
- FileWriter fw = new FileWriter(archivoTemporal, true);
- BufferedWriter bw = new BufferedWriter(fw);
- String cad = ProductID + "," + ProductCategory + "," + ProductType + "," + BrandName + "," + ProductName + "," + PricePerUnit + "," + TotalQuantity;
- bw.write(cad);
- bw.newLine();
- bw.close();
- fw.close();
- } catch (IOException ex) {
- Logger.getLogger(datosProductos.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void editar() {
- boolean res = false;
- try {
- //Abrir Archivo
- FileReader fr = new FileReader(archivoOriginal);
- BufferedReader br = new BufferedReader(fr);
- // Leer datos
- String leido;
- String[] cadenas;
- leido = br.readLine();
- while (leido != null) {
- cadenas = leido.split(",");
- ProductID = cadenas[0];
- ProductCategory = cadenas[1];
- ProductType = cadenas[2];
- BrandName = cadenas[3];
- ProductName = cadenas[4];
- PricePerUnit = Double.parseDouble(cadenas[5]);
- TotalQuantity = Integer.parseInt(cadenas[6]);
- if (!ProductID.equals(getKey())) {
- buscarTemporal();
- }
- leido = br.readLine();
- }
- //Cerrar Archivo
- br.close();
- fr.close();
- } catch (FileNotFoundException ex) {
- System.out.println("Error " + ex.getMessage());
- } catch (IOException ex) {
- System.out.println("Error " + ex.getMessage());
- }
- archivoOriginal.delete();
- archivoTemporal.renameTo(archivoOriginal);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement