Last active
August 29, 2015 14:14
-
-
Save MuoTK/92614038785208a43f5e to your computer and use it in GitHub Desktop.
アルバイト時給換算用プログラム
This file contains 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
package jp.ac.trindent.computer.s142048; | |
import java.util.Scanner; | |
public class RealizationSystem { | |
private static String confirm(String message, String prompt) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println(message); | |
System.out.print(prompt); | |
return sc.nextLine(); | |
} | |
private static int calcMoney(int endTime, int startTime, int perHour) { | |
return (endTime - startTime) / 100 * perHour; | |
} | |
private static int inputNumber(String message, String prompt) { | |
while(true) { | |
try { | |
String tmp = confirm(message, prompt); | |
int num = Integer.parseInt(tmp); | |
return num; | |
} | |
catch(NumberFormatException e) { | |
System.out.println("半角数字を入力してください。"); | |
continue; | |
} | |
} | |
} | |
private static int inputTime(String message, String prompt) { | |
while (true) { | |
int num = inputNumber(message, prompt); | |
if (num < 2400) return num; | |
System.out.println("無効な時刻です"); | |
} | |
} | |
public static final int DEFAULT_END_TIME = 2200; | |
public static final double PER_HOUR_UP = 1.25; | |
public static void main(String[] args) { | |
String name = confirm("名前を入力してください。", "名前:"); | |
String checkTime = confirm("そのアルバイトは毎回同じ時間だけ働きますか?", "y/N:"); | |
int numberOfTime = inputNumber(name + " さんは週に何回働きますか?", "回数:"); | |
int perHour = inputNumber("アルバイトの時給金額を入力してください。", "金額:"); | |
int weeklyPay = 0; | |
if ("y".equals(checkTime) || "yes".equals(checkTime)) { | |
int startTime = inputTime("アルバイトの開始時刻は何時何分からですか? ※24時間表記で4桁で記入", "開始時刻:"); | |
int endTime = inputTime("アルバイトの終了時刻は何時からですか?", "終了時刻:"); | |
if (2400 > endTime && endTime < startTime) { | |
endTime += 2400; | |
} | |
if (DEFAULT_END_TIME < endTime) { | |
int sum1 = calcMoney(DEFAULT_END_TIME, startTime, perHour); | |
int sum2 = (int)(calcMoney(endTime, DEFAULT_END_TIME, perHour) * PER_HOUR_UP); | |
weeklyPay = (sum1 + sum2) * numberOfTime; | |
} | |
else { | |
weeklyPay = calcMoney(endTime, startTime, perHour) * numberOfTime; | |
} | |
} | |
else { | |
for (int i = 0; i < numberOfTime; i++) { | |
int start = inputTime(i+1 +"日目のアルバイトの開始時刻は何時何分からですか? ※24時間表記で4桁で記入", "開始時刻:"); | |
int end = inputTime("アルバイトの終了時刻は何時からですか?", "終了時刻:"); | |
if (2400 > end && end < start) { | |
end += 2400; | |
} | |
if (DEFAULT_END_TIME < end) { | |
weeklyPay += calcMoney(end, start, perHour) * PER_HOUR_UP; | |
} | |
else { | |
weeklyPay += calcMoney(end, start, perHour); | |
} | |
} | |
} | |
System.out.println(name + " さんの一週間の収入は " + weeklyPay + " 円です。"); | |
System.out.println(name + " さんの一ヶ月の収入は " + weeklyPay * 4 + " 円です。"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment