Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package inlupp2;
- import java.util.*;
- import kennel.*;
- public class Main {
- private static ArrayList<Dog> dogs = new ArrayList<Dog>();
- public static void main(String[] args) {
- // debug dogs
- Dog stalin = new Dog("Stalin", "Bolsjevik", 10000, 100);
- Dog tax = new Dog("Urban", "Tax", 5, 84);
- Dog rofl = new Dog("stalin", "Tax", 4, 23);
- Dog omg = new Dog("omg", "mys", 76, 00);
- Dog gg = new Dog("gg", "mys", 89, 12);
- dogs.addAll(Arrays.asList(stalin, tax, rofl, omg, gg));
- if (args.length < 1) {
- System.out.println("Please specify a command (r, l or d)");
- System.exit(0);
- }
- char cmd = args[0].charAt(0);
- int numArgs = args.length - 1;
- switch (cmd) {
- case 'r':
- if (numArgs < 4) {
- System.out.println("Please specify the parameters Name, Race, Age and Weight for the new dog.");
- System.exit(0);
- }
- Dog newDog = new Dog(args[1], args[2], strToInt(args[3]), strToInt(args[4]));
- dogs.add(newDog);
- System.out.format("New dog \"%s\" added!\n", newDog.getName());
- break;
- case 'l':
- double minLen = 0;
- if (numArgs > 0) {
- minLen = strToDouble(args[1]);
- }
- printDogs(minLen);
- break;
- case 'd':
- if (numArgs < 1) {
- System.out.println("Please specify the name(s) of the dog(s) you want to delete.");
- System.exit(0);
- }
- ArrayList<Dog> toRemove = new ArrayList<Dog>(); // really ugly hack to avoid
- // ConcurrentModificationException
- for (int i = 1; i < args.length; i++) {
- boolean found = false;
- int numRemove = 0;
- for (Dog dog : dogs) {
- if (dog.getName().toLowerCase().equals(args[i].toLowerCase())) {
- found = true;
- toRemove.add(dog);
- numRemove++;
- }
- }
- if (found) {
- System.out.format("Removing %d dog(s) called %s.\n", numRemove, args[i]);
- }
- else {
- System.out.format("No dog named %s.\n", args[i]);
- }
- }
- for (Dog dog : toRemove) {
- dogs.remove(dog);
- }
- break;
- default:
- System.out.println("Unknown command, exiting ...");
- System.exit(0);
- break;
- }
- if (cmd != 'l') printDogs(0);
- }
- private static void printDogs(double minLen) {
- System.out.println("=====");
- for (Dog dog : dogs) {
- if (dog.getTailLength() >= minLen) {
- System.out.println(dog);
- System.out.println("=====");
- }
- }
- }
- private static int strToInt(String str) {
- try {
- int num = Integer.parseInt(str);
- return num;
- }
- catch (NumberFormatException e) {
- System.out.format("\"%s\" is not formatted as an integer, exiting ...\n", str);
- System.exit(0);
- }
- return 0; // Will never happen, program has ended
- }
- private static double strToDouble(String str) {
- try {
- double num = Double.parseDouble(str);
- return num;
- }
- catch (NumberFormatException e) {
- System.out.format("\"%s\" is not formatted as a decimal number, exiting ...\n", str);
- System.exit(0);
- }
- return 0; // Will never happen, program has ended
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement