-
Notifications
You must be signed in to change notification settings - Fork 6
Stage #6 Processing coffee machine states #1
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
Open
kostylevv
wants to merge
5
commits into
hyperskill:master
Choose a base branch
from
kostylevv:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f67d6f0
Stage #2: Count needed resources
kostylevv 3c0a1fb
Stage #3: Check if there are enough resources
kostylevv 054d2f6
Stage #4: Choosing different actions
kostylevv 9c71ad9
Stage #5: Creating main loop
kostylevv a63b42f
Stage #6 Processing coffee machine states
kostylevv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,231 @@ | ||
| package coffeeMachine; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.print("Hello world!"); | ||
| boolean cont = true; | ||
| VendingMachine m = new VendingMachine(); | ||
| System.out.println("Hello"); | ||
| String line; | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| while (cont) { | ||
| System.out.print("Write action (buy, fill, take, exit, remaining):"); | ||
| try { | ||
| line = scanner.nextLine().trim(); | ||
| switch (line) { | ||
| case "buy": | ||
| System.out.print("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - return to main menu:"); | ||
| line = scanner.nextLine().trim(); | ||
| switch (line) { | ||
| case "1": | ||
| m.order(Drink.ESPRESSO); | ||
| System.out.println("Espresso is ready."); | ||
| break; | ||
|
|
||
| case "2": | ||
| m.order(Drink.LATTE); | ||
| System.out.println("Latte is ready."); | ||
| break; | ||
|
|
||
| case "3": | ||
| m.order(Drink.CAPPUCHINO); | ||
| System.out.println("Cappuchino is ready."); | ||
| break; | ||
|
|
||
| case "back": | ||
| break; | ||
|
|
||
| default: | ||
| System.out.println("Unsupported drink"); | ||
| } | ||
| System.out.println(); | ||
| break; | ||
|
|
||
| case "fill": | ||
| System.out.println("Write how many ml of water do you want to add:"); | ||
| int water = Integer.parseInt(scanner.nextLine()); | ||
| System.out.println("Write how many ml of milk do you want to add:"); | ||
| int milk = Integer.parseInt(scanner.nextLine()); | ||
| System.out.println("Write how many grams of coffee beans do you want to add:"); | ||
| int beans = Integer.parseInt(scanner.nextLine()); | ||
| System.out.println("Write how many disposable cups do you want to add:"); | ||
| int cups = Integer.parseInt(scanner.nextLine()); | ||
| m.refill(Refill.getInstance(water,milk,beans,cups)); | ||
| break; | ||
|
|
||
| case "take": | ||
| int cash = m.take(); | ||
| System.out.println("I gave you $" + cash); | ||
| break; | ||
|
|
||
| case "remaining": | ||
| System.out.println(m.getState()); | ||
| break; | ||
|
|
||
| case "exit": | ||
| cont = false; | ||
| break; | ||
|
|
||
| default: | ||
| //System.out.println("Unsupported command"); | ||
| } //eof switch | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| }//eof while | ||
| System.out.println("Bye!"); | ||
| }//eof main() | ||
| } | ||
|
|
||
| class Refill implements Coffeeable { | ||
| private Refill() {} | ||
| private static final Refill INSTANCE = new Refill(); | ||
|
|
||
| public static final Refill getInstance(int water, int milk, int beans, int cups) { | ||
| resources.put(ResourceType.WATER, water); | ||
| resources.put(ResourceType.MILK, milk); | ||
| resources.put(ResourceType.BEAN, beans); | ||
| resources.put(ResourceType.CUPS, cups); | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| public Map<ResourceType, Integer> getResources(){ | ||
| return resources; | ||
| } | ||
|
|
||
| private static final Map<ResourceType, Integer> resources = new HashMap<>(); | ||
| } | ||
|
|
||
| class Drink implements Coffeeable, Payable { | ||
| public static final Drink ESPRESSO = new Drink(250,0,16,4); | ||
| public static final Drink LATTE = new Drink(350,75,20,7); | ||
| public static final Drink CAPPUCHINO = new Drink(200,100,12,6); | ||
|
|
||
| public boolean isCredit(){ | ||
| return true; | ||
| } | ||
|
|
||
| public int getCashValue(){ | ||
| return cost; | ||
| } | ||
|
|
||
| private Map<ResourceType, Integer> resources = new HashMap<>(); | ||
| private int cost; | ||
|
|
||
| private Drink(int water, int milk, int beans, int cost) { | ||
| resources.put(ResourceType.WATER, water); | ||
| resources.put(ResourceType.MILK, milk); | ||
| resources.put(ResourceType.BEAN, beans); | ||
| resources.put(ResourceType.CUPS, 1); | ||
| this.cost = cost; | ||
| } | ||
|
|
||
| public Map<ResourceType, Integer> getResources(){ | ||
| return resources; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| class VendingMachine implements Coffeeable, Payable { | ||
|
|
||
| public VendingMachine() { | ||
| resources.put(ResourceType.WATER, 0); | ||
| resources.put(ResourceType.MILK, 0); | ||
| resources.put(ResourceType.BEAN, 0); | ||
| resources.put(ResourceType.CUPS, 0); | ||
| } | ||
|
|
||
| public String getState() { | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| sb.append("The coffee machine has:"); | ||
| sb.append(System.getProperty("line.separator")); | ||
| sb.append(resources.get(ResourceType.WATER) + " of water"); | ||
| sb.append(System.getProperty("line.separator")); | ||
| sb.append(resources.get(ResourceType.MILK) + " of milk"); | ||
| sb.append(System.getProperty("line.separator")); | ||
| sb.append(resources.get(ResourceType.BEAN) + " of coffee beans"); | ||
| sb.append(System.getProperty("line.separator")); | ||
| sb.append(resources.get(ResourceType.CUPS) + " of disposable cups"); | ||
| sb.append(System.getProperty("line.separator")); | ||
| sb.append(getCashValue() + "$ in cash"); | ||
|
|
||
| return sb.toString(); | ||
| } | ||
|
|
||
| public int take() { | ||
| int result = getCashValue(); | ||
| cash = 0; | ||
| return result; | ||
| } | ||
|
|
||
| public void refill(Coffeeable refill) { | ||
| if (refill != null) { | ||
| for (ResourceType resourceType : refill.getResources().keySet()) { | ||
| int avail = (resources.get(resourceType) == null) ? 0 : resources.get(resourceType); | ||
| int additional = refill.getResources().get(resourceType); | ||
| if (additional >= 0) { | ||
| setResource(resourceType, avail+additional); | ||
| } else { | ||
| throw new IllegalArgumentException("Refill cannot be negative"); | ||
| } | ||
| } | ||
| } else throw new IllegalArgumentException("Cannot refill a null"); | ||
| } | ||
| } | ||
|
|
||
| void setResource(ResourceType type, int val){ | ||
| if (val >= 0) { | ||
| resources.put(type, val); | ||
| } else throw new IllegalArgumentException("Resource "+ type +" value cannot be negative, got " + val); | ||
| } | ||
|
|
||
| public Map<ResourceType, Integer> getResources() { | ||
| return resources; | ||
| } | ||
|
|
||
| public boolean isCredit() { | ||
| return false; | ||
| } | ||
|
|
||
| public int getCashValue() { | ||
| return cash; | ||
| } | ||
|
|
||
| public void order(Drink drink) throws MissingResourceException, IllegalArgumentException { | ||
| if (drink != null) { | ||
| for (ResourceType resourceType : drink.getResources().keySet()) { | ||
| int avail = (resources.get(resourceType) == null) ? 0 : resources.get(resourceType); | ||
| int required = (drink.getResources().get(resourceType) == null) ? 0 : drink.getResources().get(resourceType); | ||
| if ((avail-required) < 0) throw new MissingResourceException(resourceType.toString() + " is missing in this machine. Refill required. ", "ResourceType", resourceType.toString()); | ||
| setResource(resourceType, avail-required); | ||
| } | ||
| if (drink.getCashValue() >= 0) { | ||
| cash += drink.getCashValue(); | ||
| } else throw new IllegalArgumentException("Cannot vend a drink with negative cost"); | ||
| } else throw new IllegalArgumentException("Cannot vend a null drink"); | ||
| } | ||
|
|
||
| private Map<ResourceType, Integer> resources = new HashMap<>(); | ||
| private int cash = 0; | ||
| } | ||
|
|
||
| interface Coffeeable { | ||
| enum ResourceType {BEAN, MILK, WATER, CUPS} | ||
| public Map<ResourceType, Integer> getResources(); | ||
| } | ||
|
|
||
| interface Payable { | ||
| /** | ||
| Determines type of transaction with this item. | ||
| If true, item costs some value (even if its 0), i.e. Credit Payable. | ||
| This item can interact only with opposite Payable, i.e. DebitPayable, | ||
| which should return return false and accumulate values provided with | ||
| Credit Payables. | ||
| **/ | ||
| public boolean isCredit(); | ||
|
|
||
| public int getCashValue(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it important to add a comment with eof of a construction? It will be not good in production Java projects. If you want to know about eof after a '}' you may install a plugin for your IDE. Not all members of your team may need to see it.