Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.30 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.Optional;
  3. import java.util.Vector;
  4. import javax.swing.JOptionPane;
  5. import javax.swing.JTextArea;
  6.  
  7. public class MainFour {
  8. /**
  9. * [A]dd - Allow user to add pet's information in a vector.
  10. • [I]nsert - Insert pet's information to a specific position of a vector.
  11. • [S]earch - Display found pet's information after searching from a vector. {Search by index and by value}
  12. • [E]dit - Modify pet's information to an existing element from a vector. {Edit by index and by value}
  13. • [V]iew - View all data of a vector.
  14. • [D]elete - Erase pet's information from a vector. {Delete by index and by value}
  15. • [C]ount - Display the total size of a vector.
  16. • [T]erminate - Terminate the entire program.
  17.  
  18. * @param args
  19. */
  20. @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
  21.  
  22. private static Vector<Pets> pets = new Vector<>();
  23. private static String hold = "NAME\tGENDER\tAGE\tPRICE\n";
  24. public static void main(String[] args) {
  25. // TODO Auto-generated method stub
  26.  
  27. String choice;
  28. String holder = "";
  29. try{
  30. do {
  31.  
  32. String menu []= {"ADD","INSERT","SEARCH","EDIT","VIEW","DELETE","COUNT","TERMINATE"};
  33.  
  34.  
  35.  
  36. choice= JOptionPane.showInputDialog(null,"MENU","CHOOSE",1,null,menu,menu[0]).toString();
  37. switch(choice){
  38. case "ADD":
  39. String name = JOptionPane.showInputDialog("NAME: ").toUpperCase();
  40. String g[] = {"MALE","FEMALE","UNDETERMINE"};
  41. String gender = JOptionPane.showInputDialog(null,"GENDER","CHOOSE",1,null,g,g[0]).toString();
  42. String breed =JOptionPane.showInputDialog("BREED: ").toUpperCase();
  43. Double price =Double.parseDouble(JOptionPane.showInputDialog("PRICE: "));
  44.  
  45. pets.add(new Pets(name, gender, breed, price)); //adding all pets to 1 instance of vector that holds an object of Pets
  46.  
  47. JOptionPane.showMessageDialog(null, "Successfully added!");
  48. break;
  49.  
  50. case "INSERT":
  51. try {
  52. String s[] = {"MALE","FEMALE","UNDETERMINE"};
  53. int index = Integer.parseInt(JOptionPane.showInputDialog("INSERT AT INDEX: "));
  54. String newName = JOptionPane.showInputDialog("NAME: ");
  55. String newGender = JOptionPane.showInputDialog(null,"GENDER","CHOOSE",1,null,s,s[0]).toString();
  56. String newBreed= JOptionPane.showInputDialog("BREED: ");
  57. double newPrice=Double.parseDouble(JOptionPane.showInputDialog("PRICE: "));
  58.  
  59. pets.insertElementAt(new Pets(newName, newGender, newBreed, newPrice), index); //adding a pet at a specific position within vector that holds an array of pets
  60.  
  61. JOptionPane.showMessageDialog(null, "Successfuly inserted");
  62. }catch(Exception f) {
  63. JOptionPane.showMessageDialog(null, "Invalid Index");
  64. }
  65. break;
  66.  
  67. case "SEARCH":
  68. // int index = getPetByName(pets.);
  69. // System.out.println(index);
  70. hold = "NAME\tGENDER\tAGE\tPRICE\n";
  71. try {
  72. String numRegex = ".*[0-9].*";
  73.  
  74. String input = JOptionPane.showInputDialog("Search: ");
  75.  
  76. if (input.matches(numRegex)) { // if the input is a number .. we went to search by index
  77. int i = Integer.parseInt(input);
  78. String someName = pets.get(i).getPetName();
  79. String someGender = pets.get(i).getPetGender();
  80. String someBreed = pets.get(i).getPetBreed();
  81. double somePrice = pets.get(i).getPrice();
  82. hold += someName + "\t" + someGender + "\t" + someBreed + "\t" + somePrice +"\n";
  83. JOptionPane.showMessageDialog(null, new JTextArea(hold));
  84. } else { //else search by name
  85.  
  86. displayPets(input.toUpperCase());
  87. JOptionPane.showMessageDialog(null, new JTextArea(hold));
  88. }
  89.  
  90. // JOptionPane.showMessageDialog(null, G);
  91. }catch(Exception t) {
  92. t.printStackTrace();
  93. JOptionPane.showMessageDialog(null, "Can't be found");
  94. }
  95. break;
  96.  
  97. case "EDIT":
  98.  
  99. try {
  100.  
  101. String petName = JOptionPane.showInputDialog("Enter Pet to edit by Name: ");
  102. Optional<Pets> findPet = pets.stream().filter(f -> f.getPetName().equals(petName.toUpperCase())).findFirst();
  103.  
  104. if (findPet.isPresent()) {
  105.  
  106. Pets editPet = findPet.get();
  107. String Ng[] = {"MALE","FEMALE","UNDETERMINE"};
  108. String Nname = JOptionPane.showInputDialog("NAME: ");
  109. String Ngender= JOptionPane.showInputDialog(null, "EDIT","CHOOSE",1,null,Ng,Ng[0]).toString();
  110. String Nbreed = JOptionPane.showInputDialog("BREED: ");
  111. double Nprice = Double.parseDouble(JOptionPane.showInputDialog(null,"PRICE"));
  112.  
  113. System.out.println("Pet has been edited previously known as " + editPet.getPetName() + " new name is: " + Nname);
  114.  
  115. editPet.setPetName(Nname);
  116. editPet.setPetGender(Ngender);
  117. editPet.setPetBreed(Nbreed);
  118. editPet.setPrice(Nprice);
  119. JOptionPane.showMessageDialog(null, "Successfully edited");
  120.  
  121. }
  122.  
  123. }catch (Exception t) {
  124. JOptionPane.showMessageDialog(null, "Invalid index");
  125. }
  126. break;
  127.  
  128. case "VIEW":
  129.  
  130. //now we want to iterate over the vector and add all of the pets to a String which are later displayed..
  131. displayPets("");
  132.  
  133. JOptionPane.showMessageDialog(null, new JTextArea(hold));
  134. break;
  135.  
  136. case "DELETE":
  137. try {
  138. String petName = JOptionPane.showInputDialog("Name: ");
  139. System.out.println(petName);
  140. Optional<Pets> petDetails = pets.stream().filter(f -> f.getPetName().equals(petName.toUpperCase())).findFirst();
  141.  
  142. if (petDetails.isPresent()) {
  143. Pets pet = petDetails.get();
  144. System.out.println("Deleted: " + pet.getPetName());
  145. pets.remove(pet);
  146. }
  147.  
  148. JOptionPane.showMessageDialog(null, "Successfully deleted");
  149.  
  150. } catch (Exception b) {
  151. JOptionPane.showMessageDialog(null, "Index can't be found!");
  152. }
  153.  
  154. break;
  155.  
  156.  
  157. case "COUNT":
  158.  
  159. JOptionPane.showMessageDialog(null, "SIZE: "+ pets.size());
  160.  
  161. break;
  162.  
  163. case "TERMINATE":
  164. JOptionPane.showMessageDialog(null, "THE PROGRAM IS TERMINATING...");
  165. System.exit(0);
  166. break;
  167.  
  168. }
  169. }while(!choice.equals("TERMINATE"));
  170. }catch(Exception e){
  171. System.err.println("PROGRAM IS TERMINATING...");
  172. }
  173. }
  174.  
  175. public static void displayPets(String name) {
  176.  
  177. if (name.equals("")) {
  178. pets.stream().forEach(pet -> {
  179. String someName = pet.getPetName();
  180. String someGender = pet.getPetGender();
  181. String someBreed = pet.getPetBreed();
  182. double somePrice = pet.getPrice();
  183. hold += someName + "\t" + someGender + "\t" + someBreed + "\t" + somePrice +"\n";
  184.  
  185. });
  186. } else {
  187.  
  188. Optional<Pets> searchPet = pets.stream().filter(f -> f.getPetName().equals(name.toUpperCase())).findFirst();
  189.  
  190. if (searchPet.isPresent()) {
  191. Pets pet = searchPet.get();
  192. String someName = pet.getPetName();
  193. String someGender = pet.getPetGender();
  194. String someBreed = pet.getPetBreed();
  195. double somePrice = pet.getPrice();
  196.  
  197. hold += someName + "\t" + someGender + "\t" + someBreed + "\t" + somePrice +"\n";
  198. }
  199. }
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement