OOP作业总结二

一、前言

  本次题目集的知识点包括了对java封装性,以及类与类之间的关系,例如依赖,组合,聚合的考察,同时也考察了对哈希集和正则表达式的运用。总体来说知识点较多,难度也偏大,所以题量对比上一次要多一些,难度方面也有所上升。但个人认为,题量和难度的提升是很有必要的,因为只有通过不断地练习,才能够达到巩固的效果。特别是作为一个初学者,面对有挑战难度的知识点,更应该沉下心来好好思考,解决问题。

二、设计与分析

1.菜单计价程序-3(题目集4的7-1)

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish\[\] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号\\

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)\\

int getPrice()//计价,计算本条记录的价格\\

}

订单类:保存用户点的所有菜的信息。

Order {

Record\[\] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

delARecordByOrderNum(int orderNum)//根据序号删除一条记录

findRecordByNum(int orderNum)//根据序号查找一条记录

}

### 输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

### 输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+”:”

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

类图如下:

 

 

 

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

2.日期问题面向对象设计(聚合一)(题目集5的7-5)

参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

  本题主要考察了对类与类之间聚合关系的理解,一共设计了五个类,包括Year、Month、Day、DataUtil、Main。其中Year类、Month类、Day类和DataUtil四个类,前一个类和后一个类之间存在着组合关系。每个类中都有着相关的成员变量和成员方法,体现了封装性。在编程时由于存在多层聚合关系,调用类的方法时时常采用链式编程能够提高效率。

 

复制代码
import java.util.Scanner;

//定义一个年类
class Year{

    private int value;

    public Year() {
    }

    public Year(int value) {
        this.value = value;
    }


    public int getValue() {
        return value;
    }


    public void setValue(int value) {
        this.value = value;
    }

    public boolean isLeapYear(){
        if(this.value % 400 == 0)
            return true;
        if(this.value % 4 == 0 && this.value % 100 != 0)
            return true;
        return false;
    }
    public boolean validate(){
        if(this.value < 1900 || this.value > 2050){
            return false;
        }else{
            return true;
        }
    }
    public void yearIncrement(){
        this.value++;
    }
    public void yearReduction(){
        this.value--;
    }
}
//定义一个月类
class Month{
    private int value;
    private Year year;

    public Month() {
    }

    public Month(int yearValue, int monthValue) {
        this.value = monthValue;
        year = new Year(yearValue);//new一个年对象并调用它的有参构造方法
    }

    /**
     * 获取
     * @return value
     */
    public int getValue() {
        return value;
    }

    /**
     * 设置
     * @param value
     */
    public void setValue(int value) {
        this.value = value;
    }

    /**
     * 获取
     * @return year
     */
    public Year getYear() {
        return year;
    }

    /**
     * 设置
     * @param year
     */
    public void setYear(Year year) {
        this.year = year;
    }

    public void resetMin(){
        this.value = 1;
    }
    public void resetMax(){
        this.value = 12;
    }
    public boolean validate(){
        if(this.value < 1 || this.value > 12){
            return false;
        }else{
            return true;
        }
    }
    public void monthIncrement(){
        if(this.value < 12){
            this.value++;
        }else{
            this.getYear().yearIncrement();
            this.resetMin();
        }

    }
    public void monthReduction(){
        if(this.value == 1){
            this.getYear().yearReduction();
            this.resetMax();
        }else{
            this.value--;
        }

    }
}
class Day{
    private int value;
    private Month month;
    private int[] mon_maxnum = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public Day() {
    }

    public Day(int yearValue, int monthValue, int dayValue) {
        this.setValue(dayValue);
        month = new Month(yearValue, monthValue);//new一个月对象并调用它的有参构造
    }

    /**
     * 获取
     * @return value
     */
    public int getValue() {
        return value;
    }

    /**
     * 设置
     * @param value
     */
    public void setValue(int value) {
        this.value = value;
    }

    /**
     * 获取
     * @return month
     */
    public Month getMonth() {
        return month;
    }

    /**
     * 设置
     * @param month
     */
    public void setMonth(Month month) {
        this.month = month;
    }

    /**
     * 获取
     * @return mon_maxnum
     */

    public void resetMin(){
        this.value = 1;
    }
    public void resetMax(){
        if (month.getYear().isLeapYear()) {
            mon_maxnum[1] = 29;
        } else {
            mon_maxnum[1] = 28;
        }
        this.value = mon_maxnum[this.month.getValue()-1];
    }
    public boolean validate(){
        if (month.getYear().isLeapYear()) {
            mon_maxnum[1] = 29;
        } else {
            mon_maxnum[1] = 28;
        }
         if(month.validate() && this.value >= 1 && this.value <= this.mon_maxnum[this.getMonth().getValue() - 1]){
             return true;
         }else{
             return false;
         }
    }
    public void dayIncrement(){
        if (month.getYear().isLeapYear()) {
            mon_maxnum[1] = 29;
        } else {
            mon_maxnum[1] = 28;
        }
        if(this.value < this.mon_maxnum[this.month.getValue() - 1]){
            this.value++;
        }else{
            this.resetMin();
            this.getMonth().monthIncrement();
        }
    }
    public void dayReduction(){
        if(this.value == 1){
            this.getMonth().monthReduction();//减到第一天重置为最大天数,同时月份减一
            this.resetMax();

        }else{
            this.value--;
        }
    }
}
class DateUtil {
    private Day day;

    public DateUtil() {
    }

    public DateUtil(int d, int m, int y) {
        day = new Day(y, m, d);
    }

    /**
     * 获取
     *
     * @return day
     */
    public Day getDay() {
        return day;
    }

    /**
     * 设置
     *
     * @param //day
     */
    public void setDay(Day day) {
        this.day = day;
    }

    public boolean checkInputValidity() {
        if (this.day.getMonth().validate() && this.day.validate() && this.day.getMonth().getYear().validate()) {
            return true;
        } else {
            return false;
        }
    }

    public boolean compareDates(DateUtil date) {
        if (this.day.getMonth().getYear().getValue() > date.day.getMonth().getYear().getValue())
            return true;
        else if (this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && this.day.getMonth().getValue() > date.day.getMonth().getValue())
            return true;
        else if (this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && this.day.getMonth().getValue() == date.day.getMonth().getValue() && this.day.getValue() > date.day.getValue())
            return true;
        return false;
    }

    public boolean equalTwoDates(DateUtil date) {
        if (this.day.getMonth().getYear().getValue() != date.day.getMonth().getYear().getValue()
                || this.day.getMonth().getValue() != date.day.getMonth().getValue()
                || this.day.getValue() != date.day.getValue())
            return false;
        else
            return true;
    }

    public String showDate() {
        return this.day.getMonth().getYear().getValue() + "-" + this.day.getMonth().getValue() + "-" + this.day.getValue();
    }

    public DateUtil getNextNDays(int n) {
        while (n > 0) {
            this.day.dayIncrement();
            n--;
        }
        return this;
    }


    public DateUtil getPreviousNDays(int n) {
        while (n > 0) {
            this.day.dayReduction();
            n--;
        }
        return this;
    }
    public int getDaysofDates (DateUtil date){
        int res = 0;
        while (!equalTwoDates(date)) {
            if (compareDates(date)) {
                date.getDay().dayIncrement();
            }else{
                date.getDay().dayReduction();
            }
            res++;
        }
        return res;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(day, month, year);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(day, month, year);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

//
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil date1 = new DateUtil(day, month, year);
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil date2 = new DateUtil(day, month, year);
            if (!date1.checkInputValidity() || !date2.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date1.getDaysofDates(date2));

        }
    }
}
View Code
复制代码

 

 

 

3.日期问题面向对象设计(聚合二)(题目集5的7-6)

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

本题作为上一题的延申,优化了多层聚合关系,令DateUtil类分别和year类、month类、day类聚合,这样在调用类的方法时更加方便。

复制代码
import java.util.Scanner;

//定义一个年类
class Year{

    private int value;

    public Year() {
    }

    public Year(int value) {
        this.value = value;
    }


    public int getValue() {
        return value;
    }


    public void setValue(int value) {
        this.value = value;
    }

    public boolean isLeapYear(){
        if(this.value % 400 == 0)
            return true;
        if(this.value % 4 == 0 && this.value % 100 != 0)
            return true;
        return false;
    }
    public boolean validate(){
        if(this.value < 1820 || this.value > 2020){
            return false;
        }else{
            return true;
        }
    }
    public void yearIncrement(){
        this.value++;
    }
    public void yearReduction(){
        this.value--;
    }
}
//定义一个月类
class Month{
    private int value;

    public Month() {
    }

    public Month(int Value) {
        this.value = Value;
    }

    /**
     * 获取
     * @return value
     */
    public int getValue() {
        return value;
    }

    /**
     * 设置

     */
    public void setValue(int value) {
        this.value = value;
    }



    public void resetMin(){
        this.value = 1;
    }
    public void resetMax(){
        this.value = 12;
    }
    public boolean validate(){
        if(this.value < 1 || this.value > 12){
            return false;
        }else{
            return true;
        }
    }
    public void monthIncrement(){
        this.value++;
    }
    public void monthReduction(){
        this.value--;
    }
}
class Day{
    private int value;

    public Day() {
    }

    public Day(int Value) {
        this.setValue(Value);

    }

    /**
     * 获取
     * @return value
     */
    public int getValue() {
        return value;
    }

    /**
     * 设置
     * @param value
     */
    public void setValue(int value) {
        this.value = value;
    }
    public void dayIncrement(){
        this.value++;
    }
    public void dayReduction(){
        this.value--;
    }
}
class DateUtil {
    private Day day;
    private Month month;
    private Year year;
    private int[] mon_maxnum = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public DateUtil() {
    }

    public DateUtil(int y, int m, int d) {
        year = new Year(y);
        month = new Month(m);
        day = new Day(d);
    }


    /**
     * 获取
     *
     * @return day
     */
    public Day getDay() {
        return day;
    }

    /**
     * 设置
     *
     * @param //day
     */
    public void setDay(Day day) {
        this.day = day;
    }
    public void setDayMin(){
        day.setValue(1);
    }
    public void setDayMax(){
        if (year.isLeapYear()) {
            mon_maxnum[1] = 29;
        } else {
            mon_maxnum[1] = 28;
        }
        day.setValue(mon_maxnum[this.month.getValue()-1]);
    }

    public boolean checkInputValidity() {
        if (year.validate() && month.validate()) {
            if (year.isLeapYear()) {
                mon_maxnum[1] = 29;
            }else{
                mon_maxnum[1] = 28;
            }
            if(month.validate() && day.getValue() >= 1 && day.getValue() <= this.mon_maxnum[month.getValue() - 1]){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    public boolean compareDates(DateUtil date) {
        if (year.getValue() > date.year.getValue())
            return true;
        else if (year.getValue() == date.year.getValue() && month.getValue() > date.month.getValue())
            return true;
        else if (year.getValue() == date.year.getValue() && month.getValue() == date.month.getValue() && this.day.getValue() > date.day.getValue())
            return true;
        return false;
    }

    public boolean equalTwoDates(DateUtil date) {
        if (year.getValue() != date.year.getValue()
                || this.month.getValue() != date.month.getValue()
                || this.day.getValue() != date.day.getValue())
            return false;
        else
            return true;
    }

    public String showDate() {
        return year.getValue() + "-" + month.getValue() + "-" + this.day.getValue();
    }

    public DateUtil getNextNDays(int n) {
        while (n > 0) {
            if (year.isLeapYear()) {
                mon_maxnum[1] = 29;
            }else{
                mon_maxnum[1] = 28;
            }
            if(day.getValue() < this.mon_maxnum[this.month.getValue() - 1]){
                day.dayIncrement();
            }else{
                this.setDayMin();
                if(month.getValue() < 12){
                    month.monthIncrement();
                }else{
                    month.resetMin();
                    year.yearIncrement();
                }
            }
                n--;
        }
        return this;
    }
    public DateUtil getPreviousNDays(int n) {
        while (n > 0) {
            if(day.getValue() == 1){
                if(month.getValue() > 1){
                    month.monthReduction();
                    this.setDayMax();
                }else{
                    year.yearReduction();
                    month.resetMax();
                    day.setValue(31);
                }
            }else{
                if (year.isLeapYear()) {
                    mon_maxnum[1] = 29;
                }else{
                    mon_maxnum[1] = 28;
                }
                day.dayReduction();
            }
            n--;
        }
        return this;
    }
    public int getDaysofDates (DateUtil date){
        int res = 0;
        while (!equalTwoDates(date)) {
            if (date.year.isLeapYear()) {
                date.mon_maxnum[1] = 29;
            }else{
                date.mon_maxnum[1] = 28;
            }
            if (compareDates(date)) {
                if(date.day.getValue() < date.mon_maxnum[date.month.getValue() - 1]){
                    date.day.dayIncrement();
                }else{
                    date.setDayMin();
                    if(date.month.getValue() < 12){
                        date.month.monthIncrement();
                    }else{
                        date.month.resetMin();
                        date.year.yearIncrement();
                        if (date.year.isLeapYear()) {
                            date.mon_maxnum[1] = 29;
                        }else{
                            date.mon_maxnum[1] = 28;
                        }
                    }
                }
            }else{
                if(date.day.getValue() == 1){
                    if(date.month.getValue() > 1){
                        date.month.monthReduction();
                        date.setDayMax();
                    }else{
                        date.year.yearReduction();
                        date.month.resetMax();
                        date.day.setValue(31);
                    }
                }else{
                    if (date.year.isLeapYear()) {
                        date.mon_maxnum[1] = 29;
                    }else{
                        date.mon_maxnum[1] = 28;
                    }
                    date.day.dayReduction();
                }
            }
            res++;
        }
        return res;
    }

    /**
     * 获取
     * @return month
     */
    public Month getMonth() {
        return month;
    }

    /**
     * 设置
     * @param month
     */
    public void setMonth(Month month) {
        this.month = month;
    }

    /**
     * 获取
     * @return year
     */
    public Year getYear() {
        return year;
    }

    /**
     * 设置
     * @param year
     */
    public void setYear(Year year) {
        this.year = year;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

//
            System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil date1 = new DateUtil(year, month, day);
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil date2 = new DateUtil(year, month, day);
            if (!date1.checkInputValidity() || !date2.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.println("The days between " + date1.showDate() +
                    " and " + date2.showDate() + " are:"
                    + date1.getDaysofDates(date2));

        }else{
            System.out.println("Wrong Format");
        }
    }
}
View Code
复制代码

 

4.ATM机类结构设计(一)(题目集6的7-4)

编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能。尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设 计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进 行扩充和完善。

设计要求 (1)务必注意本题目中的各实体类之间的关系,尤其是一对多的组合关系。 (2)对实体类的设计要做到单一职责原则,且不能缺少规定的实体类。 (3)编程时考虑面向对象中的封装性本题目中的应用以及是否有能够扩展 的衍生需求。

类图如下:

 本题主要考察了对类图的设计与实现,需要一定的抽象能力,要能够处理好类与类之间的关系,这样才能够设计出色的类图。

复制代码
import java.util.Scanner;
import java.util.ArrayList;

//Bank类
class Bank {
    String bankname;
    ArrayList<String> ATMList;
    //创建无参构造方法
    public Bank(){
    }
    //创建带参构造方法
    public Bank(String bankname,ArrayList<String> ATMList)
    {
        this.bankname=bankname;
        this.ATMList=ATMList;
    }
    //getter
    public String getBankname() {
        return bankname;
    }
    public ArrayList<String> getATMList() {
        return ATMList;
    }
    //setter
    public void setBankname(String bankname){
        this.bankname=bankname;
    }
    public void setATMList(ArrayList<String> tATMList){
        this.ATMList=ATMList;
    }
}
//Account类
class Account {
    private String name;
    private String account;
    private String password;
    private double balance;
    ArrayList<Bank> banklist;
    Bank bank;
    private  ArrayList<String> cardList;
    //创建无参构造方法
    public Account(){
    }
    //创建带参构造方法
    public Account(String name,String account,String password,double balance,ArrayList<Bank> banklist,Bank bank,ArrayList<String> cardList){
        this.name=name;
        this.account=account;
        this.password=password;
        this.balance=balance;
        this.bank=bank;
        this.banklist=banklist;
        this.cardList=cardList;
    }
    //getter
    public String getName() {
        return name;
    }
    public String getAccount() {
        return account;
    }
    public String getPassword() {
        return password;
    }
    public double getBalance() {
        return balance;
    }
    public ArrayList<String> getCardList() {
        return cardList;
    }
    //setter
    public void setName(String name) {
        this.name = name;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public void setCardList(ArrayList<String> cardList) {
        this.cardList = cardList;
    }
}

//存取款的检查类
class Check {
    ArrayList<Account> accountList;
    String card;
    String password;
    String number;
    double money;

    public Check(ArrayList<Account> accountList,String card,String password,String number,double money){
        this.accountList=accountList;
        this.card=card;
        this.password=password;
        this.number=number;
        this.money=money;
    }

    public boolean check(){
        int flag=0;
        int i,j;
        int k=0;
        //检查账号是否正确
        for(i=0;i<accountList.size();i++){
            for(j=0;j<accountList.get(i).getCardList().size();j++){
                if (card.equals(accountList.get(i).getCardList().get(j))){
                    flag=1;
                    k=i;
                    break;
                }
            }
            if(flag==1){
                break;
            }
        }
        //检查密码是否正确
        if(flag==1){
            if(password.equals(accountList.get(k).getPassword())){
                flag=2;
            }
            else{
                System.out.println("Sorry,your password is wrong.");//银行卡密码错误
                return false;
            }
        }
        else{
            System.out.println("Sorry,this card does not exist.");//卡号不存在
            return false;
        }
        //检查ATM机编号是否正确
        if(flag==2){
            for(i=0;i<accountList.get(k).banklist.size();i++){
                for(j=0;j<accountList.get(k).banklist.get(i).ATMList.size();j++){
                    if(number.equals(accountList.get(k).banklist.get(i).ATMList.get(j))){
                        flag=3;
                        break;
                    }
                }
            }
        }
        //检查金额是否正确
        if(flag==3){
            if(money<=accountList.get(k).getBalance()){
                flag=4;
            }
            else{
                System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额
                return false;
            }
        }
        else{
            System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在
            return false;

        }
        //检查是否跨行
        if(flag==4){
            for(i=0;i<accountList.get(k).bank.ATMList.size();i++){
                if(number.equals(accountList.get(k).bank.ATMList.get(i))){
                    flag=5;
                    break;
                }
            }
        }
        if(flag!=5){
            System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款
            return false;
        }
        else
            return true;
    }

}
//余额查询的检查类
class Check1 {
    ArrayList<Account> accountList;
    String card;

    public Check1(ArrayList<Account> accountList, String card){
        this.accountList = accountList;
        this.card = card;
    }

    public boolean check(){
        int i,j;
        int flag=0;
        //卡号校验
        for(i=0;i<accountList.size();i++){
            for(j=0;j<accountList.get(i).getCardList().size();j++){
                if (card.equals(accountList.get(i).getCardList().get(j))){
                    flag=1;
                    break;
                }
            }
            if(flag==1){
                break;
            }
        }
        if(flag==1)
            return true;
        else{
            System.out.println("Sorry,this card does not exist.");//卡号不存在
            return false;
        }
    }
}

//存取款类
class Access{
    ArrayList<Account> accountList;
    String card;
    String password;
    String number;
    double money;
    public Access(ArrayList<Account> accountList,String card,String password,String number,double money){
        this.password=password;
        this.number=number;
        this.card=card;
        this.accountList=accountList;
        this.money=money;
    }
    public void access(){
        int i,j;
        int t=0;
        //卡号校验
        for(i=0;i<accountList.size();i++){
            for(j=0;j<accountList.get(i).getCardList().size();j++){
                if(card.equals(accountList.get(i).getCardList().get(j))){
                    t=i;
                    break;
                }
            }
        }
        accountList.get(t).setBalance(accountList.get(t).getBalance()-money);
    }
}

//存取款的展示类
class Show{
    ArrayList<Account> accountList;
    String card;
    String password;
    String number;
    double money;
    public Show(ArrayList<Account> accountList,String card,String password,String number,double money){
        this.password=password;
        this.number=number;
        this.card=card;
        this.accountList=accountList;
        this.money=money;
    }
    public void show(){
        int i,j;
        int t=0;
        //卡号校验
        for(i=0;i<accountList.size();i++){
            for(j=0;j<accountList.get(i).getCardList().size();j++){
                if(card.equals(accountList.get(i).getCardList().get(j))){
                    t=i;
                    break;
                }
            }
        }

        if(money>=0){//取款
            System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥%.2f\n",money);
        }
        else{
            money=-money;
            System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上存款¥%.2f\n",money);
        }
        System.out.printf("当前余额为¥%.2f\n",accountList.get(t).getBalance());
    }
}
//余额查询的展示类
class Show1{
    ArrayList<Account> accountList;
    String card;
    public Show1(ArrayList<Account> accountList,String card){
        this.accountList=accountList;
        this.card=card;
    }
    public void show1(){
        int i,j;
        int t=0;
        //卡号校验
        for(i=0;i<accountList.size();i++){
            for(j=0;j<accountList.get(i).getCardList().size();j++){
                if(card.equals(accountList.get(i).getCardList().get(j))){
                    t=i;
                    break;
                }
            }
        }
        System.out.printf("¥%.2f\n",accountList.get(t).getBalance());
    }
}

//主类
public class Main {
    public static void main(String[] args) {
        //ATM机编号数组
        ArrayList<String> ATMList1 = new ArrayList<>();
        ATMList1.add("01");
        ATMList1.add("02");
        ATMList1.add("03");
        ATMList1.add("04");
        Bank jsyh = new Bank("中国建设银行", ATMList1);
        ArrayList<String> ATMList2 = new ArrayList<>();
        ATMList2.add("05");
        ATMList2.add("06");
        Bank gsyh = new Bank("中国工商银行", ATMList2);
        //银行数组
        ArrayList<Bank> bankList = new ArrayList<>();
        bankList.add(jsyh);
        bankList.add(gsyh);
        //用户数组
        ArrayList<String> cardList1 = new ArrayList<>();
        cardList1.add("6217000010041315709");
        cardList1.add("6217000010041315715");
        Account account1 = new Account("杨过", "3217000010041315709", "88888888", 10000.00, bankList, jsyh,cardList1);

        ArrayList<String> cardList2 = new ArrayList<>();
        cardList2.add("6217000010041315718");
        Account account2 = new Account("杨过", "3217000010041315715", "88888888", 10000.00, bankList,jsyh,cardList2);

        ArrayList<String> cardList3 = new ArrayList<>();
        cardList3.add("6217000010051320007");
        Account account3 = new Account("郭靖", "3217000010051320007", "88888888", 10000.00, bankList,jsyh,cardList3);

        ArrayList<String> cardList4 = new ArrayList<>();
        cardList4.add("6222081502001312389");
        Account account4 = new Account("张无忌", "3222081502001312389", "88888888", 10000.00, bankList,gsyh,cardList4);

        ArrayList<String> cardList5 = new ArrayList<>();
        cardList5.add("6222081502001312390");
        Account account5 = new Account("张无忌", "3222081502001312390", "88888888", 10000.00, bankList,gsyh,cardList5);

        ArrayList<String> cardList6 = new ArrayList<>();
        cardList6.add("6222081502001312399");
        cardList6.add("6222081502001312400");
        Account account6 = new Account("张无忌", "3222081502001312399", "88888888", 10000.00, bankList,gsyh,cardList6);

        ArrayList<String> cardList7 = new ArrayList<>();
        cardList7.add("6222081502051320785");
        Account account7 = new Account("韦小宝", "3222081502051320785", "88888888", 10000.00, bankList,gsyh,cardList7);

        ArrayList<String> cardList8 = new ArrayList<>();
        cardList8.add("6222081502051320786");
        Account account8 = new Account("韦小宝", "3222081502051320786", "88888888", 10000.00, bankList,gsyh,cardList8);
        //用户总数组
        ArrayList<Account> accountList = new ArrayList<>();
        accountList.add(account1);
        accountList.add(account2);
        accountList.add(account3);
        accountList.add(account4);
        accountList.add(account5);
        accountList.add(account6);
        accountList.add(account7);
        accountList.add(account8);

        Scanner x=new Scanner(System.in);
        String kp;
        Check check;
        Check1 check1;

        kp=x.nextLine();
        while(!kp.equals("#")){
            int i,j;
            String[] shuju=kp.split("\\s+");//输入的每行数据用空格隔开,存为数组
            /*
            for(i=0;i<shuju.length;i++) {
                System.out.println(shuju[i]);
            }
             */

            if(shuju.length!=1){
                check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3]));
                if (check.check()){
                    Access access = new Access(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3]));
                    access.access();

                    Show show = new Show(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3]));
                    show.show();
                }
            }
            else {
                check1=new Check1(accountList,shuju[0]);
                if(check1.check()){
                    Show1 show1= new Show1(accountList,shuju[0]);
                    show1.show1();
                }
            }
            kp=x.nextLine();
        }
    }
}
View Code
复制代码

 

 

 

5.ATM机类结构设计(二)(题目集6的7-5)

编写一个银行 ATM 机的模拟程序,能够完成用户的取款以及查询余额功能。程序功能需求 (1)实现功能 ⚫ 基础数据的初始化; ⚫ 用户取款及查询余额功能。设计要求 (1)务必注意本题目中的各实体类之间的关系,尤其是一对多的组合关系。 (2)对实体类的设计要做到单一职责原则,且不能缺少规定的实体类。 (3)在“合成复用原则”及“单一职责原则”基础上,尽量对上次作业的程 序进行重构,使之符合 “开-闭”原则。

  类图如下:

 

 

 

 

三、总结

  个人觉得这次OOP作业集本人的完成情况不是非常的理想,尤其是第六次作业,分数甚至没能及格,以及第四次作业中的第一题,没能写出来,这些题目的确存在一定难度,但是并没有难到没人写出来,还是有着一部分人能做出这些题目的,而我却没能写出来,为此我觉得我应该做出深刻反思。首先,我认为第一个就是时间上没能够投入太多,就像段老师所说,java不是一门轻轻松松就能够学好的语言,而是应该投入大量的时间和精力。其次,便是效率问题,在同样的问题上,投入同样的时间,我的完成度总是比别人要少,这是我应该重点反思的。同时,这次OOP作业集也让我学到了很多,例如java的一些特性,类与类之间的关系,各种方法的调用以及数据的初始化等等。但这些还远远不够,只是入门级别而已,俗话说的好,师傅领进门,修行靠个人。如果想要取得更加优异的成绩,光靠这些肯定是不够的,还需要通过大量的练习来巩固和提升自己。在以后的练习中,我会投入更加多的时间和精力去学习java,努力提升自己。

posted @   一半在土里  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示