第三次博客
目录- 第三次博客
前言
本次大作业分析PTA6-PTA8的所有题目。解题思路主要在于理解清楚计费的类图。具体来说就是用户进行可以选择几种通话或者信息方式,再进行通信之后会留下记录,而对于这几种通信方式会有各自的扣费方式。
设计与分析
题目集6——7-1 电信计费系列1-座机计费
①题目及分析
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
输入格式:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。
输出格式:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
②分析
由于本题属于大作业的一部分,所以对于这部分题目内容的分析,我将全部放在该部分的最后一题中的分析部分对我解题的过程进行分析。
③代码
点击查看代码
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<User> us = new ArrayList<User>();
DecimalFormat x1 = new DecimalFormat("###.0#");
ArrayList<CallRecord> callRecords = new ArrayList<CallRecord>();
while(true)
{
String[] arry = null;
Date starttime=null;
Date endtime=null;
String n=in.nextLine();
if(n.equals("end")) {
break;}
if(n.matches("u-079[\\d]{1}[\\d]{7,9} 0")||n.matches("u-0701[\\d]{7,9} 0")||n.matches("t-[\\d]{11,12}\\s[\\d]{11,12}\\s[\\d]{4}.[1-12].[1-31]\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\\s[\\d]{4}.[1-12].[1-31]\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]"))
{
arry=n.split("-| ");
if(arry[0].equals("t")) {
CallRecord callRecord = new CallRecord();
String answerareaCode,callareaCode;//接听区号;拨打区号
callareaCode = arry[1].substring(0, 4);//赋值
answerareaCode = arry[2].substring(0, 4);//赋值
callRecord.setCallingAddressAreaCode(callareaCode);
callRecord.setAnswerAddressAreaCode(answerareaCode);
callRecord.setCallingNumber(arry[1]);
callRecord.setAnswerNumber(arry[2]);
SimpleDateFormat time = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
try {
starttime = time.parse(arry[3]+" "+arry[4]);
} catch (ParseException e) {
e.printStackTrace();
}
try {
endtime = time.parse(arry[5]+" "+arry[6]);
} catch (ParseException e1) {
e1.printStackTrace();
}
callRecord.setStartTime(starttime);//开始时间
callRecord.setEndTime(endtime);//结束时间
callRecords.add(callRecord);//添加用户记录
}
if(arry[0].equals("u")){
String areaCode;
CallRecord callRecord = new CallRecord();
LandlinePhoneCharging lip = new LandlinePhoneCharging();//座机
boolean decide = true;
areaCode = arry[1].substring(0, 4);
callRecord.setCallingAddressAreaCode(areaCode);
for(User user : us)
{
if(user.getNumber().equals(arry[1]))
decide=false;
}
if(decide==true) {
User u = new User(lip,arry[1]);
us.add(u);
}
}
}
}
for(int i=0;i<us.size();i++) {
UserRecords u=new UserRecords();
for(int j=0;j<callRecords.size();j++) {
CallRecord a=callRecords.get(j);
String callnumber=a.callnumber;
if(us.get(i).number.equals(callnumber)) {
if(a.getAnswerAddressAreaCode().matches("0791"))u.addCallingInCityRecords(a);
else if(a.getAnswerAddressAreaCode().matches("0701"))u.addCallingInProvinceRecords(a);
else if(a.getAnswerAddressAreaCode().matches("079[0-9]"))u.addCallingInProvinceRecords(a);
else u.addCallingInLandRecords(a);
}
}
us.get(i).setUserRecords(u);
}
Collections.sort(us,new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
// TODO Auto-generated method stub
double x=Double.parseDouble(o1.getNumber());
double y=Double.parseDouble(o2.getNumber());
return (int) (x-y);
}
});
for(User u : us) {
double cost=Double.parseDouble(x1.format(u.calCost()));
double balance=Double.parseDouble(x1.format(u.calBalance()));
System.out.println(u.getNumber()+" "+cost+" "+balance);
}
in.close();
}
}
abstract class CallChargeRule extends ChargeRule {
public abstract double calCost(ArrayList<CallRecord>callRecords);
}
class CallRecord extends CommunicationRecord{
Date startTime;
Date endTime;
String callingAddressAreaCode;
String answerAddressAreaCode;
String callnumber;
String answernumber;
Date getStartTime() {
return startTime;
}
void setStartTime(Date starttime2) {
this.startTime=(Date) starttime2;
}
Date getEndTIme() {
return endTime;
}
void setEndTime(Date endtime2) {
this.endTime=(Date) endtime2;
}
String getCallingAddressAreaCode() {
return callingAddressAreaCode;
}
void setCallingAddressAreaCode(String callingAddressAreaCode) {
this.callingAddressAreaCode=callingAddressAreaCode;
}
String getAnswerAddressAreaCode() {
return answerAddressAreaCode;
}
void setAnswerAddressAreaCode(String answerAddressAreaCode) {
this.answerAddressAreaCode=answerAddressAreaCode;
}
public void setCallingNumber(String string) {
this.callnumber=string;
}
public void setAnswerNumber(String string) {
this.answernumber=string;
}
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules= new ArrayList<>();
public ArrayList<ChargeRule> getChargeRule(){
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules= chargeRules;
}
public abstract double calCost(UserRecords userRecords);
abstract double getMonthlyRent() ;
}
abstract class ChargeRule {
}
class CommunicationRecord {
private String callingNumber;
private String answerNumber;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber=callingNumber;
}
public String getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(String answerNumber) {
this.answerNumber=answerNumber;
}
}
class LandlinePhoneCharging extends ChargeMode {
double monthlyRent = 20;
public double calCost(UserRecords userRecords) {
double sum = 0.0;
LandPhoneInlandRule p1 = new LandPhoneInlandRule();
LandPhonelnCityRule p2 = new LandPhonelnCityRule();
LandPhonelnProvinceRule p3 = new LandPhonelnProvinceRule();
double sum1= sum+p1.calCost(userRecords.getCallingInLandRecords());
double sum2=sum+p2.calCost(userRecords.getCallingInCityRecords());
double sum3=sum+p3.calCost(userRecords.getCallingInProvinceRecords());
return sum1+sum2+sum3;
}
public double getMonthlyRent() {
return monthlyRent;
}
}
class LandPhoneInlandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTIme().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1==0) {
sum+=s*0.6;
}
else {
double a=s%1;
s=s-a+1;
sum+=s*0.6;
}
}
return sum;
}
}
class LandPhonelnCityRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTIme().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1==0) {
sum+=s*0.1;
}
else {
double a=s%1;
s=s-a+1;
sum+=s*0.1;
}
}
return sum;
}
}
class LandPhonelnProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTIme().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1==0) {
sum+=s*0.3;
}
else {
double a=s%1;
s=s-a+1;
sum+=s*0.3;
}
}
return sum;
}
}
class MessageRecord extends CommunicationRecord {
String message;
String getMessage() {
return message;
}
void setMessage(String message) {
this.message=message;
}
}
class User {
UserRecords userRecords = new UserRecords();
double balance = 100;
ChargeMode chargeMode;
String number;
User(ChargeMode chargeMode,String number){
this.chargeMode=chargeMode;
this.number=number;
}
double calBalance() {
double a = balance-(calCost()+chargeMode.getMonthlyRent());
return a;
}
double calCost() {
double s =chargeMode.calCost(userRecords);
return s;
}
UserRecords getUserRecords() {
return userRecords;
}
void setUserRecords(UserRecords userRecords) {
this.userRecords=userRecords;
}
double getBalance() {
return balance;
}
ChargeMode getChargeMode() {
return chargeMode;
}
void setChargeMode(ChargeMode chargeMode) {
this.chargeMode = chargeMode;
}
String getNumber() {
return number;
}
void setNumber(String number) {
this.number = number;
}
}
class UserRecords {
ArrayList<CallRecord>callinglnCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord>callinglnProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord>callinglnLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord>answerlnCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord>answerlnProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord>answerlnLandRecords = new ArrayList<CallRecord>();
ArrayList<MessageRecord>sendMessageRecords = new ArrayList<MessageRecord>();
ArrayList<MessageRecord>receiveMessageRecords = new ArrayList<MessageRecord>();
public void addCallingInCityRecords (CallRecord callRecord) {
callinglnCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords (CallRecord callRecord) {
callinglnProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords (CallRecord callRecord) {
callinglnLandRecords.add(callRecord);
}
public void addAnswerInCityRecords (CallRecord answerRecord) {
answerlnCityRecords.add(answerRecord);
}
public void addAnswerInProvinceRecords (CallRecord answerRecord) {
answerlnProvinceRecords.add(answerRecord);
}
public void addAnswerInLandRecords (CallRecord answerRecord) {
answerlnLandRecords.add(answerRecord);
}
public void addSendMessageRecords (MessageRecord sendMessageRecord) {
sendMessageRecords.add(sendMessageRecord);
}
public void addReceiveMessageRecords (MessageRecord receiveMessageRecord) {
receiveMessageRecords.add(receiveMessageRecord);
}
public ArrayList<MessageRecord> getSendMessageRecords (){
return this.sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords (){
return this.receiveMessageRecords;
}
public ArrayList<CallRecord> getCallingInCityRecords (){
return this.callinglnCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords (){
return this.callinglnProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords (){
return this.callinglnLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords (){
return this.answerlnCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords (){
return this.callinglnProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords (){
return this.callinglnLandRecords;
}
}
题目集6——7-2 多态测试
①题目及分析
定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。
定义接口Container:
属性:
public static final double pi=3.1415926;
抽象方法:
public abstract double area();
public abstract double volume();
static double sumofArea(Container c[]);
static double sumofVolume(Container c[]);
其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和;
定义Cube类、Cylinder类均实现自Container接口。
Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。
输入格式:
第一行n表示对象个数,对象类型用cube、cylinder区分,cube表示立方体对象,后面输入边长,输入cylinder表示圆柱体对象,后面是底圆半径、高。
输出格式:
分别输出所有容器对象的表面积之和、体积之和,结果保留小数点后2位。
输入样例:
在这里给出一组输入。例如:
4
cube
15.7
cylinder
23.5 100
cube
46.8
cylinder
17.5 200
输出样例:
在这里给出相应的输出。例如:
56771.13
472290.12
②分析
本题还是比较简单的,题目说的非常清楚,无非就是使用接口完成方法的重写实现题目要求。
③代码
点击查看代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner set = new Scanner(System.in);
int num;
num = Integer.parseInt(set.nextLine());
Container[] containers = new Container[num];
for (int i = 0; i < num; i++) {
String str = set.next();
if(str.equals("cube")){
double A = Double.parseDouble(set.next());
Container CU = new Cube(A);
containers[i] = CU;
}
if(str.equals("cylinder")){
double r = Double.parseDouble(set.next());
double h = Double.parseDouble(set.next());
Container CY = new Cylinder(r,h);
containers[i] = CY;
}
}
double S = Container.sum_ofArea(containers);
double V = Container.sum_ofVolume(containers);
double s = Double.parseDouble(String.format("%.2f",S));
double v = Double.parseDouble(String.format("%.2f",V));
System.out.println(s);
System.out.println(v);
}
}
interface Container{
public static final double pi = 3.1415926;
public abstract double area();
public abstract double volume();
static double sum_ofArea(Container containers[]){
double sum = 0;
for (int i = 0; i < containers.length; i++) {
sum += containers[i].area();
}
return sum;
}
static double sum_ofVolume(Container containers[]) {
double sum = 0;
for (int i = 0; i < containers.length; i++) {
sum += containers[i].volume();
}
return sum;
}
}
class Cube implements Container{
public double A;
public Cube() {
}
public Cube(double a) {
this.A = a;
}
@Override
public double area() {
double S = 6*A*A;
return S;
}
@Override
public double volume() {
double V = A*A*A;
return V;//1478.94 14765.48522 3469.88 13141.44 962 21991.14
}
}
class Cylinder implements Container{
private final double r;
private final double h;
public Cylinder(double r,double h){
this.r = r;
this.h = h;
}
@Override
public double area() {
return this.r*this.r*pi*2+this.r*2*pi*this.h;
}
@Override
public double volume() {
return this.r*this.r*pi*this.h;
}
}
④总结与分析
对于接口的运用多学习学习。
题目集7——7-1 电信计费系列2-手机+座机计费
①题目及分析
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
输入:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
②分析
由于本题属于大作业的一部分,所以对于这部分题目内容的分析,我将全部放在该部分的最后一题中的分析部分对我解题的过程进行分析。
③代码
点击查看代码
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Play play = new Play();
play.rc();
}
}
class Play {
ArrayList<User> users = new ArrayList<>();
public void rc() {
User user;
Scanner sc = new Scanner(System.in);
String str;
String[] spn;
DecimalFormat x1 = new DecimalFormat("###.0#");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
simpleDateFormat.setLenient(false);
int pUserCoincide = 0;
while (true) {
str = sc.nextLine();
if (str.equals("end"))
break;
if(str.length() < 5) continue;//长度判断
if(str.charAt(1) != '-') {
continue;
}
if (str.charAt(0) == 'u') {
spn = str.substring(2).split(" ");
if(spn.length != 2)
continue;
if(spn[1].equals("0"))
{
if(!spn[0].matches("0\\d{9,11}"))
continue;
} else if(spn[1].equals("1")) {
if(!(spn[0].matches("1\\d{10}")))
continue;
}
for(int i = 0; i < users.size(); i++){
if(spn[0].equals(users.get(i).getNumber())){
pUserCoincide = 1;
continue;
}
}
if(pUserCoincide == 1){
pUserCoincide = 0 ;
continue;
}
user = new User();
user.setNumber(spn[0]);
if (spn[1].equals("0")) {
user.setChargeMode(new LandlinePhoneCharging());
} else if(spn[1].equals("1")){
user.setChargeMode(new CellPhoneCharging());
}
users.add(user);
}
else if (str.charAt(0) == 't') {
spn = str.substring(2).split(" +");
if(spn.length != 6 && spn.length != 7 && spn.length != 8){
continue;
}
CallRecord callRecord = new CallRecord();
if(spn.length == 6){
if(!spn[0].matches("0\\d{9,11}") || !spn[1].matches("0\\d{9,11}")){
continue;
}
String StartTime = spn[2]+" "+spn[3];
String EndTime = spn[4]+" "+spn[5];
try {
callRecord.setStartTime(simpleDateFormat.parse(StartTime));
callRecord.setEndTime(simpleDateFormat.parse(EndTime));
} catch (ParseException e) {
}
callRecord.setCallingAddressAreaCode(spn[0].substring(0, 4));
callRecord.setAnswerAddressAreaCode(spn[1].substring(0, 4));
callRecord.setCallingNumber(spn[0]);
callRecord.setAnswerNumber(spn[1]);
if(callRecord.getStartTime().compareTo(callRecord.getEndTime()) >= 0){
continue;
}
} else if(spn.length == 7){
if(spn[1].length() <= 4){
if(!spn[2].matches("0\\d{9,11}") || !spn[0].matches("1\\d{10}") || !spn[1].matches("0\\d{2,3}")){
continue;
}
String StartTime = spn[3]+" "+spn[4];
String EndTime = spn[5]+" "+spn[6];
try {
callRecord.setStartTime(simpleDateFormat.parse(StartTime));
callRecord.setEndTime(simpleDateFormat.parse(EndTime));
} catch (ParseException e) {
}
callRecord.setCallingAddressAreaCode(spn[1]);
callRecord.setAnswerAddressAreaCode(spn[2].substring(0, 4));
callRecord.setCallingNumber(spn[0]);
callRecord.setAnswerNumber(spn[2]);
} else if(spn[2].length() <= 4){
if(!(spn[0].matches("0\\d{9,11}")) || !(spn[1].matches("1\\d{10}")) || !(spn[2].matches("0\\d{2,3}"))){
continue;
}
String StartTime = spn[3]+" "+spn[4];
String EndTime = spn[5]+" "+spn[6];
try {
callRecord.setStartTime(simpleDateFormat.parse(StartTime));
callRecord.setEndTime(simpleDateFormat.parse(EndTime));
} catch (ParseException e) {
}
callRecord.setCallingAddressAreaCode(spn[0].substring(0, 4));
callRecord.setAnswerAddressAreaCode(spn[2]);
callRecord.setCallingNumber(spn[0]);
callRecord.setAnswerNumber(spn[1]);
}
if(callRecord.getCallingNumber().equals(callRecord.getAnswerNumber()))
continue;
} else if(spn.length == 8){
if(!spn[4].matches("\\d{4}\\.\\d{1,2}\\.\\d{1,2}") ){
continue;
}
if(!(spn[0].matches("1\\d{10}")) || !spn[2].matches("1\\d{10}") || !spn[1].matches("0\\d{2,3}") || !spn[3].matches("0\\d{2,3}")){
continue;
}
String StartTime = spn[4]+" "+spn[5];
String EndTime = spn[6]+" "+spn[7];
try {
callRecord.setStartTime(simpleDateFormat.parse(StartTime));
callRecord.setEndTime(simpleDateFormat.parse(EndTime));
} catch (ParseException e) {
continue;
}
callRecord.setCallingAddressAreaCode(spn[1]);
callRecord.setAnswerAddressAreaCode(spn[3]);
callRecord.setCallingNumber(spn[0]);
callRecord.setAnswerNumber(spn[2]);
}
if(callRecord.getStartTime().compareTo(callRecord.getEndTime()) >= 0){
continue;
}
for (User value : users) {
if (callRecord.callingNumber.equals(value.getNumber())) {
if (value.getChargeMode() instanceof LandlinePhoneCharging) {
if (callRecord.getAnswerAddressAreaCode().equals("0791")) {
value.getUserRecords().addCallingCityRecords(callRecord);
} else if (callRecord.getAnswerAddressAreaCode().matches("(079\\d)|(0701)")) {
value.getUserRecords().addCallingInProvinceRecords(callRecord);
} else {
value.getUserRecords().addCallingInLandRecords(callRecord);
}
} else {
if (callRecord.getCallingAddressAreaCode().equals("0791")) {
value.getUserRecords().addCallingCityRecords(callRecord);
} else if (callRecord.getCallingAddressAreaCode().matches("(079\\d)|(0701)")) {
value.getUserRecords().addCallingInProvinceRecords(callRecord);
} else {
value.getUserRecords().addCallingInLandRecords(callRecord);
}
}
}
if (callRecord.answerNumber.equals(value.getNumber())) {
if (callRecord.getAnswerAddressAreaCode().equals("0791")) {
value.getUserRecords().addAnswerInCityRecords(callRecord);
} else if (callRecord.getAnswerAddressAreaCode().matches("(079\\d)|(0701)")) {
value.getUserRecords().addAnswerInProvinceRecords(callRecord);
} else {
value.getUserRecords().addAnswerInLandRecords(callRecord);
}
}
}
}
}
users.sort(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getNumber().compareTo(o2.getNumber());
}
});
for(User u : users) {
double cost=Double.parseDouble(x1.format(u.calCost()));
double balance=Double.parseDouble(x1.format(u.calBalance()));
System.out.println(u.getNumber()+" "+cost+" "+balance);
}
}
}
class MessageRecord extends CommunicationRecord{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.3;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.3;
}
}
return sum;
}
}
class LandPhoneInLandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.6;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.6;
}
}
return sum;
}
}
class LandPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.1;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.1;
}
}
return sum;
}
}
class LandlinePhoneCharging extends ChargeMode{
double monthlyRent = 20;
public LandlinePhoneCharging(){
getChargeRules().add(new LandPhoneInCityRule());
getChargeRules().add(new LandPhoneInProvinceRule());
getChargeRules().add(new LandPhoneInLandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
LandPhoneInCityRule p1 = new LandPhoneInCityRule();
LandPhoneInProvinceRule p2 = new LandPhoneInProvinceRule();
LandPhoneInLandRule p3 = new LandPhoneInLandRule();
sum = sum + p1.calCost(userRecords.getCallingInCityRecords());
sum = sum + p2.calCost(userRecords.getCallingInProvinceRecords());
sum = sum + p3.calCost(userRecords.getCallingInLandRecords());
return sum;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumber;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(String answerNumber) {
this.answerNumber = answerNumber;
}
}
abstract class ChargeRule {
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost(ArrayList<CallRecord> callRecords);
}
class CellPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1==0) {
sum+=s*0.3;
}
else {
double a=s%1;
s=s-a+1;
sum+=s*0.3;
}
}
return sum;
}
}
class CellPhoneInLandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1==0) {
sum+=s*0.6;
}
else {
double a=s%1;
s=s-a+1;
sum+=s*0.6;
}
}
return sum;
}
public double calAnswerCost(ArrayList<CallRecord> callRecords){
double sum=0.0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1==0) {
sum+=s*0.3;
}
else {
double a=s%1;
s=s-a+1;
sum+=s*0.3;
}
}
return sum;
}
}
class CellPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for(int i=0;i<callRecords.size();i++)
{
double s = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime();
s=s/1000.0/60;//分钟
if(s%1!=0) {
double a=s%1;
s=s-a+1;
}
if(callRecords.get(i).getAnswerAddressAreaCode().equals("0791")){
sum = sum + s*0.1;
} else if(callRecords.get(i).getAnswerAddressAreaCode().matches("079\\d|0701")){
sum = sum + s*0.2;
} else {
sum = sum + s*0.3;
}
}
//判断拨打的位置,分别计费
return sum;
}
}
class CellPhoneCharging extends ChargeMode{
double monthlyRent = 15;
public CellPhoneCharging(){
getChargeRules().add(new CellPhoneInCityRule());
getChargeRules().add(new CellPhoneInProvinceRule());
getChargeRules().add(new CellPhoneInLandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
CellPhoneInCityRule c1 = new CellPhoneInCityRule();
CellPhoneInProvinceRule c2 = new CellPhoneInProvinceRule();
CellPhoneInLandRule c3 = new CellPhoneInLandRule();
sum = sum + c1.calCost(userRecords.getCallingInCityRecords());
sum = sum + c2.calCost(userRecords.getCallingInProvinceRecords());
sum = sum + c3.calCost(userRecords.getCallingInLandRecords());
sum = sum + c3.calAnswerCost(userRecords.getAnswerInLandRecords());
return sum;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class CallRecord extends CommunicationRecord{
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getCallingAddressAreaCode() {
return callingAddressAreaCode;
}
public void setCallingAddressAreaCode(String callingAddressAreaCode) {
this.callingAddressAreaCode = callingAddressAreaCode;
}
public String getAnswerAddressAreaCode() {
return answerAddressAreaCode;
}
public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
this.answerAddressAreaCode = answerAddressAreaCode;
}
}
class UserRecords {
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>();
private ArrayList<MessageRecord> sendMessageRecord = new ArrayList<>();
private ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<>();
public void addCallingCityRecords(CallRecord callRecord){
callingInCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords(CallRecord callRecord){
callingInProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords(CallRecord callRecord){
callingInLandRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord){
answerInCityRecords.add(callRecord);
}
public void addAnswerInProvinceRecords(CallRecord callRecord){
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord){
answerInLandRecords.add(callRecord);
}
public void addReceiveMessageRecords(MessageRecord messageRecord){
receiveMessageRecord.add(messageRecord);
}
public void addSendMessageRecords(MessageRecord messageRecord){
sendMessageRecord.add(messageRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecord() {
return sendMessageRecord;
}
public ArrayList<MessageRecord> getReceiveMessageRecord() {
return receiveMessageRecord;
}
}
class User {
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public double calBalance() {
balance = balance - calCost() - chargeMode.getMonthlyRent();
return balance;
}
public double calCost() {
return chargeMode.calCost(userRecords) ;
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public double getBalance() {
return balance;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode chargeMode) {
this.chargeMode = chargeMode;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
④报表
题目集7——7-2 sdut-Collection-sort--C~K的班级(II)
①题目及分析
经过不懈的努力,C~K终于当上了班主任。
现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?
输入格式:
第一行输入一个N,代表C~K导出的名单共有N行(N<100000).
接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。
6
0001 MeiK 20 M
0001 MeiK 20 M
0002 sdk2 21 M
0002 sdk2 21 M
0002 sdk2 21 M
0000 blf2 22 F
输出格式:
第一行输出一个n,代表删除重复名字后C~K的班级共有几人。
接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。
3
0000 blf2 22 F
0001 MeiK 20 M
0002 sdk2 21 M
②代码
点击查看代码
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
ArrayList<Student> students = new ArrayList<>();
for (int i = 0; i < num; i++) {
String new_sno = sc.next();
String new_sname = sc.next();
int new_age = Integer.parseInt(sc.next());
String new_sex = sc.next();
Student new_student = new Student();
new_student.setSname(new_sname);
new_student.setAge(new_age);
new_student.setSno(new_sno);
new_student.setSsex(new_sex);
students_add(students,new_student);
}
//compareTO
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int x = Integer.parseInt(o1.getSno());
int y = Integer.parseInt(o2.getSno());
return x-y;
}
});
System.out.println(students.size());
for (int i = 0; i < students.size(); i++) {
students.get(i).tosc();
}
}
public static void students_add(ArrayList<Student> students,Student new_student){
for (Student student : students) {
if (new_student.Sname.equals(student.Sname)) {
return;
}
}
students.add(new_student);
}
static class Student{
private String Sno;
private String Sname;
private int age;
private String Ssex;
public Student() {
}
public Student(String Sno, String Sname, int age, String Ssex) {
this.Sno = Sno;
this.Sname = Sname;
this.age = age;
this.Ssex = Ssex;
}
public String getSno() {
return Sno;
}
public void setSno(String Sno) {
this.Sno = Sno;
}
public String getSname() {
return Sname;
}
public void setSname(String Sname) {
this.Sname = Sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String Ssex) {
this.Ssex = Ssex;
}
public void tosc(){
System.out.println(this.Sno+" "+this.Sname+" "+this.age+" "+this.Ssex);
}
}
}
③报表
④总结与分析
本题比较简单,设计好学生的基本的JAVABean类,再学习一下如何正确使用自定义的SORT方法按照数组集合中的某个属性大小进行排序就能够解决本题。
题目集7——7-3 按照题目需求修改程序
①题目及分析
功能需求:
使用集合存储3个员工的信息(有序);
通过迭代器依次找出所有的员工。
提示:学生复制以下代码到编程区,并按需求进行调试修改。
②分析
调试代码,没啥好分析的。总结里面写具体分析以及遇上的问题。
③代码
点击查看代码
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
//定义员工类
class Employee {
private String name;
private int age;
public Employee() {
super();
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//主函数
public class Main {
public static void main(String[] args) {
// 1、创建有序集合对象
Collection<Employee> c = new ArrayList<>();
Scanner sc = new Scanner(System.in);
// 创建3个员工元素对象
for (int i = 0; i < 3; i++) {
String employeeName = sc.next();
int employeeAge = sc.nextInt();
Employee employee = new Employee(employeeName, employeeAge);
c.add(employee);
}
// 2、创建迭代器遍历集合
Iterator it = c.iterator();
//3、遍历
while (it.hasNext()) {
//4、集合中对象未知,向下转型
Employee e = (Employee) it.next();
System.out.println(e.getName() + "---" + e.getAge());
}
}
}
④报表
⑤总结与分析
不得不说该题目非常奇怪,至始至终没有找到问题所在。个人觉得SCANNER 定义的位置是通过测试点的关键,“一模一样”的代码跑出来还不能过,自己不断调整函数位置,才通过,就很离谱。
题目集8——7—2编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)
①题目及分析
编写一个类Shop(商店),该类中有一个成员内部类InnerCoupons(内部购物券),可以用于购买该商店的牛奶(假设每箱牛奶售价为50元)。要求如下:
(1)Shop类中有私有属性milkCount(牛奶的箱数,int类型)、公有的成员方法setMilkCount( )和getMilkCount( )分别用于设置和获取牛奶的箱数。
(2)成员内部类InnerCoupons,有公有属性value(面值,int类型),一个带参数的构造方法可以设定购物券的面值value,一个公有的成员方法buy( )要求输出使用了面值为多少的购物券进行支付,同时使商店牛奶的箱数减少value/50。
(3)Shop类中还有成员变量coupons50(面值为50元的内部购物券,类型为InnerCoupons)、coupons100(面值为100元的内部购物券,类型为InnerCoupons)。
(4)在Shop类的构造方法中,调用内部类InnerCoupons的带参数的构造方法分别创建上面的购物券coupons50、coupons100。
在测试类Main中,创建一个Shop类的对象myshop,从键盘输入一个整数(大于或等于3),将其设置为牛奶的箱数。假定有顾客分别使用了该商店面值为50的购物券、面值为100的购物券各消费一次,分别输出消费后商店剩下的牛奶箱数。
输入格式:
输入一个大于或等于3的整数。
输出格式:
使用了面值为50的购物券进行支付
牛奶还剩XX箱
使用了面值为100的购物券进行支付
牛奶还剩XX箱
②分析
非常easy,没啥好说的。
③代码
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Shop myshop = new Shop();
myshop.setMilkCount(sc.nextInt());
myshop.coupons50.buy();
myshop.setMilkCount(myshop.getMilkCount()-myshop.coupons50.getValue()/50);
System.out.println("牛奶还剩"
+myshop.getMilkCount()+"箱");
myshop.coupons100.buy();
myshop.setMilkCount(myshop.getMilkCount()-myshop.coupons100.getValue()/50);
System.out.println("牛奶还剩"
+myshop.getMilkCount()+"箱");
}
}
class InnerCoupons{
public int value;
public InnerCoupons() {
}
public InnerCoupons(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void buy(){
System.out.println("使用了面值为"+this.value+"的购物券进行支付");
}
}
class Shop{
private int milkCount;
InnerCoupons coupons50 = new InnerCoupons(50);
InnerCoupons coupons100 = new InnerCoupons(100);
public Shop() {
}
public Shop(int milkCount, InnerCoupons coupons50, InnerCoupons coupons100) {
this.milkCount = milkCount;
this.coupons50 = coupons50;
this.coupons100 = coupons100;
}
public int getMilkCount() {
return milkCount;
}
public void setMilkCount(int milkCount) {
this.milkCount = milkCount;
}
public InnerCoupons getCoupons50() {
return coupons50;
}
public void setCoupons50(InnerCoupons coupons50) {
this.coupons50 = coupons50;
}
public InnerCoupons getCoupons100() {
return coupons100;
}
public void setCoupons100(InnerCoupons coupons100) {
this.coupons100 = coupons100;
}
}
④报表
⑤总结
这里题目没有什么踩坑的地方,比较简单就不做过多解释啦!
题目集8——7-3 动物发声模拟器(多态)
①题目及分析
设计一个动物发生模拟器,用于模拟不同动物的叫声。比如狮吼、虎啸、狗旺旺、猫喵喵……。
定义抽象类Animal,包含两个抽象方法:获取动物类别getAnimalClass()、动物叫shout();
然后基于抽象类Animal定义狗类Dog、猫类Cat和山羊Goat,用getAnimalClass()方法返回不同的动物类别(比如猫,狗,山羊),用shout()方法分别输出不同的叫声(比如喵喵、汪汪、咩咩)。
最后编写AnimalShoutTest类测试,输出:
猫的叫声:喵喵
狗的叫声:汪汪
山羊的叫声:咩咩
其中,在AnimalShoutTestMain类中,用speak(Animal animal){}方法输出动物animal的叫声,在main()方法中调用speak()方法,分别输出猫、狗和山羊对象的叫声。
②分析
还是一样,混分项目。使用强一点的编译软件把代码复制进去根据爆红来改报错。基本上没什么问题,这里就不给大家总结啥了。
③代码
点击查看代码
//动物发生模拟器. 请在下面的【】处添加代码。
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
Dog dog = new Dog();
Goat goat = new Goat();
speak(cat);
speak(dog);
speak(goat);
}
//定义静态方法speak()
public static void speak(Animal animal){
animal.getAnimalClass();
System.out.print("的叫声:");
animal.shot();
}
}
//定义抽象类Animal
abstract class Animal{
abstract public void getAnimalClass();
abstract public void shot();
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{
@Override
public void getAnimalClass() {
System.out.print("猫");
}
@Override
public void shot() {
System.out.println("喵喵");
}
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
@Override
public void getAnimalClass() {
System.out.print("狗");
}
@Override
public void shot() {
System.out.println("汪汪");
}
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
@Override
public void getAnimalClass() {
System.out.print("山羊");
}
@Override
public void shot() {
System.out.println("咩咩");
}
}
题目集8——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.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Play play = new Play();
play.play();
}
}
class Play {
ArrayList<User> users = new ArrayList<>();
User user;
public void play() {
Scanner sc = new Scanner(System.in);
String s;
String[] a;
int pUserCoincide = 0;
DecimalFormat x1 = new DecimalFormat("###.0#");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
simpleDateFormat.setLenient(false);
while (true) {
s = sc.nextLine();
if (s.equals("end"))
break;
if(s.length() < 2)
continue;
if(s.charAt(1) != '-'){
continue;
}
if (s.charAt(0) == 'u') {
a = s.substring(2).split(" ");
if(a.length != 2)
continue;
if(a[1].equals("0"))
{
if(!a[0].matches("0(\\d{9}|\\d{10}|\\d{11})"))
continue;
}
else if(a[1].equals("1")) {
if(!a[0].matches("1\\d{10}"))
continue;
}
else if(a[1].equals("3")) {
if(!a[0].matches("1\\d{10}"))
continue;
}
for (User value : users) {
if (a[0].equals(value.getNumber())) {
pUserCoincide = 1;
continue;
}
}
if(pUserCoincide == 1){
pUserCoincide = 0 ;
continue;
}
user = new User();
user.setNumber(a[0]);
if(a[1].equals("3")){
user.setChargeMode(new MessageCharging());
}
users.add(user);
} else if (s.charAt(0) == 't') {
//电话
} else if(s.charAt(0) == 'm'){
a = s.substring(2).split(" ");
MessageRecord messageRecord = new MessageRecord();
if(s.matches("m-1\\d{10} 1\\d{10} (\\d|\\w| |,|\\.)+")){
messageRecord.setMessage(s.substring(s.indexOf(' ',s.indexOf(' ')+1)+1));
} else{
continue;
}
for (User u : users) {
if (a[0].equals(u.getNumber())) {
u.getUserRecords().addSendMessageRecords(messageRecord);
}
}
for (User u : users) {
if (a[1].equals(u.getNumber())) {
u.getUserRecords().addReceiveMessageRecords(messageRecord);
}
}
}
}
users.sort(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getNumber().compareTo(o2.getNumber());
}
});
for (User u : users) {
u.calBalance();
String number = u.getNumber();
double cost = Double.parseDouble(x1.format(u.calCost()));
double balance = Double.parseDouble(x1.format(u.getBalance()));
System.out.println(number+" "+cost+" "+balance);
}
}
}
abstract class MessageChargeRule extends ChargeRule{
public abstract double calCost(ArrayList<MessageRecord> callRecords);
}
class MessageRule extends MessageChargeRule{
@Override
public double calCost(ArrayList<MessageRecord> messageRecords) {
double cost;
int m_msg = 0;
for (MessageRecord messageRecord : messageRecords) {
if (messageRecord.getMessage().length() % 10 == 0) {
m_msg = m_msg + messageRecord.getMessage().length() / 10;
} else {
m_msg = m_msg + messageRecord.getMessage().length() / 10 + 1;
}
}
if(m_msg <= 3){
cost = m_msg * 0.1;
} else if(m_msg <= 5){
cost = 3 * 0.1 + (m_msg - 3) * 0.2;
} else {
cost = 3 * 0.1 + 2 * 0.2 + (m_msg - 5) * 0.3;
}
return cost;
}
}
class MessageCharging extends ChargeMode{
double monthlyRent = 0;
public MessageCharging(){
getChargeRules().add(new MessageRule());
}
@Override
public double calCost(UserRecords userRecords) {
double cost = 0;
cost = ((MessageRule)getChargeRules().get(0)).calCost(userRecords.getSendMessageRecord());
return cost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class UserRecords {
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>();
private ArrayList<MessageRecord> sendMessageRecord = new ArrayList<>();
private ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<>();
public void addCallingCityRecords(CallRecord callRecord){
callingInCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords(CallRecord callRecord){
callingInProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords(CallRecord callRecord){
callingInLandRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord){
answerInCityRecords.add(callRecord);
}
public void addAnswerInProvinceRecords(CallRecord callRecord){
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord){
answerInLandRecords.add(callRecord);
}
public void addReceiveMessageRecords(MessageRecord messageRecord){
receiveMessageRecord.add(messageRecord);
}
public void addSendMessageRecords(MessageRecord messageRecord){
sendMessageRecord.add(messageRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecord() {
return sendMessageRecord;
}
public ArrayList<MessageRecord> getReceiveMessageRecord() {
return receiveMessageRecord;
}
}
class User {
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public void calBalance() {
balance = balance - calCost() - chargeMode.getMonthlyRent();
}
public double calCost() {
return chargeMode.calCost(userRecords) ;
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public double getBalance() {
return balance;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode chargeMode) {
this.chargeMode = chargeMode;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
class MessageRecord extends CommunicationRecord{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.3;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.3;
}
}
return sum;
}
}
class LandPhoneInLandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.6;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.6;
}
}
return sum;
}
}
class LandPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.1;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.1;
}
}
return sum;
}
}
class LandlinePhoneCharging extends ChargeMode{
double monthlyRent = 20;
public LandlinePhoneCharging(){
getChargeRules().add(new LandPhoneInCityRule());
getChargeRules().add(new LandPhoneInProvinceRule());
getChargeRules().add(new LandPhoneInLandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
LandPhoneInCityRule p1 = new LandPhoneInCityRule();
LandPhoneInProvinceRule p2 = new LandPhoneInProvinceRule();
LandPhoneInLandRule p3 = new LandPhoneInLandRule();
sum = sum + p1.calCost(userRecords.getCallingInCityRecords());
sum = sum + p2.calCost(userRecords.getCallingInProvinceRecords());
sum = sum + p3.calCost(userRecords.getCallingInLandRecords());
return sum;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumber;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(String answerNumber) {
this.answerNumber = answerNumber;
}
}
abstract class ChargeRule {
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
}
class CellPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.3;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.3;
}
}
return sum;
}
}
class CellPhoneInLandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.6;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.6;
}
}
return sum;
}
public double calAnswerCost(ArrayList<CallRecord> callRecords){
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 == 0) {
sum += s * 0.3;
} else {
double a = s % 1;
s = s - a + 1;
sum += s * 0.3;
}
}
return sum;
}
}
class CellPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum=0.0;
for (CallRecord callRecord : callRecords) {
double s = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime();
s = s / 1000.0 / 60;//分钟
if (s % 1 != 0) {
double a = s % 1;
s = s - a + 1;
}
if (callRecord.getAnswerAddressAreaCode().equals("0791")) {
sum = sum + s * 0.1;
} else if (callRecord.getAnswerAddressAreaCode().matches("079\\d|0701")) {
sum = sum + s * 0.2;
} else {
sum = sum + s * 0.3;
}
}
return sum;
}
}
class CellPhoneCharging extends ChargeMode{
double monthlyRent = 15;
public CellPhoneCharging(){
getChargeRules().add(new CellPhoneInCityRule());
getChargeRules().add(new CellPhoneInProvinceRule());
getChargeRules().add(new CellPhoneInLandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
CellPhoneInCityRule c1 = new CellPhoneInCityRule();
CellPhoneInProvinceRule c2 = new CellPhoneInProvinceRule();
CellPhoneInLandRule c3 = new CellPhoneInLandRule();
sum = sum + c1.calCost(userRecords.getCallingInCityRecords());
sum = sum + c2.calCost(userRecords.getCallingInProvinceRecords());
sum = sum + c3.calCost(userRecords.getCallingInLandRecords());
sum = sum + c3.calAnswerCost(userRecords.getAnswerInLandRecords());
return sum;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class CallRecord extends CommunicationRecord{
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getCallingAddressAreaCode() {
return callingAddressAreaCode;
}
public void setCallingAddressAreaCode(String callingAddressAreaCode) {
this.callingAddressAreaCode = callingAddressAreaCode;
}
public String getAnswerAddressAreaCode() {
return answerAddressAreaCode;
}
public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
this.answerAddressAreaCode = answerAddressAreaCode;
}
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost(ArrayList<CallRecord> callRecords);
}
③报表
④分析
这里对于整道题共三部分进行分析,将类图拿出分析更加详细。将类图看懂对于解题过程能够更加具体顺畅。具体来说就是用户进行可以选择几种通话或者信息方式,再进行通信之后会留下记录,而对于这几种通信方式会有各自的扣费方式。再对其题目内容进行具体化——在座机通话前有一个固定费用,省内市内全国通话的费用计算都在题目中说的很清楚,写方法时需要注意计算的过程,尤其是对于通话时间的把控,不多不少刚刚好。同理,对于移动电话。一样的方法将市内、省内、国内的费用计算方法写清楚,注意通话时间的把控。其次重要的就是通话记录的保存!对于正则表达式的运用需要更加深入,对于其中时间的提取,非常关键。通话记录需要注意该通话是属于哪一块地方的,扣费方式属于哪一种。需要注意,再结合自己的记录方法以及费用计算方法,放到用户的记录中去,再进行费用的计算得到balance。最终对本题的问题进行解决。
⑤总结
对于本次大作业的三个部分与上一次大作业进行对比还是比较友好的。首先在类图方面,通过对类图的解析能够更快的了解题目意思,对于解题能够更快入手顺畅。
踩坑心得
1.这里不得不说一下题目集7中的第三题了,这里的调试了半天,因为过不去,实在想不到办法。明明结果是正确的,但是测试点的分数一直得不到。我对着已经过了室友的代码一行一行看,根本“看不出”问题,最后自己慢慢调试SCANNER的位置,通过了测试点。这题还是比较简单的,老师可能是为了让我们写代码更加规范化吧。我卡在这卡了还是蛮久的。
2.在题集7中的第二题,就是sort方法的重写了。需要自己去学习如何正确使用该方法根据集合中的类的属性的相应规则进行排序。
改进方法
这次的大作业中还是有很多冗余的代码没有进行更改。尤其是对于输入的内容进行判断划分时,写了很多判断的函数。这里还是有很多东西可以进行改进的。
总结
通过本次大作业,我对一些结构有了更加深入的了解。部分题目让我认识到自己编写的程序还是不够规范,总是会出现一些小问题是需要解决的。多态的使用以及如何正确使用接口,什么时候使用接口。更加了解之后,在编写代码时,能够大大的提高自己代码的复用性,避免一些不必要的乱起八糟的函数冗余。总体来说,大作业的完成能够让自己对程序完成的编写过程有自己的理解和自己的编程方法。
标签:return,第三次,double,ArrayList,callRecord,博客,new,public From: https://www.cnblogs.com/wudi123/p/16971518.html