Last active
December 19, 2015 05:49
-
-
Save alea12/5906843 to your computer and use it in GitHub Desktop.
VendingMachineMain.java
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
| class VendingMachine { | |
| private int inserted_money; | |
| private int cola_price; | |
| // Constructor | |
| VendingMachine() { | |
| inserted_money = 0; | |
| cola_price = 100; | |
| menu(); | |
| } | |
| private void insertMoney() { | |
| System.out.println("-----------------"); | |
| System.out.print("お金を入れてください。投入金額: "); | |
| int adding_value = Keyboard.readInt(); | |
| inserted_money += adding_value; | |
| System.out.println("お金を投入しました。"); | |
| menu(); | |
| } | |
| private void returnMoney() { | |
| System.out.println("-----------------"); | |
| System.out.println(inserted_money + " 円を返却します。"); | |
| inserted_money = 0; | |
| menu(); | |
| } | |
| private void buyProduct() { | |
| System.out.println("-----------------"); | |
| if ( inserted_money >= cola_price ) { | |
| inserted_money -= 100; | |
| System.out.println("コーラが出て来ました。"); | |
| } else { | |
| System.out.println("お金が足りません。"); | |
| } | |
| menu(); | |
| } | |
| private void menu() { | |
| System.out.println("-----------------"); | |
| System.out.println("現在の投入金額: " + inserted_money + " 円"); | |
| System.out.println("以下から選んでください。"); | |
| System.out.println("さらにお金を入れる [1]"); | |
| System.out.println("コーラを購入する [2]"); | |
| System.out.println("お金を返却する [3]"); | |
| System.out.println("自動販売機を離れる [4]"); | |
| int input = Keyboard.readInt(); | |
| if (input == 1) { | |
| insertMoney(); | |
| } else if (input == 2) { | |
| buyProduct(); | |
| } else if (input == 3) { | |
| returnMoney(); | |
| } else if (input == 4) { | |
| if (inserted_money == 0) { | |
| System.out.println("さようなら。"); | |
| } else { | |
| System.out.println("まだお金が残っていますよ!"); | |
| menu(); | |
| } | |
| } else { | |
| System.out.println("1,2,3 から選んで入力してください。"); | |
| menu(); | |
| } | |
| } | |
| } | |
| class VendingMachineMain { | |
| public static void main (String[] args) { | |
| VendingMachine vm = new VendingMachine(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment