diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fbd5ba5 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ea0b3a9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/docs/hamurabi.java b/docs/hamurabi.java index 9947af3..7dadc73 100644 --- a/docs/hamurabi.java +++ b/docs/hamurabi.java @@ -1,3 +1,4 @@ +/* import java.io.IOException; import java.util.Scanner; @@ -153,4 +154,5 @@ public static void main(String[] args) throws IOException { a.newYear(); a.finished(); } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..08c1865 --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + org.hamurabiGLJ + hamurabi + 1.0-SNAPSHOT + + + junit + junit + 4.13.1 + test + + + + + 18 + 18 + + + \ No newline at end of file diff --git a/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java new file mode 100644 index 0000000..ae6ca9d --- /dev/null +++ b/src/main/java/org/hamurabiGLJ/hamurabi/Hammurabi.java @@ -0,0 +1,128 @@ +package org.hamurabiGLJ.hamurabi; + +import java.util.*; + +public class Hammurabi { + Random rand = new Random(); // this is an instance variable + Scanner scanner = new Scanner(System.in); + private int land; + + + public static void main(String[] args) { // required in every Java program + new Hammurabi().playGame(); + } + + void playGame() { + // declare local variables here: grain, population, etc. + Integer grain = 2800; + Integer population = 100; + Integer land = 1000; + Integer landValue = 19; + + // statements go after the declarations + } + + int getNumber(String message) { + while (true) { + System.out.print(message); + try { + return scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("\"" + scanner.next() + "\" isn't a number!"); + } + } + } + // Jiayong + int askHowManyAcresToBuy(int price, int bushels) { + int maxAcres = bushels / price; + String msg = "How many acres of land would you like to buy?"; + int input = getNumber(msg); + while (input > maxAcres) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", + input * price, bushels); + System.out.println(newMsg); + input = getNumber(msg); + } + return input; + } + // Linda + int askHowManyAcresToSell(int acresOwned) { + int acresToSell = getNumber("How many acres of land would you like to sell?\n"); + while (acresToSell > acresOwned) { + System.out.println("O Hammurabi, but you only have " + land + " acres of land!\n"); + acresToSell = getNumber("How many acres of land would you like to sell?\n"); + } + return acresToSell; + } + // Gychu + int askHowMuchGrainToFeedPeople(int bushels) { + String msg = "How many bushels of grain do you want to feed your people?\n"; + int input = getNumber(msg); + while (input > bushels) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", + input, bushels); + System.out.println(newMsg); + input = getNumber(msg); + } + return input; + } + + int askHowManyAcresToPlant(int acresOwned, int population, int bushels) { + String msg = "How many acres do you wish to plant?\n"; + int input = getNumber(msg); + + while (true) { + if (input > acresOwned) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d acres but we only have %!", input, acresOwned); + System.out.println(newMsg); + input = getNumber(msg); + continue; + } + if (input / 10 > population) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d people but we only have %d!", input/10, population); + System.out.println(newMsg); + input = getNumber(msg); + continue; + } + if (input > bushels / 2) { + String newMsg = String.format("O Great Hammurabi, surely you jest! We'd need %d bushels but we only have %d!", input*2, bushels); + System.out.println(newMsg); + input = getNumber(msg); + continue; + } + return input; + } + } + + int plagueDeaths(int population) { + if (rand.nextInt(101) <= 15) { + return population / 2; + } + return 0; + } + + int starvationDeaths(int population, int bushelsFedToPeople) { + return 0; + } + + boolean uprising(int population, int howManyPeopleStarved) { + return (((double)howManyPeopleStarved / population) >= 0.45); + } + + int immigrants(int population, int acresOwned, int grainInStorage) { + return (20 * acresOwned + grainInStorage) / (100 * population) + 1; + } + + int harvest(int acres) { + int yield = rand.nextInt(1,7); + return acres * yield; + } + + int grainEatenByRats(int bushels) { + return 0; + } + + int newCostOfLand() { + return rand.nextInt(17,24); + } +} diff --git a/HammurabiTest.java b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java similarity index 98% rename from HammurabiTest.java rename to src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java index 5cf1cc9..278b653 100644 --- a/HammurabiTest.java +++ b/src/test/java/org.hamurabiGLJ.hamurabi/HammurabiTest.java @@ -1,4 +1,4 @@ -package hammurabi; +package org.hamurabiGLJ.hamurabi; import static org.junit.Assert.*; @@ -42,7 +42,7 @@ public final void testPlagueDeaths2() { assertEquals("In a plague, " + deaths + "% of your people die, not 50%.", 50, deaths); } - + @Test public final void testStarvationDeaths() { int deaths = ham.starvationDeaths(100, 1639); @@ -88,7 +88,7 @@ public final void testGrainEatenByRats1() { } } int percentInfestations = infestations / 100; - assertTrue("Number of rat infestations is about " + percentInfestations + + assertTrue("Number of rat infestations is about " + percentInfestations + ", not about 40%.", about(400, infestations)); }