Jump to content

gravação em ficheiros .dat


filipe3632

Recommended Posts

Boa tarde pessoal, estou a precisar de ajuda para resolver um problema, tenho aqui este código, este método, que é para introduzir os clientes, e grava para um array list e imprime os dados a cada vez que introduzimos um cliente novo... e eu queria conseguir grava para ficheiro .dat, mas não sei como posso fazer isso. será que me podiam da uma pequena grande ajuda?

E cada vez que eu fechasse o programa e voltasse a abrir ele carrega-se ainda os clientes, ou seja, quando eu fosse introduzir outro cliente os outros ainda existem.

public static void insereCliente(ArrayList<Cliente> listc){
	 int i, n=0;
	 boolean conf = true;
	 while (conf){
	 try{
		 n = Integer.parseInt(JOptionPane.showInputDialog("Introduza o número de clientes da loja:"));
		 } catch (InputMismatchException e)
			 {System.out.println("Tente outra vez..");}
			 catch (NumberFormatException e)
			 {System.out.println("Só pode introduzir números!!");}
	 for (i=1;i<=n;i++){

	 Cliente cli1;
		 String cnome = (JOptionPane.showInputDialog("Introduza o nome do Cliente:"));
		 Data dtNasc = Data.lerData();
		 String cendereco = (JOptionPane.showInputDialog("Introduz o endereço postal do Cliente:"));
		 String ctelefone = (JOptionPane.showInputDialog("Introduza o nr. de telefone do Cliente:"));
		 if(ctelefone.length() != 9){
				 System.out.println("ERRRRRRRRRRRRRRRRRRRROOOO");
				 return;
		 }

//				
	 cli1 = new Cliente(cnome, dtNasc, cendereco, ctelefone);
	 listc.add(cli1);
	 for (int j = 0; j < listc.size(); j++) {
						 System.out.println("Nome:" +(listc.get(j).getNome()));
						 System.out.println("Nr. Cliente:" +(listc.get(j).getNrcliente()));
						 System.out.println("Data de Nascimento:" +(listc.get(j).getD_nasc()));
						 System.out.println("Endereço:" +(listc.get(j).getEndereco()));
						 System.out.println("Telefone:" +(listc.get(j).getTelefone())+ "\n");
	 }
	 }
	 int confirmacao;
	 confirmacao = JOptionPane.showConfirmDialog( null, "Quer introduzir mais algum cliente?");
		 if (confirmacao == JOptionPane.YES_OPTION)
			 conf = true;
		 else
			 conf=false;
	 }
}

Têm aqui também o código da classe cliente para poderem ver ...

package moprogestaoloja;
import java.util.ArrayList;
import java.io.*;

/**
*
* @author Filipe
*/
public class Cliente extends Pessoas implements Serializable {
private static int nrcliGeral;
/**
 * @return the nrcliGeral
 */
public static int getNrcliGeral() {
 return nrcliGeral;
}
private int nrcliente;
private Data d_nasc; //public?
private String telefone;
public Cliente (String nome, Data d_nasc, String endereco, String telefone ){
 super (nome, endereco);
 this.d_nasc = d_nasc;
 this.endereco = endereco;
 this.telefone = telefone;
 nrcliGeral ++;
 this.nrcliente = nrcliGeral;
}
public Cliente (){
 super (null, null);
 this.d_nasc = null;
 this.nrcliente = nrcliGeral;
 this.telefone = null;
 nrcliGeral ++;
}
public Cliente (String nome, int nrcliente, Data d_nasc, String endereco, String telefone){
 super (nome, endereco);
 this.d_nasc = d_nasc;
 this.nrcliente = nrcliGeral;
 this.telefone = telefone;
 nrcliGeral ++;
}
public static Cliente procuraC(ArrayList<Cliente> listc, int numCli){
 for(Cliente c:listc){
	 if(c.getNrcliente()== numCli)
		 return c;
 }
 return null;
}

/**
 * @return the nrcliente
 */
public int getNrcliente() {
 return nrcliente;
}
/**
 * @param nrcliente the nrcliente to set
 */
public void setNrcliente(int nrcliente) {
 this.nrcliente = nrcliente;
}
/**
 * @return the idade
 */

/**
 * @return the telefone
 */
public String getTelefone() {
 return telefone;
}
/**
 * @param telefone the telefone to set
 */
public void setTelefone(String telefone) {
 this.telefone = telefone;
}
/**
 * @return the d_nasc
 */
public Data getD_nasc() {
 return d_nasc;
}
/**
 * @param d_nasc the d_nasc to set
 */
public void setD_nasc(Data d_nasc) {
 this.d_nasc = d_nasc;
}

}

agradecia mesmo a ajuda 🙂

Edited by Baderous
geshi

Peace, Love & Empathy by: Kurt Cobain

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.