filipe3632 Posted June 21, 2012 at 04:50 PM Report Share #464655 Posted June 21, 2012 at 04:50 PM (edited) 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 June 26, 2012 at 12:05 AM by Baderous geshi Peace, Love & Empathy by: Kurt Cobain Link to comment Share on other sites More sharing options...
filipe3632 Posted June 22, 2012 at 02:43 PM Author Report Share #464966 Posted June 22, 2012 at 02:43 PM Precisava mesmo da vossa ajuda... Peace, Love & Empathy by: Kurt Cobain Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted June 22, 2012 at 02:45 PM Report Share #464968 Posted June 22, 2012 at 02:45 PM a única coisa que precisas é implementar o interface serializable nos teus objectos para que possam facilmente serem gravados/lidos de ficheiros em modo binário procura na net sobre esse tema porque estar a explicar tudo seria um texto enorme IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now