Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
11 kB
0
Indexable
Never
import java.util.Arrays;
import java.util.Scanner;
import java.util.Date; 
import java.text.SimpleDateFormat; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.util.Calendar; 

public class Main {
    /*** Get user's input (select feature) and calls the corresponding function
	 * @param args, preset param
	 * @throws
	 * @return 
	 * Example: If user inputs 'B', 
	 * then user will be asked to input a year, and transform(year) will be called.
	 * Time estimate: O(n)
	 */
    public static void main(String[] args) {
    	while(true) {
	    	System.out.print("輸入指令號碼或 E(結束使用)\n");
	    	System.out.print("輸入指令:\n");
	    	System.out.print("1) A 顯示該月月曆\n");
	    	System.out.print("2) B 西元轉換干支、生肖\n");
	    	System.out.print("3) C 計算天數\n");
	    	System.out.print("4) D 計算日期\n");
	    	System.out.print("5) E 離開\n\n");
	    	
	    	Scanner scanner = new Scanner(System.in);
	    	String select = scanner.next();
	    	switch(select) {
	    	case "A":
	    	    System.out.println("請輸入欲查詢日期(年/月/日):");
	//    	    Scanner scannerA = new Scanner(System.in);
	    	    String dateA = scanner.next();
	    		showMonth(dateA);
	    		
	    		break;
	    	case "B":
	    	    System.out.print("請輸入欲查詢年:\n");
	    	    String year = scanner.next();
	    		transform(year);
	    		break;
	    	case "C":
//	    	    Scanner scannerC = new Scanner(System.in);
	    		System.out.print("請輸入欲查詢日期(年/月/日):\n");
	    	    String date = scanner.next();
	    		calDays(date);
//	    		scannerC.close();
	    		break;
	    	case "D":
	    	    System.out.println("請輸入往後推算得天數:");
	    	    Scanner scannerD = new Scanner(System.in);
	    	    int count = scanner.nextInt();
	    		calDate(count);
	    		scannerD.close();
	    		break;
	    	case "E":
	    		System.out.println("---EXIT---");
	    		scanner.close();
	    		System.exit(0);
	    		break;
	    	default:
	    		System.out.println("請重新輸入");
	    		break;
	    	}
    	}
    }
    
    /*** Show monthly calendar
	 * @param string, input date
	 * @throws ParseException
	 *  If the date is not in "yyyy/MM/dd" format, or 
	 *  missing either year, month or day in the string,
	 *  ParseException will be thrown.
	 * @return 
	 * Example: showMonth("2022/03/24"), shows the monthly calendar of 2022/03.
	 * Time estimate: O(1)
	 */
    private static void showMonth(String string) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
    	    Date date = sdf.parse(string);
    	    Calendar calendar = Calendar.getInstance();
    	    calendar.setTime(date);
//    	    int month = calendar.get(Calendar.MONTH);
//    	    int year = calendar.get(Calendar.YEAR);
    	    
    	    calendar.set(Calendar.DAY_OF_MONTH, 1);
    	    int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//            
//          System.out.println(month);
//    	    System.out.println(year);
//    	    System.out.println(firstDayOfWeek);
//    	    System.out.println(daysInMonth);
            
            System.out.println("Sun Mon Tus Wen Thu Fri Sat");

            //print space until the first day of month
            for (int i = 0; i < firstDayOfWeek - 1; i++) {
                System.out.print("    ");
            }

            for (int i = 1, day = firstDayOfWeek; i<=daysInMonth; i++, day++){
                System.out.printf("%3d ", i);
                day = day%7;
                if(day == 0)
                    System.out.print('\n');
            }
            
            System.out.println();
    	    return ;
    	    
    	    
        } catch (ParseException e) {
        	System.out.println("Wrong input format!\n");
            
            return;
        } 
    }

    /*** Parse the corresponding heaven sign from string
	 * @param string, input year
	 * @throws
	 * @return 
	 * Example: Zodiac("2022"), heaven = "壬".
	 * Time estimate: O(1)
	 */
	private static String Heaven(String string) {
    	String heaven = "";
    	int number = Integer.parseInt(string) % 10;
    	switch(number){
    	    case 0:
    	        heaven = "庚";
    	        break;
    	    case 1:
    	        heaven = "辛";
    	        break;
    	    case 2:
    	        heaven = "壬";
    	        break;
    	    case 3:
    	        heaven = "癸";
    	        break;
    	    case 4:
    	        heaven = "甲";
    	        break;
    	    case 5:
    	        heaven = "乙";
    	        break;
    	    case 6:
    	        heaven = "丙";
    	        break;
    	    case 7:
    	        heaven = "丁";
    	        break;
    	    case 8:
    	        heaven = "戊";
    	        break;
    	    case 9:
    	        heaven = "己";
    	        break;
    	   default:
    	        System.out.println("Wrong input format!\n");
    	}
    	return heaven;
    }
    
    /*** Parse the corresponding earth sign from string
	 * @param string, input year
	 * @throws
	 * @return 
	 * Example: Zodiac("2022"), earth = "寅".
	 * Time estimate: O(1)
	 */
    private static String Earth(String string) {
    	String earth = "";
    	int number = Integer.parseInt(string) % 12;
    	switch(number){
    	    case 0:
    	        earth = "申";
    	        break;
    	    case 1:
    	        earth = "酉";
    	        break;
    	    case 2:
    	        earth = "戌";
    	        break;
    	    case 3:
    	        earth = "亥";
    	        break;
    	    case 4:
    	        earth = "子";
    	        break;
    	    case 5:
    	        earth = "丑";
    	        break;
    	    case 6:
    	        earth = "寅";
    	        break;
    	    case 7:
    	        earth = "卯";
    	        break;
    	    case 8:
    	        earth = "辰";
    	        break;
    	    case 9:
    	        earth = "巳";
    	        break;
    	    case 10:
    	        earth = "午";
    	        break;
    	    case 11:
    	        earth = "未";
    	        break;
    	   default:
    	        System.out.println("Wrong input format!\n");
    	}
    	return earth;
    }
    
    /*** Parse the corresponding zodiac sign from string
	 * @param string, input year
	 * @throws
	 * @return 
	 * Example: Zodiac("2022"), zodiac = "虎".
	 * Time estimate: O(1)
	 */
	private static String Zodiac(String string) {
    	String zodiac = "";
    	int number = Integer.parseInt(string) % 12;
    	switch(number){
    	    case 0:
    	        zodiac = "猴";
    	        break;
    	    case 1:
    	        zodiac = "雞";
    	        break;
    	    case 2:
    	        zodiac = "狗";
    	        break;
    	    case 3:
    	        zodiac = "豬";
    	        break;
    	    case 4:
    	        zodiac = "鼠";
    	        break;
    	    case 5:
    	        zodiac = "牛";
    	        break;
    	    case 6:
    	        zodiac = "虎";
    	        break;
    	    case 7:
    	        zodiac = "兔";
    	        break;
    	    case 8:
    	        zodiac = "龍";
    	        break;
    	    case 9:
    	        zodiac = "蛇";
    	        break;
    	    case 10:
    	        zodiac = "馬";
    	        break;
    	    case 11:
    	        zodiac = "羊";
    	        break;
    	   default:
    	        System.out.println("Wrong input format!\n");
    	}
    	return zodiac;
    }
    
    /*** Check whether the input year for transform() is valid
	 * @param stringToCheck, input year
	 * @param radix, range for Character.digit()
	 * @throws
	 * @return true if input year is valid. Otherwise, false.
	 * Example: isStringInteger("-1", 10), return false.
	 * Time estimate: O(n)
	 */
    public static boolean isStringInteger(String stringToCheck, int radix) {
        if(stringToCheck.isEmpty()) return false;           //Check if the string is empty
        for(int i = 0; i < stringToCheck.length(); i++) {
            if(Character.digit(stringToCheck.charAt(i),radix) < 0) return false;
        }
        return true;
    }
    
    /*** Convert western years to sexagenary circle and zodiac
	 * @param string, input year
	 * @throws ParseException
	 *  If the date is not in "yyyy/MM/dd" format, or 
	 *  missing either year, month or day in the string,
	 *  ParseException will be thrown.
	 * @return 
	 * Example: transform("2022"), shows "2022年是壬寅年,屬虎".
	 * Time estimate: O(n)
	 */
    private static void transform(String string) {
        if(!isStringInteger(string, 10)){
          System.out.println("Wrong input format!");
        }
        else{
    	  String heaven = Heaven(string);
    	  String earth = Earth(string);
    	  String zodiac = Zodiac(string);
    	  System.out.println(string+"是"+heaven+earth+"年,屬"+zodiac);
        }
    	return ;
    }
    
    /*** Calculate days
	 * @param string, input date
	 * @throws ParseException
	 *  If the date is not in "yyyy/MM/dd" format, or 
	 *  missing either year, month or day in the string,
	 *  ParseException will be thrown.
	 * @return 
	 * Example: calDays("2022/04/01"), shows "2022/04/01距離今天還有8天".
	 * Time estimate: O(1)
	 */    
    private static void calDays(String string) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        
        try {
    	    Date date = sdf.parse(string);
    	    Calendar calendar = Calendar.getInstance();
    	    calendar.setTime(date);
    	    Calendar calendar2 = Calendar.getInstance();
    	    Date today = calendar2.getTime();
    	    
    	    int days = (int) ((date.getTime()-today.getTime()) / (1000*3600*24));
    	    days = days>=0?days+1:days;
    	    System.out.println(string + "距離今天還有" + days + "天\n");
    	    return ;
        } catch (ParseException e) {
        	System.out.println("Wrong input format!");
            
            return ;
        }
    }

    /*** Calculate Date
	 * @param count, input number of days after
	 * @throws ParseException
	 *  If the date is not in "yyyy/MM/dd" format, or 
	 *  missing either year, month or day in the string,
	 *  ParseException will be thrown.
	 * @return 
	 * Example: calDate("10"), shows "往後10天是2022/04/03".
	 * Time estimate: O(1)
	 */  
    private static void calDate(int count) {
        if(count<0) {
            System.out.println("Wrong input format!\n");
            return;
        }
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    	String ans = "";
	    Calendar calendar = Calendar.getInstance();
	    Date startDay = calendar.getTime();
        Calendar cl = Calendar.getInstance();
        cl.setTime(startDay);
        cl.add(Calendar.DATE, count);
        ans = sdf.format(cl.getTime());
        
    	System.out.println("往後" + count + "天是" + ans + "\n");
    }
}