南昌航空大学 软件学院 pta Java 第八次作业 蔡珂 opp
最后一次哩
题目列表
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(){
return answerAddressAreaCode;
}
void setStartTime(Date startTime){
this.startTime = startTime;
}
void setEndTime(Date endTime){
this.endTime = endTime;
}
void setCallingAddressAreaCode(String callingAddressAreaCode){
this.callingAddressAreaCode=callingAddressAreaCode;
}
void setAnswerAddressAreaCode(String answerAddressAreaCode){
this.answerAddressAreaCode =answerAddressAreaCode;
}
}
abstract class ChargeMode {
ArrayList<ChargeRule>chargeRules =new ArrayList<ChargeRule>();
ArrayList<ChargeRule> getChargeRules(){
return chargeRules;
}
void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = (ArrayList<ChargeRule>) (chargeRules.clone());
}
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
}
class Cmp implements Comparator<User> {
@Override
public int compare(User U1, User U2) {
return U1.getNumber().compareTo(U2.getNumber());
}
}
abstract class CommunicationRecord {
String callingNumer,answerNumer;
void setCallingNumer(String callingNumer){
this.callingNumer=callingNumer;
}
void setAnswerNumer(String answerNumer){
this.answerNumer =answerNumer;
}
String getCallingNumer(){
return callingNumer;
}
String getAnswerNumer(){
return answerNumer;
}
}
abstract class ChargeRule {
}
class LandlinePhoneCharging extends ChargeMode {
double monthlyRent =20;
public double calCost(UserRecords userRecords){
double sum=0;
for (CallRecord callRecord:userRecords.getCallingInCityRecords())
sum+= new LandPhoneInCityRule().calCost(callRecord);
for (CallRecord callRecord:userRecords.getCallingInLandRecords())
sum+= new LandPhoneInLandRule().calCost(callRecord);
for (CallRecord callRecord:userRecords.getCallingInProvinceRecords())
sum+= new LandPhoneInProvinceRule().calCost(callRecord);
return sum;
}
public double getMonthlyRent(){
return monthlyRent;
}
}
class LandPhoneInCityRule 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.1;
}
}
class MobilePhoneCharging extends ChargeMode {
private double monthlyRent = 15;
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord callRecord:userRecords.getCallingInCityRecords())
sum+= new MobilePhoneInlandRule().calCost(callRecord);
for (CallRecord callRecord:userRecords.getCallingInLandRecords())
sum+= new MobilePhoneInlandRule().calCost(callRecord);
for (CallRecord callRecord:userRecords.getCallingInProvinceRecords())
sum+= new MobilePhoneInProvinceRule().calCost(callRecord);
for(CallRecord callRecord:userRecords.getCallingroamlnProvinceRecords())
sum+= new MobilePhoneRoamInProvinceRule().calCost(callRecord);
for(CallRecord callRecord:userRecords.getCallingroamlnLandRecords())
sum+= new MobilePhoneRoamInLandRule().calCost(callRecord);
for(CallRecord callRecord:userRecords.getAnswerroamlnLandRecords())
sum+= new MobilePhoneRoamInLandRule().calCostans(callRecord);
return sum;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class MessageRecord extends CommunicationRecord{
String message;
String getMessage(){
return message;
}
void setMessage(String message){
this.message=message;
}
}
class MobilePhoneInlandRule extends CallChargeRule{
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 MobilePhoneInProvinceRule extends CallChargeRule{
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.2;
}
}
class MobilePhoneRoamInLandRule extends CallChargeRule{
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;
}
public double calCostans(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;
}
//System.out.printf(minu+"\n");
return minu * 0.3;
}
}
class MobilePhoneRoamInProvinceRule extends CallChargeRule{
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 UserRecords {
ArrayList<CallRecord> callinglnCityRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> callinglnProvinceRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> callinglnLandRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> callingroamlnProvinceRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> callingroamlnLandRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> answerlnCityRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> answerlnProvinceRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> answerlnLandRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> answerroamlnProvinceRecords= new ArrayList<CallRecord>();
ArrayList<CallRecord> answerroamlnLandRecords= new ArrayList<CallRecord>();
ArrayList<MessageRecord> sendMessageRecords= new ArrayList<MessageRecord>();
ArrayList<MessageRecord> receiveMessageRecords= new ArrayList<MessageRecord>();
void addCallingInCityRecords(CallRecord callRecord){
callinglnCityRecords.add(callRecord);
}
void addCallingInProvinceRecords(CallRecord callRecord){
callinglnProvinceRecords.add(callRecord);
}
void addCallingInLandRecords(CallRecord callRecord){
callinglnLandRecords.add(callRecord);
}
void addCallingRoamInProvinceRecords(CallRecord callRecord){
callingroamlnProvinceRecords.add(callRecord);
}
void addCallingRoamInLandRecords(CallRecord callRecord){
callingroamlnLandRecords.add(callRecord);
}
void addAnswerInCityRecords(CallRecord callRecord){
answerlnCityRecords.add(callRecord);
}
void addAnswerInProvinceRecords(CallRecord callRecord){
answerlnProvinceRecords.add(callRecord);
}
void addAnswerInLandRecords(CallRecord callRecord){
answerlnLandRecords.add(callRecord);
}
void addAnswerRoamInProvinceRecords(CallRecord callRecord){
answerroamlnProvinceRecords.add(callRecord);
}
void addAnswerRoamInLandRecords(CallRecord callRecord){
answerroamlnLandRecords.add(callRecord);
}
void addSendMessageRecords(MessageRecord messageRecord){
sendMessageRecords.add(messageRecord);
}
void addReceiveMessageRecords(MessageRecord messageRecord){
receiveMessageRecords.add(messageRecord);
}
ArrayList<CallRecord> getCallingInCityRecords(){
return callinglnCityRecords;
}
ArrayList<CallRecord> getCallingInProvinceRecords(){
return callinglnProvinceRecords;
}
ArrayList<CallRecord> getCallingInLandRecords(){
return callinglnLandRecords;
}
ArrayList<CallRecord> getCallingroamlnProvinceRecords(){return callingroamlnProvinceRecords;}
ArrayList<CallRecord> getCallingroamlnLandRecords(){return callingroamlnLandRecords;}
ArrayList<CallRecord> getAnswerroamlnProvinceRecords(){return answerroamlnProvinceRecords;}
ArrayList<CallRecord> getAnswerroamlnLandRecords(){return answerroamlnLandRecords;}
ArrayList<CallRecord> getAnswerInCityRecords(){
return answerlnCityRecords;
}
ArrayList<CallRecord> getAnswerInProvinceRecords(){
return answerlnProvinceRecords;
}
ArrayList<CallRecord> getAnswerInLandRecords(){
return answerlnLandRecords;
}
ArrayList<MessageRecord> getSendMessageRecords(){
return sendMessageRecords;
}
ArrayList<MessageRecord> getReceiveMessageRecords(){
return receiveMessageRecords;
}
}
class MobilePhoneInCityRule extends CallChargeRule{
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.1;
}
}
class User {
UserRecords userRecords = new UserRecords();
double balance =100;
ChargeMode chargeMode;
String number;
int masnum;
public User(){}
public User(String number){
masnum=0;
this.number = number;
}
double calBalance(){
return 100-calCost();
}
double calCost(){
if(masnum<=3)
return 0.1*masnum;
else if(masnum<=5)
return 0.3+(masnum-3)*0.2;
else
return 0.7+(masnum-5)*0.3;
}
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;
}
}
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箱
import java.util.*;
//主函数
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
Shop shop = new Shop();
shop.num = num;
shop.innerCoupons.setValue(50);
shop.innerCoupons.buy();
shop.innerCoupons.setValue(100);
shop.innerCoupons.buy();
}
}
class Shop{
int num;
InnerCoupons innerCoupons;
class InnerCoupons{
int value;
void setValue(int value){
this.value=value;
}
void buy(){
num -= value/50;
System.out.printf("使用了面值为"+value+"的购物券进行支付" +
"\n" +
"牛奶还剩"+num+"箱\n");
}
}
public Shop(){
innerCoupons = new InnerCoupons();
};
}
7-3 动物发声模拟器(多态)
设计一个动物发生模拟器,用于模拟不同动物的叫声。比如狮吼、虎啸、狗旺旺、猫喵喵……。
定义抽象类Animal,包含两个抽象方法:获取动物类别getAnimalClass()、动物叫shout();
然后基于抽象类Animal定义狗类Dog、猫类Cat和山羊Goat,用getAnimalClass()方法返回不同的动物类别(比如猫,狗,山羊),用shout()方法分别输出不同的叫声(比如喵喵、汪汪、咩咩)。
最后编写AnimalShoutTest类测试,输出:
猫的叫声:喵喵
狗的叫声:汪汪
山羊的叫声:咩咩
其中,在AnimalShoutTestMain类中,用speak(Animal animal){}方法输出动物animal的叫声,在main()方法中调用speak()方法,分别输出猫、狗和山羊对象的叫声。
请在下面的【】处添加代码。
class Main {
public static void main(String[] args) {
Animal cat = new Cat();
Animal dog = new Dog();
Animal goat = new Goat();
speak(cat);
speak(dog);
speak(goat);
}
//定义静态方法speak()
static void speak(Animal animal){
animal.getSpeak();
}
}
abstract class Animal{
abstract void getSpeak();
}
//定义抽象类Animal
class Cat extends Animal{
String speak ;
public Cat(){speak="喵喵";}
void getSpeak(){
System.out.printf("猫的叫声:"+speak+"\n");
}
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
String speak ;
public Dog(){speak="汪汪";}
void getSpeak(){
System.out.printf("狗的叫声:"+speak+"\n");
}
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
String speak;
public Goat(){
speak="咩咩";
}
void getSpeak(){
System.out.printf("山羊的叫声:"+speak+"\n");
}
}