-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created Planet Express using Java #10
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package planetExpress; | ||
import java.util.Scanner; | ||
public class Game { | ||
|
||
public static void main(String[] args) | ||
{ | ||
PlanetExpress planet; | ||
Score score; | ||
Boolean stable = true; | ||
|
||
planet = new PlanetExpress(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are not going to use the undeclared variable anywhere, I would recommend doing the declaration ( |
||
score = new Score(); | ||
Scanner reader = new Scanner(System.in); | ||
|
||
|
||
while (stable = true) | ||
{ | ||
System.out.println("Choose what do you want to do next:"); | ||
System.out.println("press \"1\" if you want to deliver"); | ||
System.out.println("press \"2\" if you want to steal"); | ||
System.out.println("press \"3\" if you want to eat"); | ||
System.out.println("press \"4\" if you want to drink"); | ||
System.out.println("press \"5\" if you want to account"); | ||
int step = reader.nextInt(); | ||
|
||
switch(step) | ||
{ | ||
case '1' : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the value is an integer, I don't think you need |
||
planet.deliver(); | ||
case '2' : | ||
planet.steal(); | ||
case '3' : | ||
planet.eat(); | ||
case '4' : | ||
planet.drink(); | ||
case '5' : | ||
planet.account(); | ||
default : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! You always want to use a default in your case statements |
||
System.out.println("Invalid number"); | ||
} | ||
|
||
score.setScore(planet.getThirst(), planet.getWork(), planet.getHorde(), planet.getHunger(), planet.getReceipts()); | ||
if (planet.check() == false) | ||
{ | ||
stable = false; | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package planetExpress; | ||
|
||
public class PlanetExpress { | ||
|
||
private int thirst; | ||
private int work; | ||
private int horde; | ||
private int hunger; | ||
private int receipts; | ||
private boolean stable; | ||
|
||
public PlanetExpress() { | ||
thirst = 50; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a constant for these values |
||
work = 50; | ||
horde = 50; | ||
hunger = 50; | ||
receipts = 50; | ||
stable = true; | ||
} | ||
|
||
public int getThirst() | ||
{ | ||
return thirst; | ||
} | ||
|
||
public int getWork() | ||
{ | ||
return thirst; | ||
} | ||
|
||
public int getHorde() | ||
{ | ||
return thirst; | ||
} | ||
|
||
public int getHunger() | ||
{ | ||
return thirst; | ||
} | ||
|
||
public int getReceipts() | ||
{ | ||
return thirst; | ||
} | ||
|
||
public void drink() | ||
{ | ||
thirst -= 10; | ||
work += 10; | ||
} | ||
|
||
public void deliver() | ||
{ | ||
work -= 10; | ||
receipts += 10; | ||
} | ||
|
||
public void steal() | ||
{ | ||
horde += 10; | ||
work += 10; | ||
} | ||
|
||
public void eat() | ||
{ | ||
hunger -= 10; | ||
work += 10; | ||
} | ||
|
||
public void account() | ||
{ | ||
receipts -= 10; | ||
horde -= 10; | ||
} | ||
|
||
public boolean check() | ||
{ | ||
if ((this.thirst > 80) || (this.work > 80) || (this.hunger > 80) || (this.horde < 20) || (this.receipts < 20)) | ||
{ | ||
this.stable = false; | ||
return false; | ||
} | ||
else | ||
{ | ||
if(this.thirst > 70) | ||
{ | ||
System.out.println("You should drink more"); | ||
} | ||
if(this.work > 70) | ||
{ | ||
System.out.println("You have too much work to do"); | ||
} | ||
if(this.hunger > 70) | ||
{ | ||
System.out.println("You need to eat something"); | ||
} | ||
if(this.horde < 30) | ||
{ | ||
System.out.println("The horde is too low"); | ||
} | ||
if(this.receipts < 30) | ||
{ | ||
System.out.println("The receipts are too low"); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package planetExpress; | ||
|
||
public class Score { | ||
float score; | ||
|
||
public Score() { | ||
score = 0; | ||
} | ||
|
||
private float checkLow(int num) | ||
{ | ||
return (((100 - num) * 2) / 10); | ||
} | ||
|
||
private float checkHigh(int num) | ||
{ | ||
return ((num * 2) / 10); | ||
} | ||
|
||
public float setScore (int thirst, int work, int horde, int hunger, int receipts) | ||
{ | ||
this.score = checkLow(thirst) + checkLow(work) + checkLow(hunger) + checkHigh(horde) + checkHigh(receipts); | ||
return this.score; | ||
} | ||
|
||
public float getScore () | ||
{ | ||
return this.score; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first I wasn't sure if I like the
Score
class in theGame
, or if it would make more sense to be in thePlanetExpress
class. I think that after some thought this makes sense to be in theGame
because then you could have a separatePlanetExpress
instance that wasn't so much a game, but just a separate class.