Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. //Robin Geijer roge8388
  2.  
  3. //32. 32? 33.
  4.  
  5. import java.util.Scanner;
  6. import java.util.ArrayList;
  7.  
  8. public class DogProgram {
  9.  
  10. private String name;
  11. private String breed;
  12. private int age;
  13. private int weight;
  14.  
  15. private ArrayList<Dog> dogList = new ArrayList<Dog>();
  16.  
  17.  
  18. private Scanner input = new Scanner(System.in);
  19.  
  20.  
  21. public void initialize() {
  22. System.out.println("Welcome! Enter one of the following commands:");
  23. System.out.println("- Register new dog");
  24. System.out.println("- Increase age");
  25. System.out.println("- List dogs");
  26. System.out.println("- Remove dog");
  27. System.out.println("- Exit");
  28. }
  29.  
  30. public void runCommandLoop(){
  31. boolean ended;
  32. do {
  33. String command = readCommand();
  34. ended = handleCommand(command);
  35. } while(!ended);
  36.  
  37. }
  38.  
  39. public String readCommand(){
  40. System.out.print("Command: ");
  41. String command = input.nextLine();
  42. return command;
  43. }
  44.  
  45. public boolean handleInput(String userInput){
  46. boolean isFilled = false;
  47. if(!userInput.isEmpty()){
  48. isFilled = true;
  49. }
  50.  
  51. return isFilled;
  52. }
  53.  
  54. public void registerNewDog() {
  55. System.out.println("You've entered the command \"Register new dog.\"");
  56. String userInput = ""; //Kommer lagra det användaren matar in
  57.  
  58. //Alla de här 4 är exakt lika, bara att det är olika egenskaper de frågar efter, därför kommenterar jag bara den översta
  59. System.out.println("Name: ");
  60. userInput = input.nextLine(); //Ber användaren knappa in name/breed/age/weight och lagrar i en STRING
  61. while(!handleInput(userInput.trim())){ //Så länge som handleInput returnerar false
  62. System.out.println("Error: the name can’t be empty");
  63. System.out.println("Name:"); //Fråga efter namn/bla/bla
  64. userInput = input.nextLine(); //Skriv in
  65. } //Om den lämnar while-loopen så betyder det att handleInput = true
  66. name = userInput; //Lagra det användaren skrev i name eller vad det nu är
  67.  
  68. System.out.println("Breed: ");
  69. userInput = input.nextLine();
  70. while(!handleInput(userInput.trim())){
  71. System.out.println("Error: the breed can’t be empty");
  72. System.out.println("Breed:");
  73. userInput = input.nextLine();
  74. }
  75. breed = userInput;
  76.  
  77. System.out.println("Age: ");
  78. userInput = input.nextLine();
  79. while(!handleInput(userInput.trim()) ){
  80. System.out.println("Error: the age can’t be empty and has to be numbers");
  81. System.out.println("Age:");
  82. userInput = input.nextLine();
  83. }
  84. age = Integer.parseInt(userInput); //Konverterar om String till int då det användaren matar in lagras som string och age/weight är int
  85.  
  86. System.out.println("Weight: ");
  87. userInput = input.nextLine();
  88. while(!handleInput(userInput.trim())){
  89. System.out.println("Error: the weight can’t be empty and has to be numbers");
  90. System.out.println("Weight:");
  91. userInput = input.nextLine();
  92. }
  93. weight = Integer.parseInt(userInput);
  94.  
  95. Dog newDog = new Dog(name, breed, age, weight);
  96. dogList.add(newDog);
  97.  
  98. }
  99.  
  100.  
  101. private boolean handleCommand(String command){
  102. switch (command.toUpperCase()) {
  103. case "EXIT":
  104. return true;
  105. case "REGISTER NEW DOG":
  106. registerNewDog();
  107. break;
  108. case "INCREASE AGE":
  109. changeAge();
  110. break;
  111. case "LIST DOGS":
  112. listAllDogs();
  113. break;
  114. case "REMOVE DOG":
  115. removeDog();
  116. break;
  117. default:
  118. System.out.println("Error: No such command.");
  119. }
  120. return false;
  121. }
  122.  
  123. public void changeAge() {
  124. System.out.println("You've entered the command \"Increase age.\"");
  125. System.out.println("Enter the name of the dog that has aged.");
  126. name = input.nextLine().trim();
  127.  
  128. int nameMatches = 0;
  129.  
  130. for (Dog dog : dogList) {
  131. if (name.equalsIgnoreCase(dog.getName())) {
  132.  
  133. dog.increaseAge(1);
  134. nameMatches++;
  135. System.out.println("The dog's age has been updated.");
  136. }
  137. }
  138.  
  139. if (nameMatches == 0) {
  140. System.out.println("Error: Dog not found in register.");
  141. }
  142.  
  143. }
  144.  
  145. public void listAllDogs() {
  146. System.out.println("You've entered the command \"List dogs.\"");
  147. System.out.println("Enter the minimum tail length to search for.");
  148. double minLength = input.nextDouble();
  149. input.nextLine();
  150.  
  151. if (minLength == 0) {
  152.  
  153. for (Dog dog : dogList) {
  154.  
  155. dog.getTailLength();
  156. System.out.println(dog);
  157.  
  158. }
  159. }
  160. else {
  161.  
  162. for (Dog dog : dogList) {
  163.  
  164. if (dog.getTailLength() >= minLength) {
  165. System.out.println(dog);
  166.  
  167. }
  168. }
  169.  
  170. }
  171.  
  172. }
  173.  
  174. public void removeDog() {
  175. System.out.println("You gave the command \"remove dog\"");
  176. System.out.println("Please enter name: ");
  177. name = input.nextLine().trim();
  178.  
  179. int matchingDogs = 0;
  180. for (Dog dog : dogList) {
  181. if(name.equalsIgnoreCase(dog.getName())) {
  182. dog.toString();
  183. dogList.remove(dog);
  184. System.out.println(name+ " has been removed from register");
  185. matchingDogs++;
  186. break;
  187. }
  188. }
  189.  
  190. if (matchingDogs==0) {
  191. System.out.println("Error: dog with given name could not be found");
  192.  
  193. }
  194. }
  195.  
  196. public void exitProgram(){
  197. System.out.println("Bye");
  198. }
  199.  
  200. private void run(){
  201. initialize();
  202. runCommandLoop();
  203. exitProgram();
  204. }
  205.  
  206. public static void main(String[] args){
  207. new DogProgram().run();
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement