Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static Scanner scanner = new Scanner(System.in); // use through-out entire progrsm
- public static void main(String[] args) {
- Bar[] bars = {
- new Bar("Moes", true),
- new Bar("Patty's Pub", true)
- };
- Player player = new Player(66, "pat", false, 3000);
- System.out.println("________________");
- boolean choosingBar = true;
- while(choosingBar) {
- System.out.println("Which bar would you like to visit? (use numbers to visit, type 'end' to quit the game.)");
- // loop through all the bars in the array
- for(int i = 0; i < bars.length; i++)
- System.out.println(i + ": " + bars[i].getName());
- String input = scanner.nextLine();
- if(input.equals("end")) {
- System.out.println("Thanks for playing!");
- choosingBar = false;
- break;
- } else if(!input.matches("\\d+")) { // if input is a not a number
- System.out.println("You must enter a number!");
- continue; // skip to the next loop iteraction; dont continue executing this iteration;
- }
- int choice = Integer.parseInt(input);
- if(choice >= bars.length || choice < 0) {
- System.out.println("That bar doesn't exist!");
- continue;
- }
- bars[choice].serve(player);
- }
- }
- }
- class Player {
- private int coinPouch;
- private int age; // keep private
- private String name;
- private boolean employed;
- public Player(int age, String name, boolean hasJob, int coins) {
- this.coinPouch = coins;
- this.age = age;
- this.name = name;
- this.employed = hasJob;
- // displayInfo - a descriptive name for the behavior
- displayInfo();
- }
- private void displayInfo() {
- // Ternary Operator: boolean ? valueIfTrue : valueIfFalse;
- String employedMessage = employed ? "I have a job" : "I am unemployed";
- System.out.println("My name is " + name + ", I am an adult and I am " + age + " years old, " + employedMessage);
- }
- public int pay(int amount) {
- return coinPouch -= amount;
- }
- public int getAge() {
- return age;
- }
- public int getCoins() {
- return coinPouch;
- }
- }
- class Bar {
- private String name; // added property
- private boolean open;
- private int earnings;
- private Drink[] drinks = { // created a new type (Drink) for objects that have a name and a price
- new Drink("Water", 50),
- new Drink("Soda", 33),
- new Drink("Beer", 60)
- };
- public Bar(String name, boolean isOpen) {
- this.name = name;
- this.open = isOpen;
- displayState();
- }
- // bad method name, but i named it this for a reason
- private void displayState() {
- System.out.println(name + " is " + (open ? "open!" : "closed!"));
- }
- // checkID doesn't suggest a value will be returned (it sounds like a void method)
- // for booleans, you should use true/false related names
- // isLegal, canDrink, idIsValid, etc...
- public boolean isLegalToDrink(int age) {
- return age >= 21;
- }
- public void serve(Player player) {
- // handle validation first
- if(!isLegalToDrink(player.getAge())) {
- System.out.println("You're not old enough to drink alcohol.");
- return; // exit this method
- }
- // now this code doesnt need to be indented
- // preferred over if(legalToDrink) { lotsOfCode } else { little code }
- System.out.println("Welcome to " + name + "! What'll you have today? ");
- System.out.println("Type -1 to leave.");
- boolean running = true;
- while(running) {
- String input = Main.scanner.nextLine(); // nextInt has issues with console
- // make sure input is a number
- if(!input.matches("(\\-?)\\d+")) {
- System.out.println("You must enter a number!");
- continue; // skip to the next loop iteraction; dont continue executing this iteration;
- }
- int drinkChoice = Integer.parseInt(input);
- switch(drinkChoice) {
- // "Fall-through cases": if cases do not specify a "break", it will contonue onto the next case, even if that case doesn't match the current value.
- // For example: if drinkChoice is 1, it'll execute case 1, 2 and 3. If 3 didnt have a "break",.the default case would also execute.
- case 1:
- case 2:
- case 3:
- Drink drink = drinks[drinkChoice - 1];
- int price = drink.getPrice();
- if(player.getCoins() < price) {
- System.out.println("You can't afford that!");
- } else {
- System.out.println(drink.getName() + "? Alright, " + price + " coins.");
- earnings += player.pay(price);
- }
- break;
- case -1:
- System.out.println("Thanks for visiting!");
- running = false;
- break;
- default:
- System.out.println("Sorry, we don't have that.");
- break; // still want to include "break" for default case
- }
- }
- }
- public String getName() {
- return name;
- }
- }
- class Drink {
- private String name;
- private int price;
- public Drink(String name, int price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public int getPrice() {
- return price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement