BLOG-1
BLOG-1|PTA作业|学习通作业
17207119胡立鑫
7-1 电信计费系列3-短信计费
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
本题只考虑短信计费,不考虑通信费用以及月租费。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.Scanner;
import java.util.Comparator;
import java.util.Date;
public class Main {
static String []tel = {"0792","0793","0794","0795","0796","0797","0798","0799","0701"};
public static void main(String[] args) throws ParseException {
Scanner input = new Scanner(System.in);
String ch = new String();
ch = input.nextLine();
ArrayList<User>users = new ArrayList<>();
SimpleDateFormat date=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
// System.out.printf(ch);
while (!ch.equals("end")) {
try {
if ((ch.matches("[u]-1[0-9]{10}\\s[13]")|| ch.matches("[u]-0791[0-9]{7,8}\\s[0]")|| ch.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+"))) {
String[] splitSet = ch.split("-");
if (splitSet[0].equals("u")) {
String[] splitSet2 = splitSet[1].split(" ");
//
if(!numEqual(users,splitSet2[0])){
try {
users.add(newUesr(splitSet2[0]));
} catch (Exception a){
}
}
} else {
users = newRecord(users, ch);
}
}
}catch (Exception e){
}
ch = input.nextLine();
}
Collections.sort(users, new Cmp());
for(User U:users){
try {
System.out.println(U.getNumber()+" "+printDouble(U.calCost())+" "+printDouble(U.calBalance()));
}
catch (Exception ps){
}
}
}
static User newUesr(String ch){
User user = new User(ch);
// System.out.printf(ch+"\n");
return user;
}
public static double printDouble(double num) {
String str = String.format("%.3f",num);
num = Double.parseDouble(str);
return num;
}
static boolean numEqual(ArrayList<User> User,String num){
for(User user:User){
if(user.getNumber().equals(num))
return true;
}
return false;
}
static ArrayList<User> newRecord(ArrayList<User> user,String ch) throws ParseException {
String[] splitSet = ch.split(" ");
String[] splitSet2 = splitSet[0].split("-");
//System.out.printf("1");
for (User U : user) {
if (U.number.equals(splitSet2[1])) {
int len = ch.length()-splitSet[0].length()-splitSet[1].length()-2;
if(len%10!=0)
len = (len/10)+1;
else len=(len/10);
U.masnum+=len;
return user;
}
}
return user;
}
User judgeChargeforAnswering(User user,CallRecord callRecord){
return user;
}
static int judgePlace(String ch){
if (ch.equals("0791")) {
return 1;
}
for (String tel2 : tel) {
if (ch.equals(tel2)) {
return 2;
}
}
return 0;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(CallRecord callRecord) {
double sec = (callRecord.getEndTime().getTime() -callRecord.getStartTime().getTime())/ 1000;
if (sec < 0) {
return 0;
}
double minu = (int) sec / 60;
if (sec % 60 != 0) {
minu += 1;
}
return minu * 0.3;
}
}
class LandPhoneInLandRule extends CallChargeRule{
@Override
public double calCost(CallRecord callRecord) {
double sec = (callRecord.getEndTime().getTime() -callRecord.getStartTime().getTime())/ 1000;
if (sec < 0) {
return 0;
}
double minu = (int) sec / 60;
if (sec % 60 != 0) {
minu += 1;
}
return minu * 0.6;
}
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost(CallRecord callRecord);
}
class CallRecord extends CommunicationRecord{
Date startTime,endTime;
String callingAddressAreaCode,answerAddressAreaCode;
public CallRecord(){}
public CallRecord(String a,String b,String c,String d,String callingAddressAreaCode,String answerAddressAreaCode) throws ParseException {
super();
SimpleDateFormat date=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
this.callingAddressAreaCode =callingAddressAreaCode;
this.answerAddressAreaCode = answerAddressAreaCode;
this.setStartTime(date.parse(a+" "+b));
this.setEndTime(date.parse(c+" "+d));
}
Date getStartTime(){
return startTime;
}
Date getEndTime(){
return endTime;
}
String getCallingAddressAreaCode(){
return callingAddressAreaCode;
}
String getAnswerAddressAreaCode