Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.List;
- import java.util.Scanner;
- public class Game {
- private final int stepsInGame;
- private int currentStep;
- private Planet p1;
- private Planet p2;
- private Planet p3;
- public Game(int stepsInGame) {
- this.stepsInGame = stepsInGame;
- }
- public int getStepsInGame() {
- return stepsInGame;
- }
- public int getCurrentStep() {
- return currentStep;
- }
- public void pregameSetups(Trader trader) {
- p1 = new Planet("Galerius");
- p2 = new Planet("Harmonia");
- p3 = new Planet("Niven");
- trader.addDestination(p1);
- trader.addDestination(p2);
- trader.addDestination(p3);
- p1.addCommodity(new Commodity("coffee beans", 500, 3));
- p1.addCommodity(new Commodity("platinum ore", 200, 1));
- p1.addCommodity(new Commodity("sugarcane", 120, 10));
- p2.addCommodity(new Commodity("coffee beans", 360, 3));
- p2.addCommodity(new Commodity("platinum ore", 200, 1));
- p2.addCommodity(new Commodity("sugarcane", 170, 10));
- p3.addCommodity(new Commodity("coffee beans", 410, 3));
- p3.addCommodity(new Commodity("platinum ore", 500, 1));
- p3.addCommodity(new Commodity("sugarcane", 100, 10));
- Commodity c1 = new Commodity("coffee beans", -1, 3);
- Commodity c2 = new Commodity("platinum ore", -1, 1);
- Commodity c3 = new Commodity("sugarcane", -1, 10);
- List<Commodity> commodities = new ArrayList<Commodity>();
- commodities.add(c1);
- commodities.add(c2);
- commodities.add(c3);
- trader.setCommodities(commodities);
- }
- public void start(Trader trader) {
- currentStep++;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Which planet would you like to travel to?");
- System.out.println("\nYou can travel to:");
- for (Planet p : trader.getDestinations()) {
- if (trader.getLocation() == null) {
- System.out.print(p + " ");
- }
- else if (!p.getName().equals(trader.getLocation().getName())) {
- System.out.print(p + " ");
- }
- }
- System.out.println();
- while (true) {
- String input = scanner.nextLine();
- if (trader.getLocation() != null && input.equals(trader.getLocation().getName())) {
- System.out.println("You are already at " + trader.getLocation());
- } else if (input.equals(p1.getName())) {
- System.out.println("Traveling to " + p1 + "...");
- trader.setLocation(p1);
- break;
- } else if (input.equals(p2.getName())) {
- System.out.println("Traveling to " + p2 + "...");
- trader.setLocation(p2);
- break;
- } else if (input.equals(p3.getName())) {
- System.out.println("Traveling to " + p3 + "...");
- trader.setLocation(p3);
- break;
- } else {
- System.out.println("Not a valid planet!");
- }
- }
- System.out.println("...\n...\n...\n...\nYou have arrived at "
- + trader.getLocation());
- printStatistics(trader);
- while (true) {
- System.out.print("\nWould you like to buy or sell?");
- System.out.println(" (buy) (sell) or (done)");
- String choice = scanner.nextLine();
- if (choice.equals("buy")) {
- buy(trader, scanner);
- } else if (choice.equals("sell")) {
- sell(trader, scanner);
- } else if (choice.equals("done")) {
- break;
- } else {
- System.out.println("Not a valid entry");
- }
- }
- p1.randomizePrices();
- p2.randomizePrices();
- p3.randomizePrices();
- scanner.close();
- }
- public void printStatistics(Trader t) {
- System.out.println("\nCurrent stats:");
- printTrader(t);
- printItems(t);
- System.out.println("\n\nPlanet Items:");
- printPlanetItems(t);
- System.out.println();
- }
- public void printPlanetItems(Trader trader) {
- for (Commodity c : trader.getLocation().getCommodities()) {
- System.out.printf("Item: %-20sInventory: %-10dPrice: $%.2f\n",
- c, c.getInventory(), c.getPrice());
- }
- }
- public void printTrader(Trader t) {
- System.out.printf("Name: %-15sTotal money: $%-15.2fPlanet: %s\n", t, t.getTotalMoney(), t.getLocation());
- }
- public void printItems(Trader trader) {
- for (Commodity c : trader.getCommodities()) {
- System.out.printf("Item: %-20sInventory: %d\n", c,
- c.getInventory());
- }
- }
- public void buy(Trader trader, Scanner scanner) {
- boolean bought = false;
- while (true) {
- if (!bought) {
- System.out.println("What would you like to buy? (Press c to cancel)");
- } else {
- System.out.println("Would you like to buy anything else? (Press c to cancel)");
- }
- String item = scanner.nextLine();
- if (item.equals("c")) {
- return;
- } else
- loop: {
- for (Commodity c : trader.getLocation().getCommodities()) {
- if (item.equals(c.getName())) {
- while (true) {
- try {
- System.out
- .println("How many " + item + "? (Enter -1 to cancel)");
- int quantity = scanner.nextInt();
- scanner.nextLine();
- if (quantity == -1) {
- break loop;
- } else if (quantity * c.getPrice() <= trader
- .getTotalMoney()
- && quantity > 0
- && quantity <= c.getInventory()) {
- trader.buy(c, quantity);
- System.out.println("Bought " + quantity
- + " " + item);
- printStatistics(trader);
- bought = true;
- break loop;
- } else if (quantity * c.getPrice() > trader.getTotalMoney()) {
- System.out.println("Not enough money.");
- } else {
- System.out.println("Not valid.");
- }
- } catch (InputMismatchException e) {
- System.out
- .println("Please enter a number.");
- scanner.nextLine();
- }
- }
- }
- }
- System.out.println("Not a valid commodity.");
- }
- }
- }
- public void sell(Trader trader, Scanner scanner) {
- boolean sold = false;
- while (true) {
- if (!sold) {
- System.out.println("What would you like to sell? (Press c to cancel)");
- } else {
- System.out.println("Would you like to sell anything else? (Press c to cancel)");
- }
- String item = scanner.nextLine();
- if (item.equals("c")) {
- return;
- } else
- loop: {
- for (Commodity c : trader.getCommodities()) {
- if (item.equals(c.getName())) {
- double price = 0;
- for (Commodity com : trader.getLocation()
- .getCommodities()) {
- if (com.getName().equals(c.getName())) {
- price = com.getPrice();
- }
- }
- while (true) {
- try {
- System.out
- .println("How many " + item + "? (Enter -1 to cancel)");
- int quantity = scanner.nextInt();
- scanner.nextLine();
- if (quantity == -1) {
- break loop;
- } else if (quantity > 0
- && quantity <= c.getInventory()) {
- trader.sell(c, quantity, price);
- System.out.println("Sold " + quantity
- + " " + item);
- printStatistics(trader);
- sold = true;
- break loop;
- } else {
- System.out.println("Not valid.");
- }
- } catch (InputMismatchException e) {
- System.out
- .println("Please enter a number.");
- scanner.nextLine();
- }
- }
- }
- }
- System.out.println("Not a valid commodity.");
- }
- }
- }
- public boolean gameOver() {
- if (currentStep >= stepsInGame) return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement