blog3

1.   前言

12-15周,我们课上学习了状态模式,策略模式以及javafx可视化页面的练习,继续复习了继承,多态等前面的内容。

作业方面我们做了题目集8,9,10,分别做了电话收费0,1,3,三次的迭代,其中个人认为题目集10最简单,题目集9最难,题目8适中。

知识点方面,巩固了前面的继承,多态外,课堂作业里复习了拉姆达表达式,在写pta时学会了按照类图编写代码,用正则表达式判断时间,日期,电话号的合法性等,在javafx里巩固了按钮驱动,文本框输入处理等简单操作,在记分钟数时找到Math.Ceil函数的帮助免除了不足一分钟的情况的麻烦。

2.   设计与分析

题目集8 第一题

实现一个简单的电信计费程序: 假设南昌市电信分公司针对市内座机用户采用的计费方式: 月租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元。 每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。 格式:号码+英文空格符+总的话费+英文空格符+余额 每个用户一行,用户之间按号码字符从小到大排序。 错误处理: 输入数据中出现的不符合格式要求的行一律忽略。 建议类图: 参见图1、2、3,可根据理解自行调整:

 

 

 

 

 

是计费方式的抽象类: chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。 getMonthlyRent()方法用于返回月租(monthlyRent)。 UserRecords是用户记录类,保存用户各种通话、短信的记录, 各种计费规则将使用其中的部分或者全部记录。 其属性从上到下依次是: 市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、 市内接听电话、省内(不含市内)接听电话、省外接听电话的记录

 

图2中CommunicationRecord是抽象的通讯记录类: 包含callingNumber拨打号码、answerNumber接听号码两个属性。 CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。 CallRecord(通话记录类)包含属性: 通话的起始、结束时间以及 拨号地点的区号(callingAddressAreaCode)、接听地 点的区号(answerAddressAreaCode)。 区号用于记录在哪个地点拨打和接听的电话。对于座机,就是本机区号。如果是手机 号,则接打地点的区号和本机的开户地区会有差异。

 

图3是计费规则的相关类,这些类的核心方法是: calCost(ArrayList callRecords)。 该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。 输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。 LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别 是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。 (提示:可以从UserRecords类中获取各种类型的callRecords)。 后续扩展说明: 后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话 费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。 部分参考测试用例:

以下内容绿色部分为输入,紧接其后的黑色部分为输出。

u-079186300001 0

 t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25

end

079186300001 3.0 77.0

u-079186300001 0

 t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:26

end

079186300001 3.6 76.4

u-079186300001 0

u-079186300002 0

t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25

t-079186300002 058686330022 2022.1.3 11:00:25 2022.1.3 11:05:25

t-079186300001 079186300001 2022.1.3 12:00:25 2022.1.3 12:05:25

end

079186300001 3.5 76.5

079186300002 3.0 77.0

u-079186300002 0

u-079186300001 0

t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25

t-079186300002 058686330022 2022.1.3 11:00:25 2022.1.3 11:05:25

t-079186300001 079186300001 2022.1.3 12:00:25 2022.1.3 12:05:25

end

079186300001 3.5 76.5 079186300002 3.0 77.0

u-079186300002 0

u-079186300001 0

t-079186300001 079286300002 2022.1.3 10:00:25 2022.1.3 10:05:25

end

079186300001 1.5 78.5

079186300002 0.0 80.0

u-079186300002 0

u-079186300001 0

t-079186300001 079286300002 2022.1.3 10:00:25 2022.1.3 10:05:25

t-079186300001 079186300003 2022.1.3 10:00:25 2022.1.3 10:05:25

end

079186300001 2.0 78.0

079186300002 0.0 80.0

u-079186300002 0

u-079186300001 0

t-079186300001 079186300002 2022.1.3 22:00:25 2022.1.3 22:05:32

t-079186300001 079186300003 2022.1.3 10:00:25 2022.1.3 10:05:25

end

079186300001 1.1 78.9

 079186300002 0.0 80.0

u-079186300001

0 t-079186300001 079286330022 2022.1.3 12:50:00 2022.1.3 13:05:00

22

rr

end

079186300001 4.5 75.5

 

 

 

源码如下

import java.text.DecimalFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Scanner;

abstract class CallChargeRule extends ChargeRule {

    public abstract double calCost(ArrayList<CallRecord> callRecords);

}

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 ChargeMode {

    private ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {

        return chargeRules;

    }

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {

        this.chargeRules = chargeRules;

    }

}

abstract class ChargeRule {

}

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;

    }

}

class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

    @Override

    public double calCost(UserRecords userRecords) {

        double sum = 0;

        for (ChargeRule chargeRule : getChargeRules()) {

            if (chargeRule instanceof LandPhoneInCityRule) {

                sum += ((CallChargeRule) chargeRule).calCost(userRecords.getCallingInCityRecords());

            }

            if (chargeRule instanceof LandPhoneInProvinceRule) {

                sum += ((CallChargeRule) chargeRule).calCost(userRecords.getCallingInProvinceRecords());

            }

            if (chargeRule instanceof LandPhoneInlandRule) {

                sum += ((CallChargeRule) chargeRule).calCost(userRecords.getCallingInLandRecords());

            }

        }

        return sum;

    }

    @Override

    public double getMonthlyRent() {

        return monthlyRent;

    }

}

class LandPhoneInCityRule extends CallChargeRule{

    @Override

    public double calCost(ArrayList<CallRecord> callRecords) {

        double sum = 0;

        for (CallRecord callRecord : callRecords) {

            sum += 0.1 * Math.ceil((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime()) * 1.0 / (1000 * 60));

        }

        return sum;

    }

}

class LandPhoneInlandRule extends CallChargeRule {

    @Override

    public double calCost(ArrayList<CallRecord> callRecords) {

        double sum = 0;

        for (CallRecord callRecord : callRecords) {

            sum += 0.6 * Math.ceil((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime()) * 1.0 / (1000 * 60));

        }

        return sum;

    }

}

class LandPhoneInProvinceRule extends CallChargeRule{

    @Override

    public double calCost(ArrayList<CallRecord> callRecords) {

        double sum = 0;

        for (CallRecord callRecord : callRecords) {

            sum += 0.3 * Math.ceil((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime()) * 1.0 / (1000 * 60));

        }

        return sum;

    }

}

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String line;

        List<User> users = new ArrayList<>();

        List<String> numbers = new ArrayList<>();

        while (!"end".equals(line = sc.nextLine())) {

            if (line.startsWith("u-")) {

                User user = new User();

                String number = line.substring(line.indexOf("-") + 1, line.indexOf(" "));

                if (!isNumber(number)) continue;

                int type = Integer.parseInt(line.split(" ")[1]);

                if (type != 0 && type != 1 && type != 2) continue;

                if (numbers.contains(number)) continue;

                numbers.add(number);

                user.setNumber(number);

                ChargeMode chargeMode = new LandlinePhoneCharging();

                ArrayList<ChargeRule> chargeRules = new ArrayList<>(3);

                chargeRules.add(new LandPhoneInCityRule());

                chargeRules.add(new LandPhoneInProvinceRule());

                chargeRules.add(new LandPhoneInlandRule());

                chargeMode.setChargeRules(chargeRules);

                user.setChargeMode(chargeMode);

                users.add(user);

            }

            if (line.startsWith("t-")) {

                String callingNumber = null;

                String answerNumber = null;

                String callingAddressAreaCode = null;

                String answerAddressAreaCode = null;

                Date startTime = null;

                Date endTime = null;

                try {

                    String[] s = line.substring(line.indexOf("-") + 1).split(" ");

                    callingNumber = s[0];

                    answerNumber = s[1];

                    if (!isNumber(callingNumber) || !isNumber(answerNumber)) continue;

                    callingAddressAreaCode = s[0].substring(0, 4);

                    answerAddressAreaCode = s[1].substring(0, 4);

                    String startTimeStr = s[2] + " " + s[3];

                    String endTimeStr = s[4] + " " + s[5];

                    if (!isDateTime(startTimeStr) || !isDateTime(endTimeStr)) continue;

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

                    startTime = sdf.parse(startTimeStr);

                    endTime = sdf.parse(endTimeStr);

                    if (endTime.getTime() - startTime.getTime() < 0) continue;

                } catch (Exception e) {

                    continue;

                }

                CallRecord callRecord = new CallRecord();

                callRecord.setCallingNumber(callingNumber);

                callRecord.setAnswerNumber(answerNumber);

                callRecord.setCallingAddressAreaCode(callingAddressAreaCode);

                callRecord.setAnswerAddressAreaCode(answerAddressAreaCode);

                callRecord.setStartTime(startTime);

                callRecord.setEndTime(endTime);

                for (User user : users) {

                    String userNumber = user.getNumber();

                    String userAreaCode = userNumber.substring(0, 4);

                    if (userNumber.equals(callingNumber)) {

                        UserRecords userRecords = user.getUserRecords();

                        if (userAreaCode.equals(callRecord.getAnswerAddressAreaCode())) {

                            userRecords.addCallingInCityRecords(callRecord);

                        } else if (callRecord.getAnswerAddressAreaCode().matches("^(079[0-9])|(0701)$") && userAreaCode.matches("^(079[0-9])|(0701)$")) {

                            userRecords.addCallingInProvinceRecords(callRecord);

                        } else {

                            userRecords.addCallingInLandRecords(callRecord);

                        }

                    }

                    if (userNumber.equals(answerNumber)) {

                        UserRecords userRecords = user.getUserRecords();

                        if (userAreaCode.equals(callRecord.getAnswerAddressAreaCode())) {

                            userRecords.addAnswerInCityRecords(callRecord);

                        } else if (callRecord.getAnswerAddressAreaCode().matches("^(079[0-9])|(0701)$") && userAreaCode.matches("^(079[0-9])|(0701)$")) {

                            userRecords.addAnswerInProvinceRecords(callRecord);

                        } else {

                            userRecords.addAnswerInLandRecords(callRecord);

                        }

                    }

                }

            }

        }

     

        for (int i = 0; i < users.size() - 1; i++) {

            for (int j = 0; j < users.size() - 1 - i; j++) {

                User temp;

                if (users.get(j).getNumber().compareTo(users.get(j + 1).getNumber()) > 0) {

                    temp = users.get(j);

                    users.set(j, users.get(j + 1));

                    users.set(j + 1, temp);

                }

            }

        }

        for (User user : users) {

            String number = user.getNumber();

            double calCost = user.calCost();

            double balance = user.calBalance();

            System.out.println(number + " " + calCost + " " + balance);

        }

    }

 

    private static boolean isNumber(String s) {

        return s.matches("^(\\d){10,12}$");

    }

 

    private static boolean isDateTime(String datetime) {

        String[] date = datetime.split(" ");

        String[] s = date[0].split("\\.");

        String year = s[0];

        String month = s[1];

        String day = s[2];

        return date[0].matches("^(\\d){4}\\.(([1-9])|(1[0-2]))\\.(([1-9])|([1-3][1-9]))$") &&

                checkYearValidity(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)) && checkTimeValidity(date[1]);

    }

 

    public static boolean isLeapYear(int year) {

        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

    }

 

    public static boolean checkYearValidity(int year, int month, int day) {

        int[] leap = new int[]{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int[] noLeap = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        if (month > 0 && month <= 12) {

            if (isLeapYear(year)) {

                return day <= leap[month] && day > 0;

            } else {

                return day <= noLeap[month] && day > 0;

            }

        }

        return false;

    }

 

    public static boolean checkTimeValidity(String time) {

        return time.matches("^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$");

    }

 

}

class MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {

        return message;

    }

    public void setMessage(String message) {

        this.message = message;

    }

}

class User {

    private UserRecords userRecords = new UserRecords();

    private double balance = 100;

    private ChargeMode chargeMode;

    private String number;

 

    public double calBalance() {

        return balance - calCost() - chargeMode.getMonthlyRent();

    }

    public double calCost() {

        return Double.parseDouble(new DecimalFormat("#.00").format(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 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> sendMessageRecords = new ArrayList<>();

    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>();

    public void addCallingInCityRecords(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 answerRecord) {

        answerInCityRecords.add(answerRecord);

    }

    public void addAnswerInProvinceRecords(CallRecord answerRecord) {

        answerInProvinceRecords.add(answerRecord);

    }

    public void addAnswerInLandRecords(CallRecord answerRecord) {

        answerInLandRecords.add(answerRecord);

    }

    public void addSendMessageRecords(MessageRecord sendMessageRecord) {

        sendMessageRecords.add(sendMessageRecord);

    }

    public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) {

        receiveMessageRecords.add(receiveMessageRecord);

    }

    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> getSendMessageRecords() {

        return sendMessageRecords;

    }

    public ArrayList<MessageRecord> getReceiveMessageRecords() {

        return receiveMessageRecords;

    }

}

难点:按照类图模块化处理数据

类图

 

 

 

根据类图写代码,下面是本人提交列表

 

可见,76分的时候本人有很强烈的思想斗争,因两个测试点错误,耗费了非常长的时间

 

最后得出测试点12是接电话号码可以有十位

测试点25是时间月份的第一个数字不能是0如01 02  03…….

 

 

 

踩坑心得

 

对待非法输入时候时长没耐心琢磨,心静不下来导致的有些函数拼写错误及大小写搞混,由提交列表就可以看出本人代码急于求成,缺少实际经验的同时强调写作业效率,加上打字速度本来就慢,这导致函数常常出现拼写错误也多亏了编译器的报错,标红,让我一次次的修改,一次次的订正,一次次的反思,一次次的迭代,这告诫了我们要编程过程中要脚踏实地,宁愿降低自己的效率也要提高代码的可读性,降低代码的耦合性,让别人好找,自己好查代码存在的漏洞以及迭代的方法内容。

改进建议

本人认为本道题出的是有问题的,本题一边说时间必须符合"yyyy.MM.dd HH:mm:ss"格式,输出保留两位小数,但给的测试用例不这么搞,并且有些测试点还专门测试这些有问题的需求,要使其需求输入非法,我觉得这是有问题的选项。

 

 

 

 

 

题目集 9 第二题

7-2 sdut-Collection-sort--C~K的班级(II

分数 10

 

 

 

 

作者 周雪芹

单位 山东理工大学

经过不懈的努力,C~K终于当上了班主任。

现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?

输入格式:

第一行输入一个N,代表C~K导出的名单共有N行(N<100000).

接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。

输出格式:

第一行输出一个n,代表删除重复名字后C~K的班级共有几人。

接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。

 

 

 

源码如下

import java.util.*;

class Student {

       String id;

    String name;

       int age;

       char xingbie;

       public Student(String id, String name, int age, char xingbie) {

              super();

              this.id = id;

              this.name = name;

              this.age = age;

              this.xingbie = xingbie;

       }

       @Override

       public String toString() {

              return id + " " + name + " " + age + " " + xingbie;

       }

}

public class Main {

       public static void main(String[] args) {

              Scanner input = new Scanner(System.in);

              HashMap<String, Student> studentmap = new HashMap<String, Student>();

              int n = input.nextInt();

              while(n > 0) {

                     Student stu = new Student(input.next(), input.next(), input.nextInt(), input.next().charAt(0));

                     studentmap.put(stu.id, stu);

            n--;

              }

              Set<String> keySet = studentmap.keySet();

              ArrayList<String> list = new ArrayList<String>(keySet);

              System.out.println(studentmap.size());

        Collections.sort(list);

              for(String str:list) {

                     System.out.println(studentmap.get(str));

              }

    

              input.close();

       }

}

这里运用了拉姆达表达式解决该问题,保持了学号对应的姓名,年龄,性别的连接性是一个不错的解决方法,在复习拉姆达表达式的同时,也让我们体会到了拉姆达表达式的实用性,是一个非常不错的题目

 

 

踩坑心得

因本人拉姆达表达式不熟练,再加上时间原因,有些东西需要上网查才能写出来,所以我们在学习新知识时要及时巩固,复习,积极使用所学知识去解决相应问题,这对我们的学习,编程,自学能力有着举住轻重的作用。

改进措施

应增加类似题出现的频率,以让我们更加有利的应用新的知识去解决问题,在满足我们的成就感时也巩固了我们的知识能力,这会形成一个有利的正向反馈,以促进我们的编程能力进一步提高。

 

 

 

 

题目10 第二题

7-2 编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)

分数 30

 

 

 

 

作者 吴光生

单位 新余学院

编写一个类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箱

输入样例:

在这里给出一组输入。例如:

5

输出样例:

在这里给出相应的输出。例如:

使用了面值为
50
的购物券进行支付

牛奶还剩
4
箱

使用了面值为
100
的购物券进行支付

牛奶还剩
2
箱

 

 

 

 

代码如下

 

 

import java.util.Scanner;

class Shop {

       InnerCoupons coupons50;

       InnerCoupons coupons100;

    private int milkCount;

    Shop(){

           coupons50=new InnerCoupons(50);

           coupons100=new InnerCoupons(100);

    }

    void setMobileAmount(int milkCount){

           this.milkCount=milkCount;

    }

    int getMobileAmount(){

        return milkCount;

    }

    class InnerCoupons{

        int Value;

        InnerCoupons(int milkCount){

            this.Value = milkCount;

        }

        void buy(){

            if(Value>=50&&Value<100){

                   milkCount--;

                System.out.println("使用了面值为"+Value+"的购物券进行支付");

                System.out.println("牛奶还剩"+getMobileAmount()+"箱");

            }

            else if(Value<200&&Value>=100){

                   milkCount = milkCount-2;

                System.out.println("使用了面值为"+Value+"的购物券进行支付");

                System.out.println("牛奶还剩"+getMobileAmount()+"箱");

            }

        }

    }

}

public class Main{

    public static void main(String arg[]){

        Scanner input=new Scanner(System.in);

        Shop shop = new Shop();

        shop.setMobileAmount(input.nextInt());

        shop.coupons50.buy();

        shop.coupons100.buy();

     

    }

}

 

运用题目需求,编写类图,自己设计,自主使用,该题要求利用题目需求,按要求编写类,对象。

踩坑心得

一开始对题目的50 100 代金券有点看不懂(包括循序,使用及怎么实现都很迷茫),但经过小伙伴的帮助以及网络的支持,已按照要求完成任务。

改进建议

建议在使用代金券的时候多一点提醒。

总结:

通过电信计费让我更加懂得了如何使用正则表达式控制输入,及如何记录时间及计算时间间隔,会用新的方法处理不满一分钟的情况等,
对类的设计也更加合理,相比从前编程的逻辑也更加清晰,类的使用更加顺手,处理类间关系时更加完善。
对容器的有关用法更加熟练,使用继承关系,编写抽象方法,不同子类实现抽象方法,对这类的编写。

 

改进方面:要尽快理解各函数的意思,提升对不同的函数的敏感程度,要积极复习前面的内容避免遗忘。

 

 

改进建议:课堂作业的每一次迭代后建议发统一的代码,避免有些特定情况下的代码丢失以及当前代码不满足下一次的迭代必要的复用性。

posted @ 2022-06-16 00:29  21206118刘世越  阅读(153)  评论(0)    收藏  举报