Created
February 18, 2021 01:35
-
-
Save trikitrok/efe2130dc04d7e5d55af0be57996adc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class DogHouse { | |
public List<Dog> dogs = new ArrayList<>(); | |
public String deposit(String userName, Dog dog) { | |
String validationMessage = ""; | |
if (dog.getAge() < 1) { | |
validationMessage = "The dog its to small to be deposit. Come back when the dog were older than 1 year."; | |
} else if (dog.getAge() > 30) { | |
validationMessage = "WTF this not suppouse to be alive???. I will call the Zoo"; | |
} else if (dog.getAge() > 10) { | |
validationMessage = "The dog its to older to be deposit. Dont come back."; | |
} else if (dog.getName().contains("People eater")) { | |
validationMessage = "The dog its to danguerous to be deposit. Dont come back."; | |
} else if (this.dogs.size() >= 100) { | |
validationMessage = "We are full :(. Why dont you adopt one"; | |
} else { | |
//All ok! the dog its fine | |
} | |
if (!validationMessage.equals("")) { | |
//something went wrong with this dog | |
return validationMessage; | |
} | |
double price = this.calculate(dog); | |
this.setIdToDog(dog); | |
//Put the dog into its cage | |
this.dogs.add(dog); | |
String ticket = "------------------------------------------------------------------------------------" + "\n"; | |
ticket += String.format("Dear %s this is the ticket conforming you are Depositing a dog to us.", userName) + "\n"; | |
ticket += String.format("The dog its %s", dog.desc()) + "\n"; | |
ticket += String.format("You must pay: %f on the cash register", price) + "\n"; | |
ticket += "------------------------------------------------------------------------------------" + "\n"; | |
return ticket; | |
} | |
public String adopt(String userName, DogBreed raceEnum) { | |
String validationMessage = ""; | |
if (null == dogs.stream().filter(d -> d.getBreed().equals(raceEnum)).findFirst().orElse(null)) | |
{ | |
validationMessage = "We dont have the dog you want"; | |
} | |
else if (userName.contains("Dog killer")) { | |
validationMessage = "I will call Police"; | |
} | |
if (!validationMessage.equals("")) { | |
//something went wrong with this dog | |
return validationMessage; | |
} | |
Dog dog = dogs.stream().filter(d -> d.getBreed().equals(raceEnum)).findFirst().orElse(null); | |
double price = this.calculate2(dog); | |
String ticket = "------------------------------------------------------------------------------------" + "\n"; | |
ticket += String.format("Dear %s this is the ticket conforming you are Adopting a dog to us.", userName) + "\n"; | |
ticket += String.format("The dog its %s", dog.desc()) + "\n"; | |
ticket += String.format("You must pay: %f on the cash register", price) + "\n"; | |
ticket += "------------------------------------------------------------------------------------" + "\n"; | |
return ticket; | |
} | |
//calculate the cost of deposit a Dog | |
private double calculate(Dog dog) { | |
double depositPriceDouble; | |
switch (dog.getBreed()) { | |
//Big dogs are expensive to mantain | |
case Boxer: | |
case Bulldog: | |
case Rotweiler: | |
case GermanShepherd: | |
double bigDogPrice = (10 * dog.getAge()) * 2.0; //2.0 its the IVA | |
depositPriceDouble = bigDogPrice; | |
break; | |
case Chihuahua: | |
double smallDogPrice = (10 * dog.getAge()) * 2.0; | |
depositPriceDouble = smallDogPrice; | |
break; | |
default: | |
double normalDogPrice = (10 * dog.getAge()) * 2.0; | |
depositPriceDouble = normalDogPrice; | |
break; | |
} | |
//return the price | |
return depositPriceDouble; | |
} | |
//calculate the cost of adopt a Dog | |
private double calculate2(Dog dog) { | |
double depositPriceDouble; | |
switch (dog.getBreed()) { | |
//Its ugly and noisy its free | |
case Chihuahua: | |
depositPriceDouble = 0; | |
break; | |
default: | |
double normalDogPrice = dog.howLongInDogHouse() * 2.0; //2.0 its the IVA | |
depositPriceDouble = normalDogPrice; | |
break; | |
} | |
//return the price | |
return depositPriceDouble; | |
} | |
private void setIdToDog(Dog dog) { | |
dog.setDogHouseId(randomString()); | |
//if we are setting Id its because we are going to deposit so we set flag to true. | |
dog.setInDogHouse(true); | |
dog.setDepositAt(LocalDate.now()); | |
//this.renameDog(dog); | |
} | |
private void renameDog(Dog dog) { | |
dog.setName(dog.getName() + " is on deposit now :(") ; | |
} | |
private String randomString() { | |
int leftLimit = 48; // numeral '0' | |
int rightLimit = 122; // letter 'z' | |
int targetStringLength = 10; | |
Random random = new Random(); | |
return random.ints(leftLimit, rightLimit + 1) | |
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)) | |
.limit(targetStringLength) | |
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) | |
.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment