aew pessoal estou colocando o meu projeto da faculdade, dai quem precisa pega algum metodo do projeto ta ai, qualquer duvida é soh pergunta. A sim esse programa consiste em cadastra, lista e buscar nome de cliente.
falow
import java.io.*;
import java.util.StringTokenizer;
public class Biblioteca{
public void Cadastrar()throws IOException{
Biblioteca x = new Biblioteca();
String dados;
dados = x.Ler();
String vetor[] = new String[5];
int c = 0;
BufferedReader ler = new BufferedReader( new
InputStreamReader(System.in));
System.out.println("\nDigite o Nome: ");
vetor[0] = ler.readLine();
System.out.println("\nDigite o CPF: ");
vetor[1] = ler.readLine();
System.out.println("\nDigite o Endereço: ");
vetor[2] = ler.readLine();
System.out.println("\nDigite o Cor: ");
vetor[3] = ler.readLine();
System.out.println("\nDigite o Nascimento: ");
vetor[4] = ler.readLine();
dados += vetor[0] + ";" + vetor[1] + ";" + vetor[2] + ";" + vetor[3] + ";" + vetor[4];
this.Salvar(dados);
}
public void Salvar(String a)throws IOException{
try{
RandomAccessFile file = new RandomAccessFile( "Clientes.txt", "rw" ); // rw serve para ler e salvar arquivo
file.writeUTF(a.trim());
file.close();
}
catch(Exception e){
}
}
public String[] AbrirArquivo(){ // Método que recebe um parâmetro, cuja função é abrir um arquivo
String[] V;
int cont = 0;
String line_str;
try{
BufferedReader data = new BufferedReader(new FileReader("Clientes.txt"));
while((line_str = data.readLine()) != null)
cont++;
data.close( );
}catch (IOException e) {}
V = new String[cont];
cont = 0;
try{
BufferedReader data = new BufferedReader(new FileReader("Clientes.txt"));
while((line_str = data.readLine()) != null) {
V[cont] = line_str;
cont++;
}
data.close();
} catch (IOException e) {}
String temp;
int max = V.length;
for(int i=0; i<max-1; i++)
for (int j=i+1; j<max; j++) {
if (V[i].compareTo(V[j])>0) {
temp = V[i];
V[i] = V[j];
V[j] = temp;
}
}
return V;
}
public String Ler(){
String line_str;
String Arquivo = "";
try{
BufferedReader data = new BufferedReader(new FileReader("Clientes.txt"));
while((line_str = data.readLine()) != null)
Arquivo += line_str + "\n";
data.close( );
}catch (IOException e) {}
return Arquivo;
}
public static void main (String args[]) throws IOException{
Biblioteca x = new Biblioteca();
BufferedReader ler = new BufferedReader( new InputStreamReader(System.in));
int c;
String Nome[];
do{
c = x.menu_select();
switch(c){
case 1: x.Cadastrar();
break;
case 2: x.Listar();
break;
case 3: Nome = x.Buscar();
System.out.println("Nome: " + Nome[0] + "\nCPF: " + Nome[1] + "\nEndereco: " + Nome[2] +
"\nCor: " + Nome[3] + "\nNascimento: " + Nome[4]);
break;
case 4: System.exit(0);
}
}while (c != 4);
}
public int menu_select() throws IOException{
BufferedReader ler = new BufferedReader( new InputStreamReader(System.in));
String c;
int a;
System.out.println("\n 1. Cadastrar");
System.out.println(" 2. Listar");
System.out.println(" 3. Buscar");
System.out.println(" 4. Sair\n");
do{
System.out.print("Informe sua escolha: ");
c = ler.readLine();
a = Integer.parseInt(c);
} while (a < 0 || a > 4);
return a;
}
public void Listar(){
Biblioteca x = new Biblioteca();
String V[] = x.AbrirArquivo();
int c = 0, contador = 0;
System.out.println("Nome\tCPF\tEndereço\tCor\tNascimento");
while (c < V.length){
StringTokenizer capturador = new StringTokenizer(V[c],";");
String leitorDados[] = {"","","","",""};
while (capturador.hasMoreTokens()) {
leitorDados[contador]= capturador.nextToken();
contador++;
}
System.out.println(leitorDados[0] + "\t" + leitorDados[1] + "\t" + leitorDados[2] + "\t\t" + leitorDados[3] +"\t" + leitorDados[4]);
c++;
contador = 0;
}
}
public String[] Buscar() throws IOException{
BufferedReader ler = new BufferedReader( new InputStreamReader(System.in));
Biblioteca x = new Biblioteca();
String V[] = x.AbrirArquivo();
String V2[] = new String[5];
String c;
int i = 0, contador = 0;
System.out.println("Informe o nome para a busca: ");
c = ler.readLine();
while (i < V.length){
StringTokenizer capturador = new StringTokenizer(V[i],";");
String leitorDados[] = {"","","","",""};
while (capturador.hasMoreTokens()) {
leitorDados[contador]= capturador.nextToken();
contador++;
}
if( leitorDados[0].equalsIgnoreCase(c)){
V2 = leitorDados;
break;
}
i++;
}
return V2;
}
}
[size=“11”]
[color=“red”]
- Editado: Lembre-se de usar BBCode em seus códigos para mantê-los identados e mais legíveis para outros usuários - Matheus[/color][/size] :joia: