isak2

 avatar
unknown
java
2 years ago
11 kB
4
Indexable
//Isak Masoliver isma7969

import java.util.ArrayList;
import java.util.Iterator;



public class DogRegister {
    private static final String[] VALID_COMMANDS = {
            "Register new dog",
            "List dogs",
            "Increase age",
            "Remove dog",
            "Register new owner",
            "Give dog",
            "List owners",
            "Remove owned dog",
            "Remove owner",
            "Exit",
    };

    private final ArrayList<Dog> mainDogList = new ArrayList<>();
    private final ArrayList<Owner> mainOwnerList = new ArrayList<>();
    private final ScannerAdapter commandScanner  = new ScannerAdapter();

    public static void main(String[] args) {
        new DogRegister().run();
    }

    private void run() {
        start();
        startCommandLoop();
        stop();
    }

    private void start() {
        System.out.println("Welcome!");
        printCommands();
    }

    private void stop() {
        System.out.println("Goodbye!");
    }

    private void startCommandLoop() {
        String command;
        boolean isRunning;

        do {
            command = commandScanner.readText("Command");
            String validCommand = checkCommand(command);

            if (validCommand == null) {
                System.out.println("Error: Unknown command");
                printCommands();
                isRunning = true;
            } else if (validCommand.equals("Exit")) {
                isRunning = false;
            } else {
                executeCommand(validCommand);
                isRunning = true;
            }
        } while (isRunning);
    }




    private void printCommands() {
        System.out.println("Enter one of the following commands:");
        for (int i = 0; i < VALID_COMMANDS.length; i++) {
            String command = VALID_COMMANDS[i];
            System.out.println("* " + command);
        }
    }


    private String checkCommand(String unCheckedCommand) {
        String normalizedCommand = unCheckedCommand.trim().toLowerCase();
        for (int i = 0; i < VALID_COMMANDS.length; i++) {
            if (VALID_COMMANDS[i].toLowerCase().equals(normalizedCommand)) {
                return VALID_COMMANDS[i];
            }
        }
        return null;
    }


    private void executeCommand(String validCommand) {
        switch (validCommand) {
            case "Register new dog" -> registerNewDog();
            case "List dogs" -> listDogs();
            case "Increase age" -> increaseDogAge();
            case "Remove dog" -> removeDogMethod();
            case "Register new owner" -> newOwner();
            case "Give dog" -> giveDog();
            case "List owners" -> listOwners();
            case "Remove owned dog" -> removeOwnedDog();
            case "Remove owner" -> removeOwnerMethod();
        }
    }


    private void registerNewDog() {
        String breed;
        String name;
        int age;
        int weight;


        do {
            name = commandScanner .readText("Enter name of dog");
            if (name.isBlank()) {
                System.out.println("Error, can't input empty name.");
            }
        } while (name.isBlank());

        do {
            breed = commandScanner .readText("Enter breed of dog");
            if (breed.isBlank()) {
                System.out.println("Error, can't input empty breed.");
            }
        } while (breed.isBlank());

        age = commandScanner .readInteger("Enter age of dog");


        weight = commandScanner .readInteger("Enter weight of dog");

        Dog dog = new Dog(name.trim(), breed.trim(), age, weight);
        mainDogList.add(dog);
    }



    private void listDogs() {
    if (mainDogList.size() == 0) {
        System.out.println("Error: no dogs in register");
        return;
    }

    double minTailLength = commandScanner.readDecimal("Smallest tail length to display");

    // Get dogs with tail length greater than or equal to minTailLength
    Dog[] validDogs = getDogsByTailLength(minTailLength);

    // Sort the valid dogs array using the sortDogs() method
    int numSwaps = sortDogs(validDogs);

    if (validDogs.length == 0) {
        System.out.println("Error: no dog have a tail that long");
    } else {
        for (Dog dog : validDogs) {
            if (dog.getOwner() == null) {
                System.out.println(dog);
            }
            if (dog.getOwner() != null) {
                System.out.println(dog + "\n" + dog.getOwner());
            }
        }
    }
    System.out.println("Sorted " + validDogs.length + " dogs with " + numSwaps + " swaps.");
    }

    private int sortDogs(Dog[] dogs) {
        int numSwaps = 0;
        for (int i = 0; i < dogs.length - 1; i++) {
            int smallestDogIndex = i;
            for (int j = i + 1; j < dogs.length; j++) {
                if (dogs[j].compareTo(dogs[smallestDogIndex]) < 0) {
                smallestDogIndex = j;
            }
        }
        if (smallestDogIndex != i) {
            swapDogs(dogs, smallestDogIndex, i);
            numSwaps++;
        }
    }
    return numSwaps;
    }

    private void swapDogs(Dog[] dogs, int i, int j) {
        Dog temp = dogs[i];
        dogs[i] = dogs[j];
        dogs[j] = temp;
    }



    private void increaseDogAge() {
        String name = commandScanner .readText("Ange hundens namn");
        Dog dog = getDogByName(name);
        if (dog != null) {
            dog.increaseAge();
            System.out.println(dog.getName() + " är nu äldre.");
        } else {
            System.out.println("Error, Hunden kunde inte hittas.");
        }
    }

    private void removeDogMethod() {
        String name = commandScanner .readText("Enter the name of the dog:").trim();
        Dog dog = getDogByName(name);
        if (dog != null) {
            mainDogList.remove(dog);
            dog.removeOwner();
            System.out.println(name.concat(" is removed from the register"));
        } else {
            System.out.println("Error: no such dog");
        }
    }
    private void newOwner(){
        String name;

        do {
            name = commandScanner .readText("Enter name of dog");
            if (name.isBlank()) {
                System.out.println("Error, can't input empty name.");
            }
        } while (name.isBlank());

        Owner owner = new Owner(name.trim());
        mainOwnerList.add(owner);
    }

    private void giveDog() {
        String dogName = commandScanner .readText("Enter the name of the dog:").trim();
        Dog dog = getDogByName(dogName);
        if (dog == null) {
            System.out.println("Error: Dog not found!");
            return;
        }

        if (dog.getOwner() != null) {
            System.out.println("Error: The dog already has an owner");
            return;
        }

        String ownerName = "";
        do {
            ownerName = commandScanner .readText("Enter the name of the owner:").trim();
            if (ownerName.isEmpty()) {
                System.out.println("Error: Please enter a valid owner name!");
            }
        } while (ownerName.isEmpty());

        Owner owner = getOwnerByName(ownerName);
        if (owner == null) {
            System.out.println("Error: Owner not found!");
            return;
        }

        owner.addDogtoOwner(dog);
        System.out.println(ownerName + " now owns " + dogName);
    }

    private void listOwners() {
        if (mainOwnerList.size() == 0) {
            System.out.println("Error: No owners in list.");
            return;
        }

        for (Owner owner : mainOwnerList) {
            boolean hasDog = false;
            for (Dog dog : mainDogList) {
                if (owner == dog.getOwner()) {
                    System.out.println("Owner: " + owner.getName() + " Dog: " + dog.getName());
                    hasDog = true;
                }
            }
            if (!hasDog) {
                System.out.println("Owner: " + owner.getName());
            }
        }
    }

    private void removeOwnedDog() {
        String dogsName = commandScanner .readText("Enter the name of the dog:").trim();
        Dog dog = getDogByName(dogsName);
        if (dog == null) {
            System.out.println("Error: Dog not found!");
            return;
        }

        if (dog.getOwner() == null) {
            System.out.println("Error: The dog is not owned by anyone.");
            return;
        }

        String ownerName = dog.getOwner().getName();
        dog.removeOwner();
        System.out.println(dogsName + "/n" + ownerName);
    }

    private void removeOwnerMethod() {
        String ownerName = commandScanner .readText("Enter the name of the owner:").trim();

        Owner owner = getOwnerByName(ownerName);

        if (owner != null) {
            removeDogsOwnedBy(owner);
            mainOwnerList.remove(owner);
            System.out.println(owner.getName() + " has been removed.");
        } else {
            System.out.println("Error: no such owner.");
        }
    }
    private void removeDogsOwnedBy(Owner owner) {
        Iterator<Dog> iter = mainDogList.iterator();

        while (iter.hasNext()) {
            Dog dog = iter.next();

            if (dog.getOwner() != null && dog.getOwner().equals(owner)) {
                iter.remove();
            }
        }
    }

    private Dog[] getDogsByTailLength(double minTailLength) {
        ArrayList<Dog> result = new ArrayList<>();
        for (int i = 0; i < mainDogList.size(); i++) {
            Dog dog = mainDogList.get(i);
            if (dog.getTailLength() >= minTailLength) {
                result.add(dog);
            }
        }
        return result.toArray(new Dog[result.size()]);
    }

    private Dog getDogByName(String name) {
        for (int i = 0; i < mainDogList.size(); i++) {
            Dog dog = mainDogList.get(i);
            if (dog.getName().equalsIgnoreCase(name)) {
                return dog;
            }
        }
        return null;
    }
    private Owner getOwnerByName(String name) {
        for (int i = 0; i < mainOwnerList.size(); i++) {
            Owner owner = mainOwnerList.get(i);
            if (owner.getName().equalsIgnoreCase(name)) {
                return owner;
            }
        }
        return null;
    }
}
Editor is loading...