Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Robin Geijer roge8388
- //32. 32? 33.
- import java.util.Scanner;
- import java.util.ArrayList;
- public class DogProgram {
- private String name;
- private String breed;
- private int age;
- private int weight;
- private ArrayList<Dog> dogList = new ArrayList<Dog>();
- private Scanner input = new Scanner(System.in);
- public void initialize() {
- System.out.println("Welcome! Enter one of the following commands:");
- System.out.println("- Register new dog");
- System.out.println("- Increase age");
- System.out.println("- List dogs");
- System.out.println("- Remove dog");
- System.out.println("- Exit");
- }
- public void runCommandLoop(){
- boolean ended;
- do {
- String command = readCommand();
- ended = handleCommand(command);
- } while(!ended);
- }
- public String readCommand(){
- System.out.print("Command: ");
- String command = input.nextLine();
- return command;
- }
- public boolean handleInput(String userInput){
- boolean isFilled = false;
- if(!userInput.isEmpty()){
- isFilled = true;
- }
- return isFilled;
- }
- public void registerNewDog() {
- System.out.println("You've entered the command \"Register new dog.\"");
- String userInput = ""; //Kommer lagra det användaren matar in
- //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
- System.out.println("Name: ");
- userInput = input.nextLine(); //Ber användaren knappa in name/breed/age/weight och lagrar i en STRING
- while(!handleInput(userInput.trim())){ //Så länge som handleInput returnerar false
- System.out.println("Error: the name can’t be empty");
- System.out.println("Name:"); //Fråga efter namn/bla/bla
- userInput = input.nextLine(); //Skriv in
- } //Om den lämnar while-loopen så betyder det att handleInput = true
- name = userInput; //Lagra det användaren skrev i name eller vad det nu är
- System.out.println("Breed: ");
- userInput = input.nextLine();
- while(!handleInput(userInput.trim())){
- System.out.println("Error: the breed can’t be empty");
- System.out.println("Breed:");
- userInput = input.nextLine();
- }
- breed = userInput;
- System.out.println("Age: ");
- userInput = input.nextLine();
- while(!handleInput(userInput.trim()) ){
- System.out.println("Error: the age can’t be empty and has to be numbers");
- System.out.println("Age:");
- userInput = input.nextLine();
- }
- age = Integer.parseInt(userInput); //Konverterar om String till int då det användaren matar in lagras som string och age/weight är int
- System.out.println("Weight: ");
- userInput = input.nextLine();
- while(!handleInput(userInput.trim())){
- System.out.println("Error: the weight can’t be empty and has to be numbers");
- System.out.println("Weight:");
- userInput = input.nextLine();
- }
- weight = Integer.parseInt(userInput);
- Dog newDog = new Dog(name, breed, age, weight);
- dogList.add(newDog);
- }
- private boolean handleCommand(String command){
- switch (command.toUpperCase()) {
- case "EXIT":
- return true;
- case "REGISTER NEW DOG":
- registerNewDog();
- break;
- case "INCREASE AGE":
- changeAge();
- break;
- case "LIST DOGS":
- listAllDogs();
- break;
- case "REMOVE DOG":
- removeDog();
- break;
- default:
- System.out.println("Error: No such command.");
- }
- return false;
- }
- public void changeAge() {
- System.out.println("You've entered the command \"Increase age.\"");
- System.out.println("Enter the name of the dog that has aged.");
- name = input.nextLine().trim();
- int nameMatches = 0;
- for (Dog dog : dogList) {
- if (name.equalsIgnoreCase(dog.getName())) {
- dog.increaseAge(1);
- nameMatches++;
- System.out.println("The dog's age has been updated.");
- }
- }
- if (nameMatches == 0) {
- System.out.println("Error: Dog not found in register.");
- }
- }
- public void listAllDogs() {
- System.out.println("You've entered the command \"List dogs.\"");
- System.out.println("Enter the minimum tail length to search for.");
- double minLength = input.nextDouble();
- input.nextLine();
- if (minLength == 0) {
- for (Dog dog : dogList) {
- dog.getTailLength();
- System.out.println(dog);
- }
- }
- else {
- for (Dog dog : dogList) {
- if (dog.getTailLength() >= minLength) {
- System.out.println(dog);
- }
- }
- }
- }
- public void removeDog() {
- System.out.println("You gave the command \"remove dog\"");
- System.out.println("Please enter name: ");
- name = input.nextLine().trim();
- int matchingDogs = 0;
- for (Dog dog : dogList) {
- if(name.equalsIgnoreCase(dog.getName())) {
- dog.toString();
- dogList.remove(dog);
- System.out.println(name+ " has been removed from register");
- matchingDogs++;
- break;
- }
- }
- if (matchingDogs==0) {
- System.out.println("Error: dog with given name could not be found");
- }
- }
- public void exitProgram(){
- System.out.println("Bye");
- }
- private void run(){
- initialize();
- runCommandLoop();
- exitProgram();
- }
- public static void main(String[] args){
- new DogProgram().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement