日常代码

//用于基于时间变换输出电话费的函数实现

import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.omg.CORBA.PRIVATE_MEMBER;

public class TelBill{
    private static String startstring,endstring;
    private static Date startDate,endDate;
    private static boolean startbool,endbool;
    private static long teltime;
    private static boolean ok=true;   //  用于局部判断的标志位
    @SuppressWarnings("deprecation")
    public static void main(String args[]){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            System.out.println("输入电话起始时间(yyyy-MM-dd hh:mm:ss):");
            while(ok){
                startstring = br.readLine();
                startDate=df.parse(startstring);
                if(startDate.getMonth()+1==3&&startDate.getHours()==2&&startDate.getMinutes()>0&&startDate.getDate()+7>31
                        &&startDate.getDay()==0){
                    System.out.println("时间输入有误!重新输入:");
                    ok=true;
                }else {
                    ok=false;
                }
            }//   2017-3-26 2:30:00  2017-3-26 1:30:00
            System.out.println("输入电话结束时间(yyyy-MM-dd hh:mm:ss):");
            ok=false;
            do{
                endstring = br.readLine();
                endDate=df.parse(endstring);
                if(endDate.getMonth()+1==3&&endDate.getHours()==2&&endDate.getMinutes()>0&&endDate.getDate()+7>31
                        &&endDate.getDay()==0){
                    System.out.println("时间输入有误!重新输入:");
                    ok=true;
                }
            }while(ok);
            if(startDate.getMonth()+1==10&&startDate.getDate()+7>31&&startDate.getDay()==0){
                if(startDate.getHours()==2){
                    System.out.println("起始时间判断需要更多信息!");
                    System.out.println("请输入起始时间是否是第一次经过该时间点(Y/N)或(y/n):");
                    ok=true;
                    while(ok){
                        String s=br.readLine();
                        if(s.equals("Y")||s.equals("y")||s.equals("N")||s.equals("n")){
                            ok=false;
                            if(s.equals("Y")||s.equals("y")){
                                startbool=true;
                            }else if(s.equals("N")||s.equals("n")){
                                startbool=false;
                            }
                        }else {
                            System.out.println("输入错误!请重新输入(Y/N)或(y/n):");
                        }
                    }
                }else if(startDate.getHours()>2){
                    startbool=false;
                }else {
                    startbool=true;
                }
            }
            if(endDate.getMonth()+1==10&&endDate.getDate()+7>31&&endDate.getDay()==0){
                if(endDate.getHours()==2){
                    System.out.println("结束时间判断需要更多信息!");
                    System.out.println("请输入结束时间是否是第一次经过该时间点(Y/N)或(y/n):");
                    ok=true;
                    while(ok){
                        String s=br.readLine();
                        if(s.equals("Y")||s.equals("y")||s.equals("N")||s.equals("n")){
                            ok=false;
                            if(s.equals("Y")||s.equals("y")){
                                endbool=true;
                            }else if(s.equals("N")||s.equals("n")){
                                endbool=false;
                            }
                        }else {
                            System.out.println("输入错误!请重新输入(Y/N)或(y/n):");
                        }
                    }
                }else if(endDate.getHours()>2){
                    endbool=false;
                }else {
                    endbool=true;
                }
            }
        } catch (IOException e) {
            System.out.println("输入有误!");
        } catch (ParseException e) {
            System.out.println("时间转换出错!");
        } catch (Exception e) {
            System.out.println("程序出错!");
        }
        int month=startDate.getMonth()+1;
        int endday=endDate.getDate();
        teltime=(long) Math.ceil((endDate.getTime()-startDate.getTime())/(1000*60*1.0));
        if(month==3&&endday+7>31&&endDate.getDay()==0){
            if(startDate.getHours()<2&&endDate.getHours()>2){
                teltime=teltime-60;
            }
        }
        if(month==10&&endday+7>31&&endDate.getDay()==0){
            if(startbool==true&&endbool==false){
                teltime=teltime+60;
            }
        }
        if(teltime>20*60){
            System.out.println("时间间隔大于20小时,输入错误!");
        }else {
            System.out.println("起始时间:"+df.format(startDate));
            System.out.println("结束时间:"+df.format(endDate));
            System.out.println("通话费:"+getBill(teltime));
        }
    
    }
    static double getBill(long time){
        double bill=0;
        if(time<=20){
            bill=time*0.05;
        }else{
            bill=1+(time-20)*0.1;
        }
        return bill;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值