11_实例


链接:https://pan.baidu.com/s/1VqZRwyun8LY6S1U8lpOK9A
提取码:diga

实例一:二维数组

/**
 * 定义一个长度为[16][16]的整型二维数组并输入或指定所有位置的元素值,
 * 分别实现二维数组中所有行和所有列中所有元素的累加和并打印,
 * 再分别实现二维数组中左上角到右下角和右上角到左下角所有元素的累加和并打印。
 */

Answer1.java

package cn.itcast.day01.homework02;


/**
 * 定义一个长度为[16][16]的整型二维数组并输入或指定所有位置的元素值,
 * 分别实现二维数组中所有行和所有列中所有元素的累加和并打印。
 * 再分别实现二维数组中左上角到右下角和右上角到左下角所有元素的累加和并打印。
 */
public class Answer1 {
    public static void main(String[] args) {
        int[][] arr1 = new int[16][16];
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                arr1[i][j] = j;
            }
        }

        //1、所有行中所有元素的累加和
        //i,j 是行列式的下标
        for (int i = 0; i < 16; i++) {
            int sumHang = 0;
            for (int j = 0; j < 16; j++) {
                sumHang += arr1[i][j];
            }
            System.out.println("第" + i + "行中所有元素的累加和是:" + sumHang);
        }

        System.out.println("---------------------------------");

        //2、所有列中所有元素的累加和
        for (int i = 0; i < 16; i++) {
            int sumLie = 0;
            for (int j = 0; j < 16; j++) {
                sumLie += arr1[j][i];
            }
            System.out.println("第" + i + "列中所有元素的累加和是:" + sumLie);
        }

        System.out.println("---------------------------------");

        //3、左上角到右下角的累加和
        int SumLeftUp2RightDown = 0;
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                if (i == j) {
                    SumLeftUp2RightDown += arr1[i][j];
                }
            }
        }
        System.out.println("左上角到右下角的累加和是:" + SumLeftUp2RightDown);

        System.out.println("---------------------------------");

        //4、右上角到左下角的累加和
        int SumRightUp2LeftDown = 0;
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                if (16 == i + j) {
                    SumRightUp2LeftDown += arr1[i][j];
                }
            }
        }
        System.out.println("左下角到右上角的累加和是:" + SumRightUp2LeftDown);
    }
}

编译并执行Answer1.java,执行结果是:

第0行中所有元素的累加和是:120
第1行中所有元素的累加和是:120
第2行中所有元素的累加和是:120
第3行中所有元素的累加和是:120
第4行中所有元素的累加和是:120
第5行中所有元素的累加和是:120
第6行中所有元素的累加和是:120
第7行中所有元素的累加和是:120
第8行中所有元素的累加和是:120
第9行中所有元素的累加和是:120
第10行中所有元素的累加和是:120
第11行中所有元素的累加和是:120
第12行中所有元素的累加和是:120
第13行中所有元素的累加和是:120
第14行中所有元素的累加和是:120
第15行中所有元素的累加和是:120
---------------------------------
第0列中所有元素的累加和是:0
第1列中所有元素的累加和是:16
第2列中所有元素的累加和是:32
第3列中所有元素的累加和是:48
第4列中所有元素的累加和是:64
第5列中所有元素的累加和是:80
第6列中所有元素的累加和是:96
第7列中所有元素的累加和是:112
第8列中所有元素的累加和是:128
第9列中所有元素的累加和是:144
第10列中所有元素的累加和是:160
第11列中所有元素的累加和是:176
第12列中所有元素的累加和是:192
第13列中所有元素的累加和是:208
第14列中所有元素的累加和是:224
第15列中所有元素的累加和是:240
---------------------------------
左上角到右下角的累加和是:120
---------------------------------
左下角到右上角的累加和是:120

实例二:五子棋

/**
 *  编程实现控制台版并支持两人对战的五子棋游戏。
 * (1)绘制棋盘 - 写一个成员方法实现
 * (2)提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。
 * (3)每当一方下棋后判断是否获胜 - 写一个成员方法实现。
 * (4)提示: 采用二维数组来模拟并描述棋盘,棋盘如下:
 */

QiZiInterface.java

package cn.itcast.day01.homework02;

public interface QiZiInterface {
    public abstract String show();  // 显示棋子
}

QiZi.java

package cn.itcast.day01.homework02;


/**
 * 棋子
 */
public enum QiZi implements QiZiInterface {
    BLACK("黑") {
        @Override
        public String show() {
//            System.out.println(" * ");
            return " @ ";
        }
    }, WHITE("白") {
        @Override
        public String show() {
//            System.out.println(" # ");
            return " # ";
        }
    };

    private final String color;

    private QiZi(String color) {
        this.color = color;
    }
}

QiPan.java

package cn.itcast.day01.homework02;

import java.util.Arrays;

/**
 * 棋盘
 * 1、用+号填充棋盘
 * 2、draw方法显示棋盘内容
 * 3、
 */
public class QiPan {
    String[][] qizhuo = new String[16][16];

    /**
     * 无参构造方法:用" + "填充棋盘
     */
    public QiPan() {
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                qizhuo[i][j] = " + ";
            }
        }
    }

    /**
     * draw方法:画出棋盘内容
     */
    public void draw() {
        for (int i = 0; i < 16; i++) {

            if (0 == i) {
                System.out.print(' ');
                System.out.print(' ');
                for (int k = 0; k < 16; k++) {
                    System.out.print(' ');
                    System.out.print(Integer.toHexString(k)); //16进制
                    System.out.print(' ');
                }
                System.out.println();
            }
            System.out.print(Integer.toHexString(i));
            System.out.print(' ');

            for (int j = 0; j < 16; j++) {
                System.out.print(qizhuo[i][j]);
            }
            System.out.println();
        }
    }

    /**
     * 在棋盘指定位置下棋
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean play(int row, int colu, String color) {
        if (row < 0 || colu < 0) {
            System.out.println("超出棋盘范围了!!!");
            return false;
        }

        if (" + " == qizhuo[colu][row]) {
            // 将棋子(白棋#,黑棋@)放入棋盘中
            qizhuo[colu][row] = QiZi.valueOf(color).show();
            return true;
        } else {
            System.out.println("该位置已经有棋子了!!!");
            return false;
        }
    }

    /**
     * 判断在当前列中是否可以获胜
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWinRow(int row, int colu, String color) {
        int minColu = colu - 4;
        int maxColu = colu + 4;
        //当落子的上下到达边界时,置为0或15
        if (minColu < 0) {
            minColu = 0;
        }
        if (maxColu > 15) {
            maxColu = 15;
        }


        //遍历九个格子若有五个相同颜色的棋子连线则为胜
        int sum = 0;
        for (int i = minColu; i <= maxColu; i++) {
            //横坐标(row)不变,纵坐标(colu)变
            String piece = qizhuo[i][row];
            if (piece == QiZi.valueOf(color).show()) {
                sum++;
                if (sum == 5) {
                    return true;
                }
            } else {
                sum = 0;   //若不为同颜色 将sum置为0
            }
        }
        return false;
    }

    /**
     * 判断在当前行中是否可以获胜
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWinColu(int row, int colu, String color) {
        int minRow = row - 4;
        int maxRow = row + 4;
        if (minRow < 0) {
            minRow = 0;
        }
        if (maxRow > 15) {
            maxRow = 15;
        }

        int sum = 0;
        for (int i = minRow; i <= maxRow; i++) {
            //纵坐标(colu)不变,横坐标(row变)
            String piece = qizhuo[colu][i];
            if (piece == QiZi.valueOf(color).show()) {
                sum++;
                if (sum == 5) {
                    return true;
                }
            } else {
                sum = 0;
            }
        }
        return false;
    }


    /**
     * 判断左斜(\)是否获胜
     *
     * @param row
     * @param colu
     * @param color
     */
    public boolean isWinLeftObl(int row, int colu, String color) {
        int sum = 0;
        for (int i = row - 4; i <= row + 5; i++) {
            for (int j = colu - 4; j < colu + 5; j++) {
                if (i >= 0 && i <= 15 && j >= 0 && j <= 15 && row - colu == i - j) {
                    if (qizhuo[j][i] == QiZi.valueOf(color).show()) {
                        sum++;
                        if (sum == 5) {
                            return true;
                        }
                    } else {
                        sum = 0;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断右斜(/)是否获胜
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWinRightObl(int row, int colu, String color) {
        int sum = 0;
        for (int i = row - 4; i <= row + 5; i++) {
            for (int j = colu - 4; j < colu + 5; j++) {
                if (i >= 0 && i <= 15 && j >= 0 && j <= 15 && row + colu == i + j) {
                    if (qizhuo[j][i] == QiZi.valueOf(color).show()) {
                        sum++;
                        if (sum == 5) {
                            return true;
                        }
                    } else {
                        sum = 0;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 五子棋判断输赢
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWin(int row, int colu, String color) {
        if (row < 0 || colu < 0) {
            System.out.println("超出棋盘范围了!!!");
            return false;
        }

        if (isWinColu(row, colu, color) || isWinRow(row, colu, color) || isWinLeftObl(row, colu, color) || isWinRightObl(row, colu, color)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        QiPan qiPan = new QiPan();
        qiPan.draw();
    }
}

Answer2.java

package cn.itcast.day01.homework02;

import java.util.Scanner;

public class Answer2 {
    public static boolean[] PeopleRun(QiPan qiPan, String color) {
        if ("WHITE" == color) {
            System.out.print("请WHITE(#)方棋手下棋(坐标:(0~15,0~15)):");
        } else if ("BLACK" == color) {
            System.out.print("请BLACK(@)方棋手下棋(坐标:(0~15,0~15)):");
        }

        //输入横纵坐标(列数、行数)
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        int colu = sc.nextInt();


        //下棋
        boolean res_Play = qiPan.play(row, colu, color);
        //显示棋盘
        qiPan.draw();
        boolean res_isWin = qiPan.isWin(row, colu, color);

        boolean[] ret = {res_Play, res_isWin};
        return ret;
    }

    public static void main(String[] args) {
        QiPan qiPan = new QiPan();
        qiPan.draw();

        boolean flag = true;
        int code = 2;
        String color;

        while (flag) {
            if (0 == code % 2) {
                color = "BLACK";
            } else {
                color = "WHITE";
            }

            boolean[] arr = Answer2.PeopleRun(qiPan, color);
            if (arr[0]) {
                code++;
            } else {
                System.out.println("请" + color + "方棋手重新落子!");
            }
            flag = !arr[1];
            if (arr[1]) {
                System.out.println(color + "方棋手获得本次对局胜利!");
            }else {
                System.out.println("本次落子" + color + "方棋手未获胜~~~");
            }
        }
    }
}

编译并执行Answer2.java,结果为:

   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
0  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
1  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
2  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
3  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
4  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
5  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
6  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
7  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
8  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
9  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
a  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
b  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
c  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
d  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
e  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
f  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
请BLACK(@)方棋手下棋(坐标:(0~15,0~15)):-1 0
超出棋盘范围了!!!
...
...
...
请WHITE(#)方棋手下棋(坐标:(0~15,0~15)):6 5
该位置已经有棋子了
   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
0  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
1  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
2  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
3  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
4  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
5  +  +  +  +  +  +  #  +  +  @  +  +  +  +  +  + 
6  +  +  +  +  +  +  #  +  @  +  +  +  +  +  +  + 
7  +  +  +  +  +  +  #  @  +  +  +  +  +  +  +  + 
8  +  +  +  +  +  +  @  +  +  +  +  +  +  +  +  + 
9  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
a  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
b  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
c  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
d  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
e  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
f  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
请WHITE方棋手重新落子!
本次落子WHITE方棋手未获胜~~~
请WHITE(#)方棋手下棋(坐标:(0~15,0~15)):6 4
   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
0  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
1  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
2  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
3  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
4  +  +  +  +  +  +  #  +  +  +  +  +  +  +  +  + 
5  +  +  +  +  +  +  #  +  +  @  +  +  +  +  +  + 
6  +  +  +  +  +  +  #  +  @  +  +  +  +  +  +  + 
7  +  +  +  +  +  +  #  @  +  +  +  +  +  +  +  + 
8  +  +  +  +  +  +  @  +  +  +  +  +  +  +  +  + 
9  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
a  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
b  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
c  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
d  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
e  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
f  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
本次落子WHITE方棋手未获胜~~~
请BLACK(@)方棋手下棋(坐标:(0~15,0~15)):10 4
   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
0  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
1  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
2  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
3  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
4  +  +  +  +  +  +  #  +  +  +  @  +  +  +  +  + 
5  +  +  +  +  +  +  #  +  +  @  +  +  +  +  +  + 
6  +  +  +  +  +  +  #  +  @  +  +  +  +  +  +  + 
7  +  +  +  +  +  +  #  @  +  +  +  +  +  +  +  + 
8  +  +  +  +  +  +  @  +  +  +  +  +  +  +  +  + 
9  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
a  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
b  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
c  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
d  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
e  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
f  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
BLACK方棋手获得本次对局胜利!

Process finished with exit code 0

实例三:手机卡

/*
3.1 第一步:设计和实现以下类
	(1)手机卡类 特征:卡类型、卡号、用户名、密码、账户余额、通话时长(分钟)、上网流量 行为:显示(卡号 + 用户名 + 当前余额) 
	(2)通话套餐类 特征:通话时长、短信条数、每月资费 行为: 显示所有套餐信息
	(3)上网套餐类 特征:上网流量、每月资费 行为:显示所有套餐信息 
	(4)用户消费信息类 特征:统计通话时长、统计上网流量、每月消费金额 

3.2 第二步:设计和实现以下枚举类 手机卡的类型总共有 3 种:大卡、小卡、微型卡 

3.3 第三步:实体类的优化 将通话套餐类和上网套餐类中相同的特征和行为提取出来组成抽象套餐类。 

3.4 第四步:创建并实现以下接口 
    (1)通话服务接口 抽象方法: 参数1: 通话分钟, 参数2: 手机卡类对象 让通话套餐类实现通话服务接口。 
    (2)上网服务接口 抽象方法: 参数1: 上网流量, 参数2: 手机卡类对象 让上网套餐类实现上网服务接口。
 
3.5 第五步:进行代码测试
*/

Package.java:

package cn.itcast.day01.homework02.IPhone;

public interface Package {
    public abstract void show();
}

CallService.java:

package cn.itcast.day01.homework02.IPhone;

public interface CallService extends Package {
    public abstract int getCallDuration();

    public abstract void setCallDuration(int callDuration);

    public abstract int getCountOfTextMessage();

    public abstract void setCountOfTextMessage(int countOfTextMessage);

    public abstract double getMonthCost();

    public abstract void setMonthCost(double monthCost);

}

OnlineService.java:

package cn.itcast.day01.homework02.IPhone;

public interface OnlineService extends Package {
    public abstract double getNetworkFlow();

    public abstract void setNetworkFlow(long networkFlow);

    public abstract double getMonthCost();

    public abstract void setMonthCost(double monthCost);
}

CallPackage.java:

package cn.itcast.day01.homework02.IPhone;

/**
 * 通话套餐类
 * 特征:通话时长、短信条数、每月资费
 * 行为: 显示所有套餐信息
 */
public class CallPackage implements CallService {
    private int callDuration;       //通话时长(分钟)
    private int countOfTextMessage; //短信条数
    private double monthCost;       //每月资费
    private int usedCallDuration = 0;   //已消耗的通话时长
    private int usedCountOfTextMessage = 0; //已消耗的短信条数

    public CallPackage(PhoneCardType phoneCardType) {
        if ("大".equals(phoneCardType.getCardType())) {
            setCallDuration(500);
            setCountOfTextMessage(200);
            setMonthCost(20);
        } else if ("小".equals(phoneCardType.getCardType())) {
            setCallDuration(200);
            setCountOfTextMessage(100);
            setMonthCost(10);
        } else if ("微型".equals(phoneCardType.getCardType())) {
            setCallDuration(100);
            setCountOfTextMessage(50);
            setMonthCost(5);
        } else {
            System.out.println("error");
        }
    }

    public int getCallDuration() {
        return callDuration;
    }

    public void setCallDuration(int callDuration) {
        this.callDuration = callDuration;
    }

    public int getCountOfTextMessage() {
        return countOfTextMessage;
    }

    public void setCountOfTextMessage(int countOfTextMessage) {
        this.countOfTextMessage = countOfTextMessage;
    }

    public double getMonthCost() {
        return monthCost;
    }

    public void setMonthCost(double monthCost) {
        this.monthCost = monthCost;
    }

    public int getUsedCallDuration() {
        return usedCallDuration;
    }

    public void setUsedCallDuration(int usedCallDuration) {
        this.usedCallDuration = this.usedCallDuration + usedCallDuration;
    }

    public int getUsedCountOfTextMessage() {
        return usedCountOfTextMessage;
    }

    public void setUsedCountOfTextMessage(int usedCountOfTextMessage) {
        this.usedCountOfTextMessage = this.usedCountOfTextMessage + usedCountOfTextMessage;
    }

    public void show() {
        System.out.println("本套餐包含通话时长:" + getCallDuration() + "分钟,短信:" + getCountOfTextMessage() + "条,每月资费:" + getMonthCost() + "元");
    }

    public int call(int callDuration) {
        setUsedCallDuration(callDuration);
        System.out.println("本次通话时长为:" + callDuration + "分钟");
        return callDuration;
    }
}

OnlinePackage.java:

package cn.itcast.day01.homework02.IPhone;

/**
 * 上网套餐类
 * 特征:上网流量、每月资费
 * 行为:显示所有套餐信息
 */
public class OnlinePackage implements OnlineService {
    private double networkFlow;     //上网流量
    private double monthCost;       //每月资费
    private double usedNetworkFlow; //已消耗的上网流量

    public OnlinePackage(PhoneCardType phoneCardType) {
        if ("大".equals(phoneCardType.getCardType())) {
            setNetworkFlow(1000);
            setMonthCost(20);
        } else if ("小".equals(phoneCardType.getCardType())) {
            setNetworkFlow(500);
            setMonthCost(10);
        } else if ("微型".equals(phoneCardType.getCardType())) {
            setNetworkFlow(200);
            setMonthCost(5);
        } else {
            System.out.println("error");
        }

    }

    public double getNetworkFlow() {
        return networkFlow;
    }

    public void setNetworkFlow(long networkFlow) {
        this.networkFlow = this.usedNetworkFlow + networkFlow;
    }

    public double getMonthCost() {
        return monthCost;
    }

    public void setMonthCost(double monthCost) {
        this.monthCost = monthCost;
    }

    public double getUsedNetworkFlow() {
        return usedNetworkFlow;
    }

    public void setUsedNetworkFlow(double usedNetworkFlow) {
        this.usedNetworkFlow = usedNetworkFlow;
    }

    public void show() {
        System.out.println("本套餐包含上网流量:" + getNetworkFlow() + "MB,每月资费为:" + getMonthCost() + "元");
    }

    public long surfTheInternet(long networkFlow) {
        setUsedNetworkFlow(networkFlow);
        System.out.println("本次消耗流量为:" + networkFlow + "MB");
        return networkFlow;
    }
}

PhoneCardType.java:

package cn.itcast.day01.homework02.IPhone;

public enum PhoneCardType {
    BIG("大"), SMALL("小"), MINI("微型");

    private final String cardType;

    private PhoneCardType(String cardType) {
        this.cardType = cardType;
    }

    public String getCardType() {
        return cardType;
    }
}

PhoneCard.java:

package cn.itcast.day01.homework02.IPhone;

/**
 * 手机卡类
 * 特征:卡类型、卡号、用户名、密码、账户余额、通话时长(分钟)、上网流量
 * 行为:显示(卡号 + 用户名 + 当前余额)
 */
public class PhoneCard {
    private PhoneCardType cardType;         //卡类型
    private CallPackage callPackage;        //通话套餐
    private OnlinePackage onlinePackage;    //上网套餐

    private String cardNumber;              //卡号
    private String username;                //用户名
    private String password;                //密码

    private double accountBalance = 200;          //账户余额
    private int callDuration = 0;               //通话时长(分钟)
    private double networkFlow = 0;             //上网流量(MB)


    public PhoneCard(PhoneCardType phoneCardType, String cardNumber) {
        setCardType(phoneCardType);
        setCallService();
        setOnlineService();

        setCardNumber(cardNumber);
        setUsername("username");
        setPassword("password");
    }

    public PhoneCardType getCardType() {
        return cardType;
    }

    public void setCardType(PhoneCardType phoneCardType) {
        this.cardType = phoneCardType;
    }

    public CallPackage getCallPackage() {
        return callPackage;
    }

    public void setCallService() {
        this.callPackage = new CallPackage(this.cardType);
    }

    public OnlinePackage getOnlinePackage() {
        return onlinePackage;
    }

    public void setOnlineService() {
        this.onlinePackage = new OnlinePackage(this.cardType);
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }

    public int getCallDuration() {
        return callDuration;
    }

    public void setCallDuration(int callDuration) {
        this.callDuration = callDuration;
    }

    public double getNetworkFlow() {
        return networkFlow;
    }

    public void setNetworkFlow(double networkFlow) {
        this.networkFlow = networkFlow;
    }

    public void show() {
        System.out.println("卡号:" + getCardNumber() + ",用户名:" + getUsername() + ",当前余额:" + getAccountBalance());
    }
}

UserConsumptionInfo.java:

package cn.itcast.day01.homework02.IPhone;

/**
 * 用户消费信息类
 * 特征:统计通话时长、统计上网流量、每月消费金额
 */
public class UserConsumptionInfo {
    private int callDuration;       //通话时长(分钟)
    private double networkFlow;     //上网流量
    private double mouthCost;       //每月资费

    public UserConsumptionInfo(PhoneCard phoneCard) {
        setCallDuration(phoneCard);
        setNetworkFlow(phoneCard);
        setMouthCost(phoneCard);
    }

    public int getCallDuration() {
        return callDuration;
    }

    public void setCallDuration(PhoneCard phoneCard) {
        this.callDuration = phoneCard.getCallPackage().getUsedCallDuration();
    }

    public double getNetworkFlow() {
        return networkFlow;
    }

    public void setNetworkFlow(PhoneCard phoneCard) {
        this.networkFlow = phoneCard.getOnlinePackage().getUsedNetworkFlow();
    }

    public double getMouthCost() {
        return mouthCost;
    }

    public void setMouthCost(PhoneCard phoneCard) {
        this.mouthCost = phoneCard.getCallPackage().getMonthCost() + phoneCard.getOnlinePackage().getMonthCost();
    }

    public void show() {
        System.out.println("当前用户通话总时长为:" + getCallDuration() + ",总消耗流量为:" + getNetworkFlow() + "MB,每月总资费为:" + getMouthCost() + "元");
    }

}

MyTest.java:

package cn.itcast.day01.homework02.IPhone;

public class MyTest {
    public static void main(String[] args) {
        PhoneCard phoneCard = new PhoneCard(PhoneCardType.BIG, "15066668888");
        phoneCard.show();

        System.out.println("------------------------------------------------------");
        phoneCard.getCallPackage().show();
        phoneCard.getCallPackage().call(33);

        System.out.println("------------------------------------------------------");
        phoneCard.getOnlinePackage().show();
        phoneCard.getOnlinePackage().surfTheInternet(66);

        System.out.println("------------------------------------------------------");
        new UserConsumptionInfo(phoneCard).show();
    }
}

编译并执行MyTest.java

卡号:15066668888,用户名:username,当前余额:200.0
------------------------------------------------------
本套餐包含通话时长:500分钟,短信:200条,每月资费:20.0元
本次通话时长为:33分钟
------------------------------------------------------
本套餐包含上网流量:1000.0MB,每月资费为:20.0元
本次消耗流量为:66MB
------------------------------------------------------
当前用户通话总时长为:33,总消耗流量为:66.0MB,每月总资费为:40.0元
posted @ 2020-10-22 07:07  咕噜噜~  阅读(320)  评论(0编辑  收藏  举报