Adding back the old files

This commit is contained in:
2019-03-12 08:45:44 -06:00
parent ae39a210be
commit c24d485463
10 changed files with 2416 additions and 0 deletions

13
README.md Normal file
View File

@@ -0,0 +1,13 @@
# TaipanClone
Computer Science 233 project, Winter 2019
How to run:
If you are using intellij, extract "TaipanClone-master.zip", and open the "TaipanClone-master" folder in intellij. Also set up the SDK. Then, run main.java.
If you are using the command line, extract "TaipanClone-master.zip", and open the "TaipanClone-master" folder. Open your terminal and change its directory to the "src" folder within "TaipanClone-master" folder. Then, type in "javac *.java", this compiles all the necessary files. Now, run main.java using "java main".
Additional information:
For input, the program usually takes the first letter of whichever option you need to select. Example:
What would you like to buy? Valid inputs are "O" (for Opium), "S" (for Silk), "A" (for Arms), "G" (for General cargo).
You lose if your HP reaches 0. You can win if you "retire" in Hong Kong while having a net worth of over $1 million.

102
src/Bank.java Normal file
View File

@@ -0,0 +1,102 @@
import java.util.Scanner;
public class Bank{
private Player player;
/**
* setter method that takes in a Player object as an argument.
*
* @param player object of the class Player
*/
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* getter method for obtaining a player object.
*
* @return returns player object
*/
public Player getPlayer(){
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* Class Constructor that takes in a type player as a parameter
*
* @param player object of the class Player
*/
public Bank(Player player){
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* This method is used to withdraw or deposit money into the bank account
* by prompting the user if they would like to withdraw or deposit. Followed
* by prompting them to enter an amount to transfer. This method also uses the
* player class to see if the transfer can be made,and if it can it changes the
* values accordingly
*/
public void bank(){
Scanner input = new Scanner(System.in);
boolean notDone = true;
int check = 0;
while(notDone){
//Prompt the user if they want to withdraw or deposit
System.out.println("Would you like to Withdraw or Deposit?");
String response = input.nextLine();
//If user chose withdraw then subtract the amount from bank account and add it to cash
if(response.equalsIgnoreCase("W")){
boolean notDone2 = true;
while(notDone2){
System.out.println("How much do you wish to Withdraw?");
int withdraw = input.nextInt();
//Prompt the user for the amount and check if the bank has sufficient funds
if(withdraw <= player.getBank()){
player.setMoney(withdraw + player.getMoney());
player.setBank(player.getBank()-withdraw);
notDone2 = false;
check = 1;
}
}
}
//If the user chooses to deposit the continue to this code
else if(response.equalsIgnoreCase("D")){
boolean notDone2 = true;
while(notDone2){
//Prompt the user for the amount they would like to deposit and ensure suffiecent funds
System.out.println("How much do you wish to Deposit?");
int deposit = input.nextInt();
if(deposit <= player.getMoney()){
player.setBank(deposit + player.getBank());
player.setMoney(player.getMoney()-deposit);
notDone2 = false;
check = 1;
}
}
}
if(check == 1){
boolean notDone3 = true;
// Asks user if they would like to continue in bank or not
while(notDone3){
System.out.println("Would you like to continue? Y/N");
response = input.nextLine();
response = input.nextLine();
if(response.equalsIgnoreCase("Y")){
notDone3 = false;
}else if(response.equalsIgnoreCase("N")){
notDone = false;
notDone3 = false;
}
}
}
}
}
}

432
src/Player.java Normal file
View File

@@ -0,0 +1,432 @@
import java.util.Scanner;
public class Player {
private String name = "Taipan";
private int bank = 0;
private int money = 0;
private int opiumHeld = 0;
private int silkHeld = 0;
private int generalHeld = 0;
private int armsHeld = 0;
private int location = 1;
private int guns = 0;
private int HP = 100;
private int debt = 0;
private int wOpium = 0;
private int wSilk = 0;
private int wGeneral = 0;
private int wArms = 0;
private boolean retire = false;
private int cargoSpace = 60;
public Player(){
}
/**
* Copy constructor
*
* @param player object of the class Player
*/
public Player(Player player){
this.bank = player.bank;
this.money = player.money;
this.opiumHeld = player.opiumHeld;
this.silkHeld = player.silkHeld;
this.generalHeld = player.generalHeld;
this.armsHeld = player.armsHeld;
this.location = player.location;
this.guns = player.guns;
this.HP = player.HP;
this.debt = player.debt;
this.wOpium = player.wOpium;
this.wSilk = player.wSilk;
this.wGeneral = player.wGeneral;
this.wArms = player.wArms;
}
/**
* getter method for the instance variable cargoSpace.
*
* @return returns the instance variable cargoSpace
*/
public int getCargoSpace() {
return cargoSpace;
}
/**
* setter method for the instance variable cargoSpace.
*
* @param cargoSpace takes an int that is greater than 0 as an argument
*/
public void setCargoSpace(int cargoSpace) {
if(cargoSpace > 0){
this.cargoSpace = cargoSpace;
}
}
/**
* getter method for the instance variable retire.
*
* @return returns the instance variable retire
*/
public boolean getRetire(){
return retire;
}
/**
* setter method for the instance variable retire.
*
* @param retire takes a boolean as an argument
*/
public void setRetire(boolean retire){
if(retire){
this.retire = retire;
}
}
/**
* getter method for the instance variable name.
*
* @return returns the instance variable name
*/
public String getName() {
return name;
}
/**
* setter method for the instance variable name.
*
* @param name takes a string as an argument
*/
public void setName(String name) {
this.name = name;
}
/**
* getter method for the instance variable HP.
*
* @return returns the instance variable HP
*/
public int getHP() {
return HP;
}
/**
* setter method for the instance variable HP.
*
* @param HP takes an int as an argument
*/
public void setHP(int HP) {
this.HP = HP;
}
/**
* getter method for the instance variable bank.
*
* @return returns the instance variable bank
*/
public int getBank() {
return bank;
}
/**
* setter method for the instance variable bank.
*
* @param bank takes an int that is greater than or equal to 0 as an argument
*/
public void setBank(int bank) {
if (bank >= 0) {
this.bank = bank;
}
}
/**
* getter method for the instance variable money.
*
* @return returns the instance variable money
*/
public int getMoney() {
return money;
}
/**
* setter method for the instance variable money.
*
* @param money takes an int that is greater than or equal to 0 as an argument
*/
public void setMoney(int money) {
if (money >= 0) {
this.money = money;
}
}
/**
* getter method for the instance variable opiumHeld.
*
* @return returns the instance variable opiumHeld
*/
public int getOpiumHeld() {
return opiumHeld;
}
/**
* setter method for the instance variable opiumHeld.
*
* @param opiumHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setOpiumHeld(int opiumHeld) {
if (opiumHeld >= 0) {
this.opiumHeld = opiumHeld;
}
}
/**
* getter method for the instance variable silkHeld.
*
* @return returns the instance variable silkHeld
*/
public int getSilkHeld() {
return silkHeld;
}
/**
* setter method for the instance variable silkHeld.
*
* @param silkHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setSilkHeld(int silkHeld) {
if (silkHeld >= 0) {
this.silkHeld = silkHeld;
}
}
/**
* getter method for the instance variable generalHeld.
*
* @return returns the instance variable generalHeld
*/
public int getGeneralHeld() {
return generalHeld;
}
/**
* setter method for the instance variable generalHeld.
*
* @param generalHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setGeneralHeld(int generalHeld) {
if (generalHeld >= 0) {
this.generalHeld = generalHeld;
}
}
/**
* getter method for the instance variable armsHeld.
*
* @return returns the instance variable armsHeld
*/
public int getArmsHeld() {
return armsHeld;
}
/**
* setter method for the instance variable armsHeld.
*
* @param armsHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setArmsHeld(int armsHeld) {
if (armsHeld >= 0) {
this.armsHeld = armsHeld;
}
}
/**
* getter method for the instance variable location.
*
* @return returns the instance variable location
*/
public int getLocation() {
return location;
}
/**
* setter method for the instance variable location.
*
* @param location takes an int that is greater than or equal to 0 as an argument
*/
public void setLocation(int location) {
if (location >= 0) {
this.location = location;
}
}
/**
* getter method for the instance variable guns.
*
* @return returns the instance variable guns
*/
public int getGuns() {
return guns;
}
/**
* setter method for the instance variable guns.
*
* @param guns takes an int that is greater than or equal to 0 as an argument
*/
public void setGuns(int guns) {
if (guns >= 0) {
this.guns = guns;
}
}
/**
* getter method for the instance variable debt.
*
* @return returns the instance variable debt
*/
public int getDebt() {
return debt;
}
/**
* setter method for the instance variable debt.
*
* @param debt takes an int that is greater than or equal to 0 as an argument
*/
public void setDebt(int debt) {
if (debt >= 0) {
this.debt = debt;
}
}
/**
* getter method for the instance variable wOpium.
*
* @return returns the instance variable wOpium
*/
public int getwOpium() {
return wOpium;
}
/**
* setter method for the instance variable wOpium.
*
* @param wOpium takes an int that is greater than or equal to 0 as an argument
*/
public void setwOpium(int wOpium) {
if (wOpium >= 0){
this.wOpium = wOpium;
}
}
/**
* getter method for the instance variable wSilk.
*
* @return returns the instance variable wSilk
*/
public int getwSilk() {
return wSilk;
}
/**
* setter method for the instance variable wSilk.
*
* @param wSilk takes an int that is greater than or equal to 0 as an argument
*/
public void setwSilk(int wSilk) {
if (wSilk >= 0){
this.wSilk = wSilk;
}
}
/**
* getter method for the instance variable wGeneral.
*
* @return returns the instance variable wGeneral
*/
public int getwGeneral() {
return wGeneral;
}
/**
* setter method for the instance variable wGeneral.
*
* @param wGeneral takes an int that is greater than or equal to 0 as an argument
*/
public void setwGeneral(int wGeneral) {
if (wGeneral >= 0){
this.wGeneral = wGeneral;
}
}
/**
* getter method for the instance variable wArms.
*
* @return returns the instance variable wArms
*/
public int getwArms() {
return wArms;
}
/**
* setter method for the instance variable wArms.
*
* @param wArms takes an int that is greater than or equal to 0 as an argument
*/
public void setwArms(int wArms) {
if (wArms >= 0){
this.wArms = wArms;
}
}
/**
* Method to indicate that you have lost the game. If the player has lost, console will be cleared and will only
* show the statement "Game Over". After showing the message the game closes.
*
**/
public void gameOver(){
System.out.flush();
System.out.println("Game over");
System.exit(0);
}
}

466
src/ShipWarfare.java Normal file
View File

@@ -0,0 +1,466 @@
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class ShipWarfare {
private int numOfPeasantShips = 0;
private int numOfLittyShips = 0;
private boolean userAttacks = true;
private int startingPeasantShips = 0;
private int startingLittyShips = 0;
private int howMuchRun = 0;
private String pirateName = "Liu Yen";
private Player player;
/**
* Class Constructor that takes in a type player as a parameter
* @param player object of the class Player
*/
public ShipWarfare(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* setter method for player
* @param player object of the class Player
*/
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* getter method for obtaining a player object.
* @return returns player object
*/
public Player getPlayer() {
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* This fleet is easy to defeat as a maximum of 15 ships can run away each volley, they can not tank hits
* @throws Exception in case of errors due to the delay
*/
public void peasantFleetAttack() throws Exception {
Scanner userResponse = new Scanner(System.in);
setNumOfPeasantShips(numOfShips());
System.out.printf("By Golly! We have $%,d and are being attacked by %d Merchant ships\nCurrently our ship status is %d%%\n", player.getMoney(), numOfPeasantShips, player.getHP());
fightOrRunMessage();
while (true) {
String response = userResponse.nextLine();
if (response.equalsIgnoreCase("f")) {
userAttacks = true;
System.out.println("Ohh, fight ehh?");
delayForSeconds(1);
boolean winOrLose = destroyPeasantShipsOrEscape();
if (winOrLose == true) {
break;
}
} else if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) {
System.out.println("Couldn't run away!");
if (destroyPeasantShipsOrEscape())
break;
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
break;
}
}
}
}
/**
* This fleet is difficult to defeat as a maximum of 10 ships can run away each volley, they can tank hits
* @throws Exception in case of errors due to the delay
*/
public void littyFleetAttack() throws Exception {
Scanner userResponse = new Scanner(System.in);
setNumOfLittyShips(numOfShips());
System.out.printf("By Golly! We have $%,d and are being attacked by %d of %s's ships\nCurrently our ship status is %d%%\n", player.getMoney(), numOfLittyShips, pirateName, player.getHP());
fightOrRunMessage();
while (true) {
String response = userResponse.nextLine();
if (response.equalsIgnoreCase("f")) {
userAttacks = true;
System.out.println("Ohh, fight ehh?");
boolean winOrLose = destroyLittyShipsOrEscape();
if (winOrLose == true) {
break;
}
} else if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) {
System.out.println("Couldn't run away!");
delayForSeconds(1);
if (destroyLittyShipsOrEscape())
break;
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
break;
}
}
}
}
/**
* Asks user if they would like to fight or run against ships
*/
public void fightOrRunMessage() {
System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)\n", player.getGuns());
}
/**
* setter method that takes in an integer as an argument
* @param numOfLittyShips the number of ships to be used in the litty fleet attack
*/
public void setNumOfLittyShips(int numOfLittyShips) {
this.numOfLittyShips = numOfLittyShips;
startingLittyShips = numOfLittyShips;
}
/**
* setter method that takes in an integer as an argument
* @param numOfPeasantShips the number of ships to be used in the peasant fleet attack
*/
public void setNumOfPeasantShips(int numOfPeasantShips) {
this.numOfPeasantShips = numOfPeasantShips;
startingPeasantShips = numOfPeasantShips;
}
/**
* delays for a specific amount of seconds, takes an integer as an argument
* @param num the seconds to delay
* @throws Exception in case of errors due to the delay
*/
public void delayForSeconds(int num) throws Exception {
TimeUnit.SECONDS.sleep(num);
}
/**
* The number of ships that attack is based on the amount of money one has on hand
* @return the number of ships which will attack
*/
public int numOfShips() {
int numOfShipsAttacking = 0;
Random randomValue = new Random();
if (player.getMoney() <= 100000) {
//Minimum one ship will attack, maximum 20
numOfShipsAttacking = randomValue.nextInt(20) + 1;
} else if (player.getMoney() <= 200000) {
//Minimum 30 Ships will attack, maximum 70
numOfShipsAttacking = randomValue.nextInt(40) + 30;
} else if (player.getMoney() <= 500000) {
//Minimum 50 ships will attack, maximum 140
numOfShipsAttacking = randomValue.nextInt(90) + 50;
} else if (player.getMoney() > 1000000) {
//Minimum 100 ships will attack, maximum 300 ships
numOfShipsAttacking = randomValue.nextInt(3) + 100;
}
return numOfShipsAttacking;
}
/**
* One in two chance of running away
* @return true if the user is allowed to run, false if not, the "default" is false
*/
public boolean runFromShips() {
userAttacks = false;
Random randomValue = new Random();
int runSuccessChance = randomValue.nextInt(2) + 1;
if (runSuccessChance == 2) {
return true;
} else if (runSuccessChance == 1) {
return false;
}
return false;
}
/**
* The user faces off against the litty ships and either prevails, dies, or runs away
* The loot for defeating a litty fleet is much higher than that of a peasant one
* @return true if the user wins, loses, or flees, it returns false otherwise
* @throws Exception in case of errors due to the delay
*/
public boolean destroyLittyShipsOrEscape() throws Exception {
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
Scanner userInput = new Scanner(System.in);
Random randomValue = new Random();
int exitValue = 0;
//Player volley
while (exitValue == 0) {
if (player.getGuns() > 0) {
for (int j = 0; j < player.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(3) + 1;
if (hitOrMiss == 1) {
numOfLittyShips--;
if (numOfLittyShips <= 0) {
exitValue = 1;
break;
}
System.out.println("Got eem");
delayForSeconds(1);
} else if (hitOrMiss == 2) {
System.out.printf("ARRG! We missed %s\n", player.getName());
delayForSeconds(1);
} else {
System.out.println("Darn! Their fleet tanked our attack");
delayForSeconds(1);
}
} else {
continue;
}
}
} else {
System.out.printf("%s! We don't have any GUNS!!!!\n",player.getName());
delayForSeconds(1);
}
if (numOfLittyShips <= 0) {
exitValue = 1;
break;
}
if (player.getGuns() > 0) {
if (chanceOfEnemyRun == 2) {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
howMuchRun = randomValue.nextInt(10) + 1;
if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
setNumOfLittyShips(numOfLittyShips - howMuchRun);
if (userAttacks == true) {
System.out.printf("Cowards! %d ships ran away %s!\n", howMuchRun, player.getName());
} else {
System.out.printf("Escaped %d of them!\n", howMuchRun);
}
}
}
}
System.out.printf("%d ships remaining\n", numOfLittyShips);
System.out.println("Oh no, they are taking the offensive!");
delayForSeconds(1);
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
System.out.println("Dang it! They destroyed one of our guns");
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(15)));
}
if (player.getHP() <= 0) {
exitValue = 2;
break;
}
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
delayForSeconds(1);
if (userAttacks == false) {
userAttacks = true;
}
System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)\n", player.getGuns());
String response = userInput.nextLine();
if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) {
System.out.println("Couldn't run away");
delayForSeconds(1);
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
break;
}
}
}
if (exitValue == 1) {
System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", player.getHP());
delayForSeconds(1);
calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300;
player.setMoney(player.getMoney() + calculateLoot);
System.out.printf("We got $%,d!\n", calculateLoot);
delayForSeconds(2);
return true;
} else if (exitValue == 2) {
player.gameOver();
return true;
} else if (exitValue == 3) {
System.out.printf("We made it out at %d%% ship status!\n", player.getHP());
delayForSeconds(2);
return true;
}
return false;
}
/**
* The user faces off against the peasant ships and either prevails, dies, or runs away
* @return true if the user wins, loses, or flees, it returns false otherwise
* @throws Exception in case of errors due to the delay
*/
public boolean destroyPeasantShipsOrEscape() throws Exception {
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
Scanner userInput = new Scanner(System.in);
Random randomValue = new Random();
int exitValue = 0;
//Player volley
while (exitValue == 0) {
if (player.getGuns() > 0) {
for (int j = 0; j < player.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) {
numOfPeasantShips--;
if (numOfPeasantShips <= 0) {
exitValue = 1;
break;
}
System.out.println("Got eem");
delayForSeconds(1);
} else {
System.out.printf("ARRG! We missed %s\n", player.getName());
delayForSeconds(1);
}
} else {
continue;
}
}
}
else{
System.out.printf("%s! We don't have any GUNS!!!!\n", player.getName());
delayForSeconds(1);
}
if (numOfPeasantShips <= 0) {
exitValue = 1;
break;
}
if (player.getGuns() > 0) {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
if (howMuchRun != 0 && howMuchRun < numOfPeasantShips) {
setNumOfPeasantShips(numOfPeasantShips - howMuchRun);
if (userAttacks == true) {
System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, player.getName());
} else {
System.out.printf("Escaped %d of them!\n", howMuchRun);
}
}
}
}
System.out.printf("%d ships remaining\n", numOfPeasantShips);
delayForSeconds(1);
System.out.println("Oh no, they are taking the offensive!");
delayForSeconds(1);
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
System.out.println("Dang it! They destroyed one of our guns");
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (player.getHP() <= 0) {
exitValue = 2;
break;
}
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
delayForSeconds(1);
if (userAttacks == false) {
userAttacks = true;
}
System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)\n", player.getGuns());
String response = userInput.nextLine();
if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) {
System.out.println("Couldn't run away");
} else {
exitValue = 3;
break;
}
}
}
if (exitValue == 1) {
System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", player.getHP());
delayForSeconds(1);
calculateLoot = (randomValue.nextInt(startingPeasantShips) + startingPeasantShips) * 100;
player.setMoney(player.getMoney() + calculateLoot);
System.out.printf("We got $%,d!", calculateLoot);
delayForSeconds(2);
return true;
} else if (exitValue == 2) {
player.gameOver();
return true;
} else if (exitValue == 3) {
System.out.printf("We made it out at %d%% ship status!\n", player.getHP());
delayForSeconds(2);
return true;
}
return false;
}
}

79
src/Start.java Normal file
View File

@@ -0,0 +1,79 @@
import java.util.Scanner;
public class Start
{
private Player player;
/**
* gets the player instance variable. The method returns a copy of the instance variable for encapsulation purposes.
*
* @return playerDummy -- playerDummy is a copy of the player instance variable.
*/
public Player getPlayer() {
Player playerTemp = new Player(player);
return playerTemp;
}
/**
* sets the player instance variable equal to a copy of the parameter -- a copy is used for encapsulation purposes.
*
* @param player is a Player object that will replace the current instance of the player instance variable.
*/
public void setPlayer(Player player) {
Player playerTemp = new Player(player);
this.player = playerTemp;
}
/**
* Asks the user to input the name that they would like to be called in the game
*
* @param name the name that you would like to be called in the game
*/
public void setFirm (String name) {
if (name.length() <= 22) {
player.setName(name);
}
}
/**
* Initializes the game by asking for your name and if you would like to start with either: 1) money and a debt or
* 2) guns and no cash/debt.
*/
public void initialize()
{
Scanner userInput = new Scanner(System.in);
System.out.println("Taipan, \nWhat will you name your firm:");
setFirm(userInput.nextLine());
System.out.println("Do you want to start . . .\n\t1) With cash (and a debt)\n\t\t\t>> or <<\n\t" +
"2) With five guns and no cash (But no debt!)?\n ");
int input = userInput.nextInt();
if (input == 1)
{
player.setMoney(400);
player.setDebt(5000);
}
if (input == 2)
{
player.setGuns(5);
}
// purely for testing purposes.
if(player.getName().equalsIgnoreCase("Vikram")){
player.setMoney(999999999);
player.setBank(999999999);
player.setGuns(999);
player.setHP(99999999);
player.setCargoSpace(99999999);
}
}
/**
* Copy constructor.
* @param player object of the class Player
*/
public Start(Player player)
{
Player playerTemp = new Player(player);
this.player = playerTemp;
}
}

726
src/TaipanShop.java Normal file
View File

@@ -0,0 +1,726 @@
import java.util.Random;
import java.util.Scanner;
public class TaipanShop {
private Player player;
private int opiumPrice = 16000;
private int silkPrice = 1600;
private int armsPrice = 160;
private int generalPrice = 8;
/**
* This method is evoked if the user is eligible to win, and chooses to end the game (by winning).
*/
public void retire(){
player.setRetire(true);
System.out.println("You win!");
System.exit(0);
}
/**
* sets the player instance variable equal to a copy of the parameter -- a copy is used for encapsulation purposes.
*
* @param player is a Player object that will replace the current instance of the player instance variable.
*/
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* gets the player instance variable. The method returns a copy of the instance variable for encapsulation purposes.
*
* @return playerDummy -- playerDummy is a copy of the player instance variable.
*/
public Player getPlayer(){
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* constructor; only runs when a Player object is provided. The constructor is fully encapsulated.
*
* @param player is a Player object that will be copied and the player instance variable is set to the copy.
*/
public TaipanShop(Player player){
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* getter for opiumPrice instance variable.
*
* @return opiumPrice -- the price of opium in the shop
*/
public int getOpiumPrice() {
return opiumPrice;
}
/**
* setter for the opiumPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param opiumPrice -- what the instance variable opiumPrice should be changed to.
*/
public void setOpiumPrice(int opiumPrice) {
if(opiumPrice > 0){
this.opiumPrice = opiumPrice;
}
}
/**
* getter for silkPrice instance variable.
*
* @return silkPrice -- the price of silk in the shop.
*/
public int getSilkPrice() {
return silkPrice;
}
/**
* setter for the silkPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param silkPrice -- what the instance variable silkPrice should be changed to.
*/
public void setSilkPrice(int silkPrice) {
if(silkPrice > 0){
this.silkPrice = silkPrice;
}
}
/**
* getter for armsPrice instance variable.
*
* @return armsPrice -- the price of arms in the shop.
*/
public int getArmsPrice() {
return armsPrice;
}
/**
* setter for the armsPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param armsPrice -- what the instance variable armsPrice should be changed to.
*/
public void setArmsPrice(int armsPrice) {
if(armsPrice > 0){
this.armsPrice = armsPrice;
}
}
/**
* getter for generalPrice instance variable.
*
* @return generalPrice -- the price of general cargo in the shop.
*/
public int getGeneralPrice() {
return generalPrice;
}
/**
* setter for the generalPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param generalPrice -- what the instance variable generalPrice should be changed to.
*/
public void setGeneralPrice(int generalPrice) {
if(generalPrice > 0){
this.generalPrice = generalPrice;
}
}
/**
* this method is evoked if the user has decided to travel elsewhere.
*/
public void travel(){
Travel travel = new Travel(player);
travel.travelTo();
player = travel.getPlayer();
}
/**
* this method is evoked if the user wants to use the warehouse to store items or take items out.
*/
public void warehouse(){
Warehouse warehouse = new Warehouse(player);
warehouse.changeWarehouse();
player = warehouse.getPlayer();
}
/**
* this method is evoked if the user wants to use the bank to deposit or withdraw money.
*/
public void bank(){
Bank bank = new Bank(player);
bank.bank();
player = bank.getPlayer();
}
/**
* this method is evoked if the user wants to use get a loan or pay a loan off.
*/
public void loan(){
loanShark loan = new loanShark(player);
loan.loanMoney();
player = loan.getPlayer();
}
/**
* this method is when the shop is accessed, randomizing the prices of all the items.
*/
public void updatePrices(){
String s = "\n" + player.getName() + ", the price of ";
double value = 80*Math.random();
Random rand = new Random();
opiumPrice = (rand.nextInt(201) + 60)*100;
silkPrice = (rand.nextInt(201) + 60)*10;
armsPrice = (rand.nextInt(21) + 6)*10;
generalPrice = rand.nextInt(17) + 4;
// there is a 10% chance that the price of an item is increased/decreased beyond its regular range.
if(value < 8){
if(value < 2){
if(value < 1){
opiumPrice /= 5;
System.out.println(s + "Opium has dropped to " + opiumPrice +"!!!\n");
}else{
opiumPrice *= 5;
System.out.println(s + "Opium has risen to " + opiumPrice +"!!!\n");
}
}else if(value < 4){
if(value < 3){
silkPrice /= 5;
System.out.println(s + "Silk has dropped to " + silkPrice +"!!!\n");
}else{
silkPrice *= 5;
System.out.println(s + "Silk has risen to " + silkPrice +"!!!\n");
}
}else if(value < 6){
if(value < 3){
armsPrice /= 5;
System.out.println(s + "Arms has dropped to " + armsPrice +"!!!\n");
}else{
armsPrice *= 5;
System.out.println(s + "Arms has risen to " + armsPrice +"!!!\n");
}
}else{
if(value < 7){
generalPrice = 1;
System.out.println(s + "General Cargo has dropped to 1!!!\n");
}else{
generalPrice *= 5;
System.out.println(s + "General Cargo has risen to " + generalPrice + "!!!\n");
}
}
}
}
/**
* this method prints the shop UI and the player's inventory and status.
*/
public void printShop(){
int currentCargo = player.getOpiumHeld()+player.getGuns()*10+player.getSilkHeld()+player.getArmsHeld()+player.getGeneralHeld();
if(player.getCargoSpace() - currentCargo < 0){
System.out.println("Hold: Overloaded" + " Guns: " + player.getGuns() + " HP: " + player.getHP() +"%");
}else{
System.out.println("Hold: " + (player.getCargoSpace()-currentCargo) + " Guns: " + player.getGuns() + " HP: " + player.getHP() +"%");
}
System.out.println("-------------------------------------------------------------");
System.out.println(" Opium: " + player.getOpiumHeld() + " Silk: " + player.getSilkHeld());
System.out.println(" Arms: " + player.getArmsHeld() + " General: " + player.getGeneralHeld());
System.out.println("-------------------------------------------------------------");
System.out.println("Cash: " + player.getMoney() + " Bank: " + player.getBank()+ " Debt: " + player.getDebt()+"\n");
System.out.println(player.getName() + ", present prices per unit here are:");
System.out.println(" Opium: " + opiumPrice + " Silk: " + silkPrice);
System.out.println(" Arms: " + armsPrice + " General: " + generalPrice);
}
/**
* This method is evoked if the user is at the location one port.
*/
public void atLocationOne(){
boolean notDone = true;
Scanner input = new Scanner(System.in);
// as long as the user does not enter a valid input, the code will run in a loop forever.
while(notDone){
printShop();
System.out.println("\nShall I Buy, Sell, Visit Bank, Get Loans, Transfer Cargo, or Quit Trading?");
String response = input.next();
if (response.equalsIgnoreCase("B")) {
boolean notDone2 = true;
System.out.println("What do you wish me to buy, " + player.getName() + "?");
// when buying an item, the user must have the right amount of money, and buy non-negative amounts.
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / opiumPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / opiumPrice && num >= 0) {
player.setOpiumHeld(player.getOpiumHeld()+num);
player.setMoney(player.getMoney()-num * opiumPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / silkPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / silkPrice && num >= 0) {
player.setSilkHeld(player.getSilkHeld()+num);
player.setMoney(player.getMoney()-num * silkPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / armsPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / armsPrice && num >= 0) {
player.setArmsHeld(player.getArmsHeld()+num);
player.setMoney(player.getMoney() - num*armsPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / generalPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / generalPrice && num >= 0) {
player.setGeneralHeld(player.getGeneralHeld()+num);
player.setMoney(player.getMoney() - num*generalPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} // when selling, the user must enter a non-negative amount of items, and not more than what they have.
else if (response.equalsIgnoreCase("S")) {
boolean notDone2 = true;
System.out.println("What do you wish me to sell, " + player.getName() + "?");
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I sell, " + player.getName() + "? (You have " + player.getOpiumHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getOpiumHeld() && num >= 0) {
player.setOpiumHeld(player.getOpiumHeld()-num);
player.setMoney(player.getMoney() + num*opiumPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I sell, " + player.getName() + "? (You have " + player.getSilkHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getSilkHeld() && num >= 0) {
player.setSilkHeld(player.getSilkHeld()-num);
player.setMoney(player.getMoney() + num*silkPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I sell, " + player.getName() + "? (You have " + player.getArmsHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getArmsHeld() && num >= 0) {
player.setArmsHeld(player.getArmsHeld()-num);
player.setMoney(player.getMoney() + num*armsPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I sell, " + player.getName() + "? (You have " + player.getGeneralHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getGeneralHeld() && num >= 0) {
player.setGeneralHeld(player.getGeneralHeld()-num);
player.setMoney(player.getMoney() + num*generalPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} else if (response.equalsIgnoreCase("V")) {
bank();
} else if (response.equalsIgnoreCase("T")) {
warehouse();
}else if (response.equalsIgnoreCase("G")||response.equalsIgnoreCase("L")) {
loan();
} // if the user wishes to quit trading, they may do so. Doing this breaks them out of the loop.
else if (response.equalsIgnoreCase("Q")) {
travel();
notDone = false;
}
}
}
/**
* This method is evoked when the user is at any port other than location one.
*/
public void notAtLocationOne(){
boolean notDone = true;
Scanner input = new Scanner(System.in);
// as long as the user does not enter a valid input, the code will run in a loop forever.
while(notDone){
printShop();
System.out.println("\nShall I Buy, Sell, or Quit Trading?");
String response = input.next();
if (response.equalsIgnoreCase("B")) {
boolean notDone2 = true;
System.out.println("What do you wish me to buy, " + player.getName() + "?");
// when buying an item, the user must have the right amount of money, and buy non-negative amounts.
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / opiumPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / opiumPrice && num >= 0) {
player.setOpiumHeld(player.getOpiumHeld()+num);
player.setMoney(player.getMoney() - num*opiumPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / silkPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / silkPrice && num >= 0) {
player.setSilkHeld(player.getSilkHeld()+num);
player.setMoney(player.getMoney() - num*silkPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / armsPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / armsPrice && num >= 0) {
player.setArmsHeld(player.getArmsHeld()+num);
player.setMoney(player.getMoney() - num*armsPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / generalPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / generalPrice && num >= 0) {
player.setGeneralHeld(player.getGeneralHeld()+num);
player.setMoney(player.getMoney() - num*generalPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} // when selling, the user must enter a non-negative amount of items, and not more than what they have.
else if (response.equalsIgnoreCase("S")) {
boolean notDone2 = true;
System.out.println("What do you wish me to sell, " + player.getName() + "?");
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I sell, " + player.getName() + "? (You have " + player.getOpiumHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getOpiumHeld() && num >= 0) {
player.setOpiumHeld(player.getOpiumHeld()-num);
player.setMoney(player.getMoney() + num*opiumPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I sell, " + player.getName() + "? (You have " + player.getSilkHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getSilkHeld() && num >= 0) {
player.setSilkHeld(player.getSilkHeld()-num);
player.setMoney(player.getMoney() + num*silkPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I sell, " + player.getName() + "? (You have " + player.getArmsHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getArmsHeld() && num >= 0) {
player.setArmsHeld(player.getArmsHeld()-num);
player.setMoney(player.getMoney() + num*armsPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I sell, " + player.getName() + "? (You have " + player.getGeneralHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getGeneralHeld() && num >= 0) {
player.setGeneralHeld(player.getGeneralHeld()-num);
player.setMoney(player.getMoney() + num*generalPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} // if the user wishes to quit trading, they may do so. Doing this breaks them out of the loop.
else if (response.equalsIgnoreCase("Q")) {
travel();
notDone = false;
}
}
}
/**
* this method is run if the user is eligible to win, and is at location one.
*/
public void retireAndLocationOne(){
boolean notDone = true;
Scanner input = new Scanner(System.in);
// as long as the user does not enter a valid input, the code will run in a loop forever.
while(notDone){
printShop();
System.out.println("\nShall I Buy, Sell, Visit Bank, Transfer Cargo, Get Loans, Retire, or Quit Trading?");
String response = input.next();
if (response.equalsIgnoreCase("B")) {
boolean notDone2 = true;
System.out.println("What do you wish me to buy, " + player.getName() + "?");
// when buying an item, the user must have the right amount of money, and buy non-negative amounts.
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / opiumPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / opiumPrice && num >= 0) {
player.setOpiumHeld(player.getOpiumHeld()+num);
player.setMoney(player.getMoney()-num * opiumPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / silkPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / silkPrice && num >= 0) {
player.setSilkHeld(player.getSilkHeld()+num);
player.setMoney(player.getMoney()-num * silkPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / armsPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / armsPrice && num >= 0) {
player.setArmsHeld(player.getArmsHeld()+num);
player.setMoney(player.getMoney() - num*armsPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I buy, " + player.getName() + "? (You can afford " + player.getMoney() / generalPrice + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getMoney() / generalPrice && num >= 0) {
player.setGeneralHeld(player.getGeneralHeld()+num);
player.setMoney(player.getMoney() - num*generalPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you can't afford that!");
} else {
System.out.println(player.getName() + ", how am I supposed to buy " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} // when selling, the user must enter a non-negative amount of items, and not more than what they have.
else if (response.equalsIgnoreCase("S")) {
boolean notDone2 = true;
System.out.println("What do you wish me to sell, " + player.getName() + "?");
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I sell, " + player.getName() + "? (You have " + player.getOpiumHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getOpiumHeld() && num >= 0) {
player.setOpiumHeld(player.getOpiumHeld()-num);
player.setMoney(player.getMoney() + num*opiumPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I sell, " + player.getName() + "? (You have " + player.getSilkHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getSilkHeld() && num >= 0) {
player.setSilkHeld(player.getSilkHeld()-num);
player.setMoney(player.getMoney() + num*silkPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I sell, " + player.getName() + "? (You have " + player.getArmsHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getArmsHeld() && num >= 0) {
player.setArmsHeld(player.getArmsHeld()-num);
player.setMoney(player.getMoney() + num*armsPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I sell, " + player.getName() + "? (You have " + player.getGeneralHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= player.getGeneralHeld() && num >= 0) {
player.setGeneralHeld(player.getGeneralHeld()-num);
player.setMoney(player.getMoney() + num*generalPrice);
notDone2 = false;
} else if (num >= 0) {
System.out.println(player.getName() + ", you don't have that many to sell!");
} else {
System.out.println(player.getName() + ", how am I supposed to sell " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} else if (response.equalsIgnoreCase("V")) {
bank();
} else if (response.equalsIgnoreCase("T")) {
warehouse();
} else if (response.equalsIgnoreCase("G")||response.equalsIgnoreCase("L")) {
loan();
} // if the user wishes to quit trading, they may do so. Doing this breaks them out of the loop.
else if (response.equalsIgnoreCase("Q")) {
travel();
notDone = false;
} // if the user wishes to retire and win the game, they may do so. Doing this breaks them out of the loop.
else if (response.equalsIgnoreCase("R")) {
retire();
notDone = false;
}
}
}
/**
* the general method that utilizes all the other methods to form a fully functioning shop.
*/
public void shop() {
updatePrices();
// first case is triggered if the user is at location one, and has less than $1 million net worth
if (player.getLocation() == 1 && player.getBank()+player.getMoney()-player.getDebt() < 1000000) {
atLocationOne();
} // the second case is triggered if the user is at a location other than location one.
else if(player.getLocation() != 1) {
notAtLocationOne();
} // the last case is triggered when the other conditions are not met; it is triggered when the user has a net
// worth that is greater than or equal to $1 million and is at location one.
else{
retireAndLocationOne();
}
}
}

174
src/Travel.java Normal file
View File

@@ -0,0 +1,174 @@
import java.util.Random;
import java.util.Scanner;
public class Travel {
private Player player;
/**
* sets the player instance variable equal to a copy of the parameter -- a copy is used for encapsulation purposes.
*
* @param player is a Player object that will replace the current instance of the player instance variable.
*/
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* getter method for obtaining a player object.
*
* @return returns player object
*/
public Player getPlayer() {
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* constructor; only runs when a Player object is provided. The constructor is fully encapsulated.
*
* @param player is a Player object that will be copied and the player instance variable is set to the copy.
*/
public Travel(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* When provided a location number: the method returns a print statement stating the location's name and call another
* method to change the location of the Player object.
*
* @param locationOfTravel is a Player object that will be copied and the player instance variable is set to the copy.
*/
private void seaAtlas(int locationOfTravel) {
switch (locationOfTravel) {
case 1:
System.out.println("\nArriving at Hong Kong");
player.setLocation(1);
break;
case 2:
System.out.println("\nArriving at Shanghai");
player.setLocation(2);
break;
case 3:
System.out.println("\nArriving at Nagasaki");
player.setLocation(3);
break;
case 4:
System.out.println("\nArriving at Saigon");
player.setLocation(4);
break;
case 5:
System.out.println("\nArriving at Manila");
player.setLocation(5);
break;
case 6:
System.out.println("\nArriving at Singapore");
player.setLocation(6);
break;
case 7:
System.out.println("\nArriving at Batavia");
player.setLocation(7);
break;
}
}
/**
* Based on random chance either attacks the player with enemy ships, throws them to a different location or does nothing.
*
* @param locationOfTravel is used to see where the player is going to travel, just in case their location is changed
* by a typhoon.
**/
private void randomEventSea(int locationOfTravel) throws Exception {
Random rand = new Random();
int randGenNum = rand.nextInt(3) + 1;
if (randGenNum == 1) {
peasantFleet();
}else if (randGenNum == 2) {
disaster(locationOfTravel);
System.out.println("We made it!");
}
}
/**
* Based on random chance either throws the player character off course, or continues them on their way to their
* destination.
*
* @param locationOfTravel is used to see where the player is going to travel, just in case their location is changed
* by a typhoon.
**/
private void disaster(int locationOfTravel) {
//Tells player that there is a storm approaching.
System.out.print("Storm " + player.getName() + "! ");
Random rand = new Random();
int randGenNum = rand.nextInt(5) + 1;
//If the player lands within this range, nothing happens to them
//Else they randomly get thrown into a location they weren't planning on going to(Anything but location of Travel).
if (randGenNum <= 2) {
System.out.println(" We made it through!");
}else {
while (randGenNum == locationOfTravel) {
randGenNum = rand.nextInt(7) + 1;
if (randGenNum != locationOfTravel) {
System.out.println("We've been blown off course!");
seaAtlas(randGenNum);
}
}
}
}
/**
* To use the peasant fleet class while still maintaining encapsulation we have to create a ShipWarefare object and
* run the method from there. After the method has been run we can update the player object in this class.
* @throws Exception throws Exception so that we can use the time library to make the player if we want to.
**/
public void peasantFleet() throws Exception {
ShipWarfare attackShip = new ShipWarfare(player);
attackShip.peasantFleetAttack();
player = attackShip.getPlayer();
}
/**
*Used to travel between different areas inside of the game world.
* If the player's inventory is too full it won't run.
* Also calculates loan and bank interest between the jumps between islands.
**/
public void travelTo() {
Scanner keyboard = new Scanner(System.in);
String response;
int tempInt;
boolean hasTraveled = false;
//Only lets the player leave the port if their inventory is greater than or equal to the sum of the items in the inventory.
if(player.getCargoSpace() >= (player.getOpiumHeld()+ (player.getGuns()*10)+player.getSilkHeld() + player.getArmsHeld() + player.getGeneralHeld())){
while (true) {
System.out.println("\n" + player.getName() + ", do you wish to go to:\n");
System.out.println("1) Hong Kong, 2) Shanghai, 3) Nagasaki,\n4) Saigon, 5) Manila, 6) Singapore, or 7) Batavia?");
response = keyboard.nextLine();
//Just in case the player types something that was not intended. It will refresh the question and ask it again
try {
tempInt = Integer.parseInt(response);
//Makes sure you can't travel to your own location.
if (tempInt != player.getLocation()) {
randomEventSea(tempInt);
seaAtlas(tempInt);
hasTraveled = true;
player.setBank((int) (player.getBank() * 1.01));
player.setDebt((int) (player.getDebt() * 1.01));
} else System.out.println("\nYou're already here " + player.getName() + ".");
} catch (Exception e) {
System.out.print("\nSorry, " + player.getName() + " could you say that again?");
}
if (hasTraveled) {
break;
}
}
}
else{
System.out.println(player.getName() + " the cargo is too heavy! We can't set sail!");
}
}
}

272
src/Warehouse.java Normal file
View File

@@ -0,0 +1,272 @@
import java.util.Scanner;
/**
* The purpose of this class is to create a warehouse where the goods
* can be safely stored without holing space on the ship!
*/
public class Warehouse {
/*private int wOpium = 0;
private int wSilk = 0;
private int wGeneral = 0;
private int wArms = 0;*/
private Player player;
/**
* setter method that takes in a Player object as an argument.
*
* @param player object of the class Player
*/
public void setPlayer(Player player) {
Player playerDumy = new Player(player);
this.player = playerDumy;
}
/**
* getter method for obtaining a player object.
*
* @return returns player object
*/
public Player getPlayer() {
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* Class Constructor that takes in a type player as a parameter
*
* @param player object of the class Player
*/
public Warehouse(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* This method adds an amount of a certain good
* the user is prompted to enter the amount they would like to
* add followed by the good they would like to add to the warehouse.
* the method checks if the player has sufficient goods to transfer, and if the player does
* then the method executes the transfer
*
*/
public void addAmount() {
boolean askGood = false;
String amount;
int finalAmount = 0;
System.out.println("Please enter the amount of the good you would like to ADD.");
Scanner keyboard = new Scanner(System.in);
amount = keyboard.nextLine();//Asks the user for the amount of the good they would like to add
/*The try function ensures that the program does not crash
due to any errors while giving the program an incorrect input*/
try {
//The if statement checks that you have enough resources to make the transfer
if (Integer.parseInt(amount) <= player.getOpiumHeld() || Integer.parseInt(amount) <= player.getSilkHeld() || Integer.parseInt(amount) <= player.getGeneralHeld() || Integer.parseInt(amount) <= player.getArmsHeld()) {
finalAmount = Integer.parseInt(amount);
askGood = true;
}
//Else statement lets the user know that they do not hav enough goods to make the requested transfer
else {
System.out.println("Nice try but you don't have any items of that quantity!");
askGood = false;
}
//Ensures that goods are only transferred if they have the specified amount
//The user is prompted to enter which good they want to transfer
if (askGood == true) {
String good;
System.out.println("Please enter a good to transfer O, S, G, A :");
good = keyboard.nextLine();
int held = 0;
//The following set of loops check to see which good the user has selected and makes the transfer
if (Integer.parseInt(amount) > 0) {
if (good.equalsIgnoreCase("O")) {
if (player.getOpiumHeld() >= Integer.parseInt(amount)) {
player.setwOpium(player.getwOpium() + finalAmount);
held = player.getOpiumHeld();
player.setOpiumHeld(held - finalAmount);
System.out.println(player.getOpiumHeld());
} else {
System.out.println("You don't even have that much opium!");
}
} else if (good.equalsIgnoreCase("S")) {
if (player.getSilkHeld() >= Integer.parseInt(amount)) {
player.setwSilk(player.getwSilk() + finalAmount);
held = player.getSilkHeld();
player.setSilkHeld(held - finalAmount);
} else {
System.out.println("You don't even have that much silk!");
}
} else if (good.equalsIgnoreCase("G")) {
if (player.getGeneralHeld() >= Integer.parseInt(amount)) {
player.setwGeneral(player.getwGeneral() + finalAmount);
held = player.getGeneralHeld();
player.setGeneralHeld(held - finalAmount);
} else {
System.out.println("You don't even have that much general cargo!");
}
} else if (good.equalsIgnoreCase("A")) {
if (player.getArmsHeld() >= Integer.parseInt(amount)) {
player.setwArms(player.getwArms() + finalAmount);
held = player.getArmsHeld();
player.setArmsHeld(held - finalAmount);
} else {
System.out.println("You don't even have that much Arms!");
}
}
}
//Ensures no negative amounts are entered
else {
System.out.println("Sorry this transfer cannot be made");
}
}
//If the program errors out this is the message displayed and the method is re-run, so that the game does not end.
} catch (Exception e) {
System.out.println("Wait, that's not a valid input please try again");
}
}
/**
* This method removes an amount of a certain good
* the user is prompted to enter the amount they would like to
* remove followed by the good they would like to remove from the warehouse.
* the method checks if the player has sufficient goods to transfer, and if the player does
* then the method executes the transfer
*
*/
public void removeAmount() {
String amount;
boolean askGood = false;
int finalAmount = 0;
System.out.println("Please enter the amount of the good you would like to REMOVE");
Scanner keyboard = new Scanner(System.in);
//Prompts the user for the amount they would like to remove
amount = keyboard.nextLine();
//The if statement checks that you have enough resources to make the transfer
try {
//The if statement checks that you have enough resources to make the transfer
if (Integer.parseInt(amount) <= player.getwOpium() || Integer.parseInt(amount) <= player.getwSilk() || Integer.parseInt(amount) <= player.getwGeneral() || Integer.parseInt(amount) <= player.getwArms()) {
finalAmount = Integer.parseInt(amount);
askGood = true;
}
//Else statement lets the user know that they do not hav enough goods to make the requested transfer
else {
System.out.println("Nice try but you don't have any items of that quantity in the warehouse!");
askGood = false;
}
//Ensures that goods are only transferred if they have the specified amount
//The user is prompted to enter which good they want to transfer
if (askGood == true) {
String good;
System.out.println("Please enter a good to transfer O, S, G, A :");
good = keyboard.nextLine();
int held = 0;
//The following set of loops check to see which good the user has selected and makes the transfer and amount > 0
if (Integer.parseInt(amount) > 0) {
if (good.equalsIgnoreCase("O")) {
if (player.getwOpium() >= Integer.parseInt(amount)) {
player.setwOpium(player.getwOpium() - Integer.parseInt(amount));
held = player.getOpiumHeld();
player.setOpiumHeld(held + finalAmount);
} else {
System.out.println("You don't have that much opium stored in the warehouse!");
}
} else if (good.equalsIgnoreCase("S")) {
if (player.getwSilk() >= Integer.parseInt(amount)) {
player.setwSilk(player.getwSilk() - Integer.parseInt(amount));
held = player.getSilkHeld();
player.setSilkHeld(held + finalAmount);
} else {
System.out.println("You don't have that much silk stored in the warehouse!");
}
} else if (good.equalsIgnoreCase("G")) {
if (player.getwGeneral() >= Integer.parseInt(amount)) {
player.setwGeneral(player.getwGeneral() - Integer.parseInt(amount));
held = player.getGeneralHeld();
player.setGeneralHeld(held + finalAmount);
} else {
System.out.println("You don't have that much general cargo stored in the warehouse!");
}
} else if (good.equalsIgnoreCase("A")) {
if (player.getwArms() >= Integer.parseInt(amount)) {
player.setwArms(player.getwArms() - Integer.parseInt(amount));
held = player.getArmsHeld();
player.setArmsHeld(held + finalAmount);
} else {
System.out.println("You don't have that much arms stored in the warehouse!");
}
}
}
//Ensures the value entered is positive
else {
System.out.println("Sorry this transfer cannot be made");
}
}
}
//If the program errors out this is the message displayed and the method is re-run, so that the game does not end.
catch (Exception e){
System.out.println("Wait, that's not a valid input please try again");
}
}
/**
* This method prints the stock that is in the warehouse currently using the get and set
* methods from the player class. This is to allow the user to be able to know how much they have
* stored in the warehouse
*/
public void showWarehouse() {
System.out.println("--------------------\nWarehouse\n--------------------");
System.out.println("Opium : " + player.getwOpium());
System.out.println("Silk : " + player.getwSilk());
System.out.println("General : " + player.getwGeneral());
System.out.println("Arms : " + player.getwArms());
}
/**
* This method combines the add and remove methods and prompts the user to
* enter what they would like to do. Add or remove and accordingly invokes
* the required methods
*/
public void changeWarehouse() {
boolean keepGoing = true;
while (keepGoing) {
this.showWarehouse();
String input = " ";
System.out.println("Would you like to add(A) or remove(R) resources? ");
Scanner keyboard = new Scanner(System.in);
input = keyboard.next();
if (input.equalsIgnoreCase("R")) {
this.removeAmount();
this.showWarehouse();
} else if (input.equalsIgnoreCase("A")) {
this.addAmount();
this.showWarehouse();
}
else{
System.out.println("Don't waste the warehouse's time, try again later with a valid input");
}
String check;
//Check to see if the player wants to continue in the warehouse or they are done
System.out.println("Would you like to do any other business? Y / N?");
check = keyboard.nextLine();
check = keyboard.nextLine();
if (check.equalsIgnoreCase("Y")) {
keepGoing = true;
} else if (check.equalsIgnoreCase("N")) {
keepGoing = false;
}
}
}
}

97
src/loanShark.java Normal file
View File

@@ -0,0 +1,97 @@
import java.util.Scanner;
public class loanShark {
private Player player;
/**
* setter method that takes in a Player object as an argument.
*
* @param player object of the class Player
*/
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* getter method for obtaining a player object.
*
* @return returns player object
*/
public Player getPlayer(){
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* Class Constructor that takes in a type player as a parameter
*
* @param player object of the class Player
*/
public loanShark(Player player){
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* This methods purpose is to loan the player the funds it wants
* or pay its outstanding debts. The method prompts the user if they
* would like to borrow money or repay. depending on what the player chooses
* the corresponding loop is evoked. The player can only be loaned 2 times the
* money they have minus the debt id their debt exceeds the cash balance, the loan
* cannot be given.
*/
public void loanMoney() {
boolean keepGoing = true;
while(keepGoing) {
String check;
Scanner keyboard = new Scanner(System.in);
System.out.println("Would you like to return money or borrow money?");
//Prompts the user or what they would like to do
check = keyboard.nextLine();
//If the user chooses to return money, the money is subtracted from the cash amount and debt
if(check.equalsIgnoreCase("r")){
int returnAsk = 0;
System.out.println("Please enter how much you would like to return?");
returnAsk = keyboard.nextInt();
if(returnAsk <= player.getDebt() && returnAsk >= 0) {
player.setDebt(player.getDebt() - returnAsk);
player.setMoney(player.getMoney() - returnAsk);
}
//if you try to return more money than you owe it wont allow you to
else if(returnAsk > player.getDebt()){
System.out.println("You don't need to return that much!");
}
//No negative amounts are allowed
else{
System.out.println("You can't return a negative amount.");
}
}
//If the user chooses to borrow, the money is added to cash and the debt is increased
else if(check.equalsIgnoreCase("b")){
int loanAsk = 0;
System.out.println("Please enter how much you would like to borrow");
//Prompts user for the amount they would like to borrow
loanAsk = keyboard.nextInt();
if(loanAsk <= 2*(player.getMoney() - player.getDebt())&& loanAsk >= 0) {
player.setDebt(player.getDebt() + loanAsk);
player.setMoney(player.getMoney() + loanAsk);
}
//If the requested money exceeds 2*(cash - debt) then the loan cannot be given
else{
System.out.println("Sorry you can't be loaned that much");
break;
}
}
//Asks the player if they have any other things to do with the load shark
System.out.println("Would you like to do any other business? Y / N?");
check = keyboard.nextLine();
check = keyboard.nextLine();
if(check.equalsIgnoreCase("Y")) {
keepGoing = true;
}
else if(check.equalsIgnoreCase("N")) {
keepGoing = false;
}
}
}
}

55
src/main.java Normal file
View File

@@ -0,0 +1,55 @@
public class main {
private Player player = new Player();
/**
* getter method for the Player object player.
*
* @return returns a copy of the object player
*/
public Player getPlayer(){
Player copy = new Player(player);
return copy;
}
/**
* Initializes the Taipan shop with the players stats after the player finishes shopping, it updates the player object and returns it.
*
* @param shop player object from the main class used to update the shop class
*/
public void shop(TaipanShop shop){
shop.setPlayer(player);
shop.shop();
player = shop.getPlayer();
}
/**
* Initializes the player object with 5 guns or $400 and $5000 debt.
*
* @param start player object from the main class used to update the start class
*/
public void start(Start start){
start.setPlayer(player);
start.initialize();
player = start.getPlayer();
}
/**
* Updates main class with player data and starts the game.
* The game will only run as long as the player has not retired or has been destroyed.
* @param args Just the console for the player to look at.
*/
public static void main(String[] args) {
main main = new main();
TaipanShop littyShop = new TaipanShop(main.getPlayer());
Start start = new Start(main.getPlayer());
main.start(start);
while(!main.getPlayer().getRetire()){
main.shop(littyShop);
}
}
}