Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.Optional;
- import java.util.Vector;
- import javax.swing.JOptionPane;
- import javax.swing.JTextArea;
- public class MainFour {
- /**
- * [A]dd - Allow user to add pet's information in a vector.
- • [I]nsert - Insert pet's information to a specific position of a vector.
- • [S]earch - Display found pet's information after searching from a vector. {Search by index and by value}
- • [E]dit - Modify pet's information to an existing element from a vector. {Edit by index and by value}
- • [V]iew - View all data of a vector.
- • [D]elete - Erase pet's information from a vector. {Delete by index and by value}
- • [C]ount - Display the total size of a vector.
- • [T]erminate - Terminate the entire program.
- * @param args
- */
- @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
- private static Vector<Pets> pets = new Vector<>();
- private static String hold = "NAME\tGENDER\tAGE\tPRICE\n";
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String choice;
- String holder = "";
- try{
- do {
- String menu []= {"ADD","INSERT","SEARCH","EDIT","VIEW","DELETE","COUNT","TERMINATE"};
- choice= JOptionPane.showInputDialog(null,"MENU","CHOOSE",1,null,menu,menu[0]).toString();
- switch(choice){
- case "ADD":
- String name = JOptionPane.showInputDialog("NAME: ").toUpperCase();
- String g[] = {"MALE","FEMALE","UNDETERMINE"};
- String gender = JOptionPane.showInputDialog(null,"GENDER","CHOOSE",1,null,g,g[0]).toString();
- String breed =JOptionPane.showInputDialog("BREED: ").toUpperCase();
- Double price =Double.parseDouble(JOptionPane.showInputDialog("PRICE: "));
- pets.add(new Pets(name, gender, breed, price)); //adding all pets to 1 instance of vector that holds an object of Pets
- JOptionPane.showMessageDialog(null, "Successfully added!");
- break;
- case "INSERT":
- try {
- String s[] = {"MALE","FEMALE","UNDETERMINE"};
- int index = Integer.parseInt(JOptionPane.showInputDialog("INSERT AT INDEX: "));
- String newName = JOptionPane.showInputDialog("NAME: ");
- String newGender = JOptionPane.showInputDialog(null,"GENDER","CHOOSE",1,null,s,s[0]).toString();
- String newBreed= JOptionPane.showInputDialog("BREED: ");
- double newPrice=Double.parseDouble(JOptionPane.showInputDialog("PRICE: "));
- pets.insertElementAt(new Pets(newName, newGender, newBreed, newPrice), index); //adding a pet at a specific position within vector that holds an array of pets
- JOptionPane.showMessageDialog(null, "Successfuly inserted");
- }catch(Exception f) {
- JOptionPane.showMessageDialog(null, "Invalid Index");
- }
- break;
- case "SEARCH":
- // int index = getPetByName(pets.);
- // System.out.println(index);
- hold = "NAME\tGENDER\tAGE\tPRICE\n";
- try {
- String numRegex = ".*[0-9].*";
- String input = JOptionPane.showInputDialog("Search: ");
- if (input.matches(numRegex)) { // if the input is a number .. we went to search by index
- int i = Integer.parseInt(input);
- String someName = pets.get(i).getPetName();
- String someGender = pets.get(i).getPetGender();
- String someBreed = pets.get(i).getPetBreed();
- double somePrice = pets.get(i).getPrice();
- hold += someName + "\t" + someGender + "\t" + someBreed + "\t" + somePrice +"\n";
- JOptionPane.showMessageDialog(null, new JTextArea(hold));
- } else { //else search by name
- displayPets(input.toUpperCase());
- JOptionPane.showMessageDialog(null, new JTextArea(hold));
- }
- // JOptionPane.showMessageDialog(null, G);
- }catch(Exception t) {
- t.printStackTrace();
- JOptionPane.showMessageDialog(null, "Can't be found");
- }
- break;
- case "EDIT":
- try {
- String petName = JOptionPane.showInputDialog("Enter Pet to edit by Name: ");
- Optional<Pets> findPet = pets.stream().filter(f -> f.getPetName().equals(petName.toUpperCase())).findFirst();
- if (findPet.isPresent()) {
- Pets editPet = findPet.get();
- String Ng[] = {"MALE","FEMALE","UNDETERMINE"};
- String Nname = JOptionPane.showInputDialog("NAME: ");
- String Ngender= JOptionPane.showInputDialog(null, "EDIT","CHOOSE",1,null,Ng,Ng[0]).toString();
- String Nbreed = JOptionPane.showInputDialog("BREED: ");
- double Nprice = Double.parseDouble(JOptionPane.showInputDialog(null,"PRICE"));
- System.out.println("Pet has been edited previously known as " + editPet.getPetName() + " new name is: " + Nname);
- editPet.setPetName(Nname);
- editPet.setPetGender(Ngender);
- editPet.setPetBreed(Nbreed);
- editPet.setPrice(Nprice);
- JOptionPane.showMessageDialog(null, "Successfully edited");
- }
- }catch (Exception t) {
- JOptionPane.showMessageDialog(null, "Invalid index");
- }
- break;
- case "VIEW":
- //now we want to iterate over the vector and add all of the pets to a String which are later displayed..
- displayPets("");
- JOptionPane.showMessageDialog(null, new JTextArea(hold));
- break;
- case "DELETE":
- try {
- String petName = JOptionPane.showInputDialog("Name: ");
- System.out.println(petName);
- Optional<Pets> petDetails = pets.stream().filter(f -> f.getPetName().equals(petName.toUpperCase())).findFirst();
- if (petDetails.isPresent()) {
- Pets pet = petDetails.get();
- System.out.println("Deleted: " + pet.getPetName());
- pets.remove(pet);
- }
- JOptionPane.showMessageDialog(null, "Successfully deleted");
- } catch (Exception b) {
- JOptionPane.showMessageDialog(null, "Index can't be found!");
- }
- break;
- case "COUNT":
- JOptionPane.showMessageDialog(null, "SIZE: "+ pets.size());
- break;
- case "TERMINATE":
- JOptionPane.showMessageDialog(null, "THE PROGRAM IS TERMINATING...");
- System.exit(0);
- break;
- }
- }while(!choice.equals("TERMINATE"));
- }catch(Exception e){
- System.err.println("PROGRAM IS TERMINATING...");
- }
- }
- public static void displayPets(String name) {
- if (name.equals("")) {
- pets.stream().forEach(pet -> {
- String someName = pet.getPetName();
- String someGender = pet.getPetGender();
- String someBreed = pet.getPetBreed();
- double somePrice = pet.getPrice();
- hold += someName + "\t" + someGender + "\t" + someBreed + "\t" + somePrice +"\n";
- });
- } else {
- Optional<Pets> searchPet = pets.stream().filter(f -> f.getPetName().equals(name.toUpperCase())).findFirst();
- if (searchPet.isPresent()) {
- Pets pet = searchPet.get();
- String someName = pet.getPetName();
- String someGender = pet.getPetGender();
- String someBreed = pet.getPetBreed();
- double somePrice = pet.getPrice();
- hold += someName + "\t" + someGender + "\t" + someBreed + "\t" + somePrice +"\n";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement