Advertisement
Guest User

Controller.java

a guest
Apr 27th, 2015
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.StringTokenizer;
  4.  
  5. public class Controller {
  6.     public void startSystem() {
  7.         System.out.printf("Welcome to Hungry Puppies, please insert a correct input...\n");
  8.         Scanner in = new Scanner(System.in);
  9.         String input = in.nextLine();
  10.         while (!correctInput(input)) {
  11.             System.out.printf("Incorrect input! Please try again...\n");
  12.             input = in.nextLine();
  13.         }
  14.         StringTokenizer stringTokenizer = new StringTokenizer(input);
  15.         ArrayList<Puppy> puppies = new ArrayList<Puppy>();
  16.         while (stringTokenizer.hasMoreTokens()) {
  17.             Puppy pup = new Puppy(Integer.parseInt(stringTokenizer.nextToken()));
  18.             puppies.add(pup);
  19.         }
  20.         for (int i = 0; i < puppies.size(); i++) {
  21.             if (i == 0) {
  22.                 puppies.get(i).setHappiness(puppies.get(i + 1).getBiscuitSize());
  23.             } else if (i == puppies.size() - 1) {
  24.                 puppies.get(i).setHappiness(puppies.get(i - 1).getBiscuitSize());
  25.             } else {
  26.                 puppies.get(i).setHappiness(puppies.get(i - 1).getBiscuitSize(), puppies.get(i + 1).getBiscuitSize());
  27.             }
  28.         }
  29.  
  30.         for (int i = 0; i < puppies.size(); i++) {
  31.             System.out.printf("%s ", puppies.get(i).getMood());
  32.         }
  33.  
  34.  
  35.  
  36.     }
  37.  
  38.     public boolean correctInput(String input) {
  39.         for (int i = 0; i < input.length(); i++) {
  40.             if (input.charAt(i) < 48 || input.charAt(i) > 57) {
  41.                 if (input.charAt(i) == ' ') {
  42.                 } else {
  43.                     return false;
  44.                 }
  45.  
  46.             }
  47.         }
  48.         return true;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement