Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static Scanner scanner = new Scanner(System.in); // use through-out entire progrsm
  5.  
  6.     public static void main(String[] args) {
  7.         Bar[] bars = {
  8.             new Bar("Moes", true),
  9.             new Bar("Patty's Pub", true)
  10.         };
  11.         Player player = new Player(66, "pat", false, 3000);
  12.         System.out.println("________________");
  13.        
  14.         boolean choosingBar = true;
  15.         while(choosingBar) {
  16.             System.out.println("Which bar would you like to visit? (use numbers to visit, type 'end' to quit the game.)");
  17.            
  18.             // loop through all the bars in the array
  19.             for(int i = 0; i < bars.length; i++)
  20.                 System.out.println(i + ": " + bars[i].getName());
  21.            
  22.             String input = scanner.nextLine();
  23.             if(input.equals("end")) {
  24.                 System.out.println("Thanks for playing!");
  25.                 choosingBar = false;
  26.                 break;
  27.             } else if(!input.matches("\\d+")) { // if input is a not a number
  28.                 System.out.println("You must enter a number!");
  29.                 continue; // skip to the next loop iteraction; dont continue executing this iteration;
  30.             }
  31.  
  32.             int choice = Integer.parseInt(input);
  33.             if(choice >= bars.length || choice < 0) {
  34.                 System.out.println("That bar doesn't exist!");
  35.                 continue;
  36.             }
  37.  
  38.             bars[choice].serve(player);
  39.         }
  40.     }
  41. }
  42.  
  43. class Player {
  44.     private int coinPouch;
  45.     private int age; // keep private
  46.  
  47.     private String name;
  48.     private boolean employed;
  49.  
  50.     public Player(int age, String name, boolean hasJob, int coins) {
  51.         this.coinPouch = coins;
  52.         this.age = age;
  53.         this.name = name;
  54.         this.employed = hasJob;
  55.  
  56.         // displayInfo - a descriptive name for the behavior
  57.         displayInfo();
  58.     }
  59.    
  60.     private void displayInfo() {
  61.         // Ternary Operator: boolean ? valueIfTrue : valueIfFalse;
  62.         String employedMessage = employed ? "I have a job" : "I am unemployed";
  63.         System.out.println("My name is " + name + ", I am an adult and I am " + age + " years old, " + employedMessage);
  64.     }
  65.    
  66.     public int pay(int amount) {
  67.         return coinPouch -= amount;
  68.     }
  69.  
  70.     public int getAge() {
  71.         return age;
  72.     }
  73.  
  74.     public int getCoins() {
  75.         return coinPouch;
  76.     }
  77. }
  78.  
  79.  
  80. class Bar {
  81.     private String name; // added property
  82.     private boolean open;
  83.     private int earnings;
  84.  
  85.     private Drink[] drinks = { // created a new type (Drink) for objects that have a name and a price
  86.         new Drink("Water", 50),
  87.         new Drink("Soda", 33),
  88.         new Drink("Beer", 60)
  89.     };
  90.  
  91.     public Bar(String name, boolean isOpen) {
  92.         this.name = name;
  93.         this.open = isOpen;
  94.  
  95.         displayState();
  96.     }
  97.  
  98.     // bad method name, but i named it this for a reason
  99.     private void displayState() {
  100.         System.out.println(name + " is " + (open ? "open!" : "closed!"));
  101.     }
  102.  
  103.     // checkID doesn't suggest a value will be returned (it sounds like a void method)
  104.     // for booleans, you should use true/false related names
  105.     // isLegal, canDrink, idIsValid, etc...
  106.     public boolean isLegalToDrink(int age) {
  107.         return age >= 21;
  108.     }
  109.  
  110.     public void serve(Player player) {
  111.         // handle validation first
  112.         if(!isLegalToDrink(player.getAge())) {
  113.             System.out.println("You're not old enough to drink alcohol.");
  114.             return; // exit this method
  115.         }
  116.  
  117.         // now this code doesnt need to be indented
  118.         // preferred over if(legalToDrink) { lotsOfCode } else { little code }
  119.         System.out.println("Welcome to " + name + "! What'll you have today? ");
  120.         System.out.println("Type -1 to leave.");
  121.        
  122.         boolean running = true;
  123.         while(running) {
  124.             String input = Main.scanner.nextLine(); // nextInt has issues with console
  125.  
  126.             // make sure input is a number
  127.             if(!input.matches("(\\-?)\\d+")) {
  128.                 System.out.println("You must enter a number!");
  129.                 continue; // skip to the next loop iteraction; dont continue executing this iteration;
  130.             }
  131.  
  132.             int drinkChoice = Integer.parseInt(input);
  133.             switch(drinkChoice) {
  134.                 // "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.
  135.                 // 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.
  136.                 case 1:
  137.                 case 2:
  138.                 case 3:
  139.                     Drink drink = drinks[drinkChoice - 1];
  140.                     int price = drink.getPrice();
  141.  
  142.                     if(player.getCoins() < price) {
  143.                         System.out.println("You can't afford that!");
  144.                     } else {
  145.                         System.out.println(drink.getName() + "? Alright, " + price + " coins.");
  146.                         earnings += player.pay(price);
  147.                     }
  148.                     break;
  149.                 case -1:
  150.                     System.out.println("Thanks for visiting!");
  151.                     running = false;
  152.                     break;
  153.                 default:
  154.                     System.out.println("Sorry, we don't have that.");
  155.                     break; // still want to include "break" for default case
  156.             }
  157.         }
  158.     }
  159.  
  160.     public String getName() {
  161.         return name;
  162.     }
  163. }
  164.  
  165. class Drink {
  166.     private String name;
  167.     private int price;
  168.  
  169.     public Drink(String name, int price) {
  170.         this.name = name;
  171.         this.price = price;
  172.     }
  173.  
  174.     public String getName() {
  175.         return name;
  176.     }
  177.  
  178.     public int getPrice() {
  179.         return price;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement