java自学笔记7:阶段总结

来源:互联网 发布:淘宝皇冠和天猫哪个好? 编辑:程序博客网 时间:2024/06/08 18:44

这里写图片描述

数据模型分析
1.通过对现实世界的事与物主要特征的分析,抽象,为信息系统的实施提供数据存取的数据结构以及相应的约束。

2.数据结构组成:操作(方法),属性

这里写图片描述

业务模型分析
1.在设计应用程序之前,应该明确该应用程序必须执行哪些任务。
分析业务需求是应用程序开发中最重要的步骤之一。
确认业务需求的目的在于创建一个能同时满足零售商和消费者需要的解决方案。

2.答答租车系统中,只需要考虑消费者业务需求即可
这里写图片描述

显示和流程分析
1.显示:用户可以看到的信息提示界面。
2.流程:显示信息的执行过程,步骤。
3.答答租车系统中,要以命令行的方式显示提示信息和输出结果信息,要考虑其样式,用户输入的数据不同,信息该如何提示,如何处理并显示结果,这部分知识囊括了显示与流程的内容
例如:请选择车辆-》请输入序号-》输出总金额

参考:
这里写图片描述

代码实现:

首先创建一个父类,Carstore,
然后创建三个子类,PickUp,Coach,Van
接着创建两个接口,IVan,ICoach.

父类Carstore

package com.vishuo.www;public class Carstore {    private int id;    private String name;    private int rent;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getRent() {        return rent;    }    public void setRent(int rent) {        this.rent = rent;    }}

接口ICoach

package com.vishuo.www;public interface ICoach {    public void setpeopleCapacity(int peopleCapacity);    public int getpeopleCapacity();}

接口IVan

package com.vishuo.www;public interface IVan {    public void setcargoCapacity(int cargoCapacity);    public int getcargoCapacity();}

子类Coach

package com.vishuo.www;public  class Coach extends Carstore implements ICoach {    private int peopleCapacity;    // 构造一个带参的方法,并且通过super关键字调用父类的方法    public Coach(int id, String name, int rent, int peopleCapacity) {        super.setId(id);        super.setName(name);        super.setRent(rent);        this.setpeopleCapacity(peopleCapacity);    }    /*     * 这是一段不使用接口的get/set方法 public int getpeopleCapacity(){ return     * peopleCapacity; }     *      * public void setPeopleCapacity(int peopleCapacity){ this.peoplecapacity =     * peopleCapacity; }     */    // 使用toString方法输出车辆信息    public String toString() {        return super.getId() + "\t" + super.getName() + "\t" + super.getRent() + "元/天" + "\t" + "\t"                + this.getpeopleCapacity() + "人";    }    @Override    // 继承接口的方法.    public void setpeopleCapacity(int peopleCapacity) {         // TODO Auto-generated method stub        this.peopleCapacity = peopleCapacity;    }    @Override    public int getpeopleCapacity() {        // TODO Auto-generated method stub        return peopleCapacity;    }}

子类PickUp

package com.vishuo.www;public class PickUp extends Carstore implements ICoach, IVan {    private int peopleCapacity;    private int cargoCapacity;    public PickUp(int id, String name, int rent, int peopleCapacity, int cargocapacity) {        super.setId(id);        super.setName(name);        super.setRent(rent);        this.setpeopleCapacity(peopleCapacity);        this.setcargoCapacity(cargocapacity);    }    /*     * public int getPeopleCapacity() { return peopleCapacity; } public void     * setPeopleCapacity(int peopleCapacity) { this.peopleCapacity =     * peopleCapacity; } public int getCargocapacity() { return cargocapacity; }     * public void setCargocapacity(int cargocapacity) { this.cargocapacity =     * cargocapacity; }     */    public String toString() {        return super.getId() + "\t" + super.getName() + "\t" + super.getRent() + "元/天" + "\t" + this.getpeopleCapacity()                + "人" + "\t" + this.getcargoCapacity() + "吨";    }    @Override    public void setcargoCapacity(int cargoCapacity) {        // TODO Auto-generated method stub        this.cargoCapacity = cargoCapacity;    }    @Override    public int getcargoCapacity() {        // TODO Auto-generated method stub        return cargoCapacity;    }    @Override    public void setpeopleCapacity(int peopleCapacity) {        // TODO Auto-generated method stub        this.peopleCapacity = peopleCapacity;    }    @Override    public int getpeopleCapacity() {        // TODO Auto-generated method stub        return peopleCapacity;    }}

子类:Van

package com.vishuo.www;public class Van extends Carstore implements IVan {    private int cargoCapacity;    public Van(int id, String name, int rent, int cargocapacity) {        super.setId(id);        super.setName(name);        super.setRent(rent);        this.setcargoCapacity(cargocapacity);    }    /*     * public int getCargocapacity() { return cargocapacity; } public void     * setCargocapacity(int cargocapacity) { this.cargocapacity = cargocapacity;     * }     */    public String toString() {        return super.getId() + "\t" + super.getName() + "\t" + super.getRent() + "元/天" + "\t" + this.getcargoCapacity()                + "吨";    }    @Override    public void setcargoCapacity(int cargoCapacity) {        // TODO Auto-generated method stub        this.cargoCapacity = cargoCapacity;    }    @Override    public int getcargoCapacity() {        // TODO Auto-generated method stub        return cargoCapacity;    }}

测试类:Index

package com.vishuo.www;import java.util.Scanner;public class Index {    private Scanner input;    public void CarUse() {        /*         * 通过"数组+多态"的方法创建各种类型的车 轿车参数, 皮卡参数, 火车参数,         */        Carstore[] car = { new Coach(1, "普拉多", 999, 6), new Coach(2, "金牛座", 800, 4), new Coach(3, "自由侠", 700, 4),                new Coach(4, "朗逸", 109, 5), new Coach(5, "科鲁兹", 109, 5), new Coach(6, "帕萨特", 212, 5),                new PickUp(7, "丰田坦途", 1000, 5, 15), new Van(8, "依维柯", 1100, 20), new Van(9, "松花江", 1000, 15) };        System.out.println("欢迎您使用格致租车系统" + "\n是否租车 1是 0否 ");        input = new Scanner(System.in);        int i = input.nextInt();        if (i == 1) {            System.out.println("下面是可租车辆价目表:" + "\n序号" + "\t" + "车名" + "\t" + "租金" + "\t" + "\t" + "容量"); // 通过for循环将车辆信息将车辆信息依次输出.            for (i = 0; i < car.length; i++) {                System.out.println(car[i]);            }            System.out.println("请输入您要租的数量:");            int j = input.nextInt();            if (i > 9 || i == 0 || i < 0) {                System.out.println("输入错误,系统无法验证!");                System.exit(0);            }            String outname = ""; // 车名            int rentsum = 0; // 租金总和            int peoplesum = 0; // 载客人数和            int cargosum = 0; // 载货量和            int count = 1;            int w;            while (count <= j) {                // 使用do..while循环,先执行后判断.                do {                    System.out.println("请输入第" + count + "辆车的序号:");                    w = input.nextInt() - 1;                } while (w >= car.length || w < 0); // 使用if循环,判断输入的车辆序号为哪种类型,并且计算租金等信息.                if (car[w].getClass() == Coach.class) {                    outname = car[w].getName();                    System.out.println(outname);                    rentsum = car[w].getRent() + rentsum;                    peoplesum = ((Coach) car[w]).getpeopleCapacity() + peoplesum;                } else if (car[w].getClass() == PickUp.class) {                    outname = car[w].getName();                    System.out.println(outname);                    rentsum = car[w].getRent() + rentsum;                    peoplesum = ((PickUp) car[w]).getpeopleCapacity() + peoplesum;                    cargosum = ((PickUp) car[w]).getcargoCapacity() + cargosum;                } else if (car[w].getClass() == Van.class) {                    outname = car[w].getName();                    System.out.println(outname);                    rentsum = car[w].getRent() + rentsum;                    cargosum = ((Van) car[w]).getcargoCapacity() + cargosum;                }                count++;            }            System.out.println("请输入您要租的天数:");            int day = input.nextInt();            System.out.println("您的账单如下:");            System.out.println("载人数:" + peoplesum + "人" + "\n载货量:" + cargosum + "吨" + "\n总租金:" + rentsum * day + "元");            System.out.println("感谢您光临格致租车,祝您生活愉快!");        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Index cus = new Index();        cus.CarUse();        System.exit(0);    }}