//用于基于时间变换输出电话费的函数实现
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;
}
}
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;
}
}
4772

被折叠的 条评论
为什么被折叠?



