primeira classe main ( principal )
/**
*
-
@author Lenon
*/
package lojadepets ;
import java.io.;
import javax.swing.;
import java.util.ArrayList;
import javax.swing.JOptionPane ;
import java.awt.Component;
/**
* @param args the command line arguments
*/
public class LojadePets {
private static Component parent;
public static void main(String[] args) {
JFileChooser escolhe = new JFileChooser();
int returnVal = escolhe.showOpenDialog(parent);
File arq = new File("pet.dat");
FileOutputStream arq_w;
ObjectOutputStream flux_w;
FileInputStream arq_r;
ObjectInputStream flux_r;
Object temp;
//String nomearq, patharq;
//File localenome;
ArrayList<ListadePets> pets = new ArrayList<>();
try{
arq_r = new FileInputStream(arq);
flux_r = new ObjectInputStream(arq_r);
temp = flux_r.readObject();
pets = (ArrayList<ListadePets>) temp;
arq_r.close();
flux_r.close();
}
catch (Exception e){
e.printStackTrace();
}
try {
arq_w = new FileOutputStream(arq);
flux_w = new ObjectOutputStream(arq_w);
flux_w.writeObject(pets);
flux_w.close();
arq_w.close(); JOptionPane.showMessageDialog(null,"Dados gravados com sucesso no arquivo.");
}
catch (Exception e){
e.printStackTrace();
}
/*JOptionPane.showMessageDialog(null, "retorno do OpenDialog: " + returnVal);
nomearq = escolhe.getSelectedFile().getName();
JOptionPane.showMessageDialog(null, "Nome do arquivo escolhido: " + nomearq);
localenome = escolhe.getSelectedFile().getAbsoluteFile();
JOptionPane.showMessageDialog(null, "Nome do arquivo escolhido: " + localenome);
patharq = escolhe.getSelectedFile().getAbsolutePath();
JOptionPane.showMessageDialog(null, "Caminho do arquivo escolhido: " + patharq);
/*
for ( ListadePets i : pets ){
}
opcao = JOptionPane.showInputDialog(" Digite "
+ " \n (I) para inserir um pet "
+ " \n (E) para excluir um pet "
+ " \n (A) para alterar um pet "
+ " \n (L) para listar um pet: ");
*/
boolean laco= true;
while(laco) {
String[] options = {"Inserir um pet", "Excluir um pet", "Alterar um pet", "Listar um pet","Sair"};
int x = JOptionPane.showOptionDialog(null, "Selecione uma das opções",
"Pets",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
switch (x) {
case 0: {
/*
int ID = Integer.parseInt(JOptionPane.showInputDialog("\n Digite um o ID de um cliente "));
String nom_prop = JOptionPane.showInputDialog("\n Digite o nome do propetário");
String nom_pet = JOptionPane.showInputDialog("\n Digite o nome do pet");
String espec = JOptionPane.showInputDialog("\n Digite a especie ");
String rac = JOptionPane.showInputDialog("\n Digite a raça ");
Integer ida = Integer.parseUnsignedInt (JOptionPane.showInputDialog("\n Digite a idade "));
float pes = Float.parseFloat(JOptionPane.showInputDialog("\n Digite o peso ") );
char sex = JOptionPane.showInputDialog("\n Digite o sexo ").charAt(0);
boolean cas = Boolean.parseBoolean(JOptionPane.showInputDialog("\n Digite se é castrado ou não "));
do{
if (( sexo == 'm')&&( sexo == 'f'))
{
JOptionPane.showInputDialog("\n Sexo válido ") ;
}
}while ( ( sexo != 'm') && ( sexo != 'f' ) ) ;
}
*/
int ID = Integer.parseInt(JOptionPane.showInputDialog("\n Digite um o ID de um cliente(numero): "));
String nom_prop = JOptionPane.showInputDialog("\n Digite o nome do propetário: ");
String nom_pet = JOptionPane.showInputDialog("\n Digite o nome do pet: ");
String espec = JOptionPane.showInputDialog("\n Digite a especie: ");
String rac = JOptionPane.showInputDialog("\n Digite a raça: ");
Integer ida = Integer.parseUnsignedInt (JOptionPane.showInputDialog("\n Digite a idade(numero): "));
float pes = Float.parseFloat(JOptionPane.showInputDialog("\n Digite o peso(numero): ") );
//optionDialog
String[] opcoesSexo = {"Macho", "Fêmea"};
int sex = JOptionPane.showOptionDialog(null, "Selecione uma das opções",
"Pets",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, opcoesSexo, opcoesSexo[0]);
char sexoAnimal = 'A';
if(sex == 0){sexoAnimal = 'M';}
else {sexoAnimal = 'F';}
//optionDialog
String[] opcoesCastrado = {"Castrado", "Não castrado"};
int cas = JOptionPane.showOptionDialog(null, "Selecione uma das opções",
"Pets",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, opcoesCastrado, opcoesCastrado[0]);
boolean castradoAnimal = false;
if(cas == 0){castradoAnimal = true;}
else {castradoAnimal = false;}
pets.add(new ListadePets(ID,nom_prop,nom_pet,espec,rac,ida,pes,sexoAnimal,castradoAnimal));
}
break;
case 1: {//remoção de um pet
boolean removido = false;
int ID = Integer.parseInt(JOptionPane.showInputDialog(" Digite um o ID do cliente a ser removido: "));
for(ListadePets p: pets) {
if(p.getID() == ID) {
pets.remove(p);
removido = true;
//mensagem em janela
String mensagem = "O pet foi removido com sucesso";
JOptionPane.showMessageDialog (null, mensagem);
break;
}
}
if(!removido) {
//mensagem em janela
String mensagem = "Não possui um pet registrado com esse ID";
JOptionPane.showMessageDialog (null, mensagem);
}
}
break ;
case 2: {
int check ;
int idtemp = 0 ;
for (int i = 0; i < pets.size(); i++) {
int ID = Integer.parseInt(JOptionPane.showInputDialog(" Digite um o ID de um cliente "));
i = ID;
do{
check = 0 ;
if ( ID == idtemp ) {
break ;
}
else {
check = 1 ;
}
}while ( check == 0 ) ;
String nom_prop = JOptionPane.showInputDialog("\n Digite o nome do propetário");
String nom_pet = JOptionPane.showInputDialog("\n Digite o nome do pet");
String espec = JOptionPane.showInputDialog("\n Digite a especie");
String rac = JOptionPane.showInputDialog("\n Digite a raça ");
int ida = Integer.parseInt(JOptionPane.showInputDialog("\n Digite a idade "));
float pes = Float.parseFloat( JOptionPane.showInputDialog("\n Digite o peso "));
char sex = JOptionPane.showInputDialog("\n Digite o sexo ").charAt(0);
boolean cas = Boolean.parseBoolean(JOptionPane.showInputDialog("\n Digite se é castrado ou não "));
}
}
break;
case 3: {
for ( int i = 0 ; i < pets.size() ; i++){
JOptionPane.showMessageDialog(null,"ID:" + pets.get(i).getID()+ "\n"
+ "Nome do Propetário:" + pets.get(i).getNomePropetario()+"\n"
+ "Nome do Pet :" + pets.get(i).getNomePet() + "\n"
+ "Especie:" + pets.get(i).getEspecie() + "\n"
+ "Raça:" + pets.get(i).getRaca() + "\n"
+ "Idade:" + pets.get(i).getIdade() + "\n"
+ "Peso:" + pets.get(i).getPeso()+ "\n"
+ "Sexo:" + pets.get(i).getSexo() + "\n"
+ "Castrado:" + pets.get(i).getCastrado() + "\n"
+ "\n---------------" );
}
}//case 3
default:
{
laco = false;
break;
}
}//switch
}//while
}//pets na loja
}//classe
segunda classe :
/**
*
-
@author Lenon
*/
package lojadepets ;
public class ListadePets {
private Integer ID ;
private String nome_prop ;
private String nome_pet ;
private String especie ;
private String raca ;
private Integer idade ;
private float peso ;
private char sexo ;
private boolean castrado ;
public ListadePets(){
//construtor padrao
}
public ListadePets ( Integer ID ,String nome_prop , String nome_pet , String especie , String raca ,Integer idade , float peso ,char sexo , boolean castrado )
{
this.ID = ID ;
this.nome_prop = nome_prop ;
this.nome_pet = nome_pet ;
this.especie = especie ;
this.raca = raca ;
this.idade = idade ;
this.peso = peso ;
this.sexo = sexo ;
this.castrado = castrado ;
}
public Integer getID ( )
{
return this.ID ;
}
public String getNomePropetario ( )
{
return this.nome_prop ;
}
public String getNomePet ( )
{
return this.nome_pet ;
}
public String getEspecie ( )
{
return this.especie ;
}
public String getRaca ( )
{
return this.raca ;
}
public Integer getIdade ( )
{
return this.idade ;
}
public float getPeso ( )
{
return this.peso ;
}
public char getSexo ( )
{
return this.sexo ;
}
public boolean getCastrado ( )
{
return this.castrado ;
}
public void setID ( Integer ID )
{
this.ID = ID ;
}
public void setNomePropetario ( String nome_prop )
{
this.nome_prop = nome_prop ;
}
public void setNomePet ( String nome_pet )
{
this.nome_pet = nome_pet ;
}
public void setEspecie ( String especie )
{
this.especie = especie ;
}
public void setRaca ( String raca )
{
this.raca = raca ;
}
public void setIdade ( Integer idade )
{
this.idade = idade ;
}
public void setPeso ( float peso )
{
this.peso = peso ;
}
public void setSexo ( char sexo )
{
this.sexo = sexo ;
}
public void setCastrado ( boolean castrado )
{
this.castrado = castrado ;
}
}