Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package inlupp2;
  2.  
  3. import java.util.*;
  4. import kennel.*;
  5.  
  6. public class Main {
  7.     private static ArrayList<Dog> dogs = new ArrayList<Dog>();
  8.  
  9.     public static void main(String[] args) {
  10.     // debug dogs
  11.     Dog stalin = new Dog("Stalin", "Bolsjevik", 10000, 100);
  12.     Dog tax = new Dog("Urban", "Tax", 5, 84);
  13.     Dog rofl = new Dog("stalin", "Tax", 4, 23);
  14.     Dog omg = new Dog("omg", "mys", 76, 00);
  15.     Dog gg = new Dog("gg", "mys", 89, 12);
  16.     dogs.addAll(Arrays.asList(stalin, tax, rofl, omg, gg));
  17.  
  18.     if (args.length < 1) {
  19.         System.out.println("Please specify a command (r, l or d)");
  20.         System.exit(0);
  21.     }
  22.  
  23.     char cmd = args[0].charAt(0);
  24.     int numArgs = args.length - 1;
  25.  
  26.     switch (cmd) {
  27.         case 'r':
  28.         if (numArgs < 4) {
  29.             System.out.println("Please specify the parameters Name, Race, Age and Weight for the new dog.");
  30.             System.exit(0);
  31.         }
  32.         Dog newDog = new Dog(args[1], args[2], strToInt(args[3]), strToInt(args[4]));
  33.         dogs.add(newDog);
  34.         System.out.format("New dog \"%s\" added!\n", newDog.getName());
  35.         break;
  36.  
  37.         case 'l':
  38.         double minLen = 0;
  39.         if (numArgs > 0) {
  40.             minLen = strToDouble(args[1]);
  41.         }
  42.         printDogs(minLen);
  43.         break;
  44.  
  45.         case 'd':
  46.         if (numArgs < 1) {
  47.             System.out.println("Please specify the name(s) of the dog(s) you want to delete.");
  48.             System.exit(0);
  49.         }
  50.         ArrayList<Dog> toRemove = new ArrayList<Dog>(); // really ugly hack to avoid
  51.                                                         // ConcurrentModificationException
  52.         for (int i = 1; i < args.length; i++) {
  53.             boolean found = false;
  54.             int numRemove = 0;
  55.             for (Dog dog : dogs) {
  56.             if (dog.getName().toLowerCase().equals(args[i].toLowerCase())) {
  57.                 found = true;
  58.                 toRemove.add(dog);
  59.                 numRemove++;
  60.             }
  61.             }
  62.             if (found) {
  63.             System.out.format("Removing %d dog(s) called %s.\n", numRemove, args[i]);
  64.             }
  65.             else {
  66.             System.out.format("No dog named %s.\n", args[i]);
  67.             }
  68.         }
  69.         for (Dog dog : toRemove) {
  70.             dogs.remove(dog);
  71.         }
  72.         break;
  73.  
  74.         default:
  75.         System.out.println("Unknown command, exiting ...");
  76.         System.exit(0);
  77.         break;
  78.     }
  79.  
  80.     if (cmd != 'l') printDogs(0);
  81.     }
  82.  
  83.     private static void printDogs(double minLen) {
  84.     System.out.println("=====");
  85.     for (Dog dog : dogs) {
  86.         if (dog.getTailLength() >= minLen) {
  87.         System.out.println(dog);
  88.         System.out.println("=====");
  89.         }
  90.     }
  91.     }
  92.  
  93.     private static int strToInt(String str) {
  94.     try {
  95.         int num = Integer.parseInt(str);
  96.         return num;
  97.     }
  98.     catch (NumberFormatException e) {
  99.         System.out.format("\"%s\" is not formatted as an integer, exiting ...\n", str);
  100.         System.exit(0);
  101.     }
  102.  
  103.     return 0; // Will never happen, program has ended
  104.     }
  105.  
  106.     private static double strToDouble(String str) {
  107.     try {
  108.         double num = Double.parseDouble(str);
  109.         return num;
  110.     }
  111.     catch (NumberFormatException e) {
  112.         System.out.format("\"%s\" is not formatted as a decimal number, exiting ...\n", str);
  113.         System.exit(0);
  114.     }
  115.  
  116.     return 0; // Will never happen, program has ended
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement