手记

Java学习第二季 答答租车系统

父类

package com.oldboy;

public class Car_Car {
    private String name;
    private int price;
    private int ren;
    private int liang;

    public void car(String name,int price){
        this.name=name;
        this.name=name;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getRen() {
        return ren;
    }
    public void setRen(int ren) {
        this.ren = ren;
    }
    public int getLiang() {
        return liang;
    }
    public void setLiang(int liang) {
        this.liang = liang;
    }

}

子类

package com.oldboy;

public class Car_zhche extends Car_Car {

    public int liang;

    public Car_zhche(String name,int price,int liang){
        super();
        super.setName(name);
        super.setPrice(price);
        this.liang=liang;

    }

    public int getLiang() {
        return liang;
    }
    public void setLiang(int liang) {
        this.liang = liang;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getName()+"\t"+this.getPrice()+"\t"+this.liang;
    }

}
package com.oldboy;

public class Car_zkche extends Car_Car {
    private int ren;
    public int getRen() {
        return ren;
    }
    public void setRen(int ren) {
        this.ren = ren;
    }
    public Car_zkche(String name,int price,int ren){
        super();
        super.setName(name);
        super.setPrice(price);
        this.ren=ren;

    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getName()+"\t"+this.getPrice()+"\t"+this.ren;
    }

}
package com.oldboy;

public class Car_pkche extends Car_Car {
    public Car_pkche(String name,int price,int ren,int liang){
        super();
        super.setName(name);
        super.setPrice(price);
        this.ren=ren;
        this.liang=liang;

    }
    private int ren;
    private int liang;
    public int getRen() {
        return ren;
    }
    public void setRen(int ren) {
        this.ren = ren;
    }
    public int getLiang() {
        return liang;
    }
    public void setLiang(int liang) {
        this.liang = liang;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getName()+"\t"+this.getPrice()+"\t"+this.ren+" "+this.liang;
    }

}

main

package com.oldboy;

import java.util.Scanner;

public class Car_main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Car_Car[] car = {
                new Car_zhche("松花江",400,4),
                new Car_zhche("依维柯",1000,20),
                new Car_zkche("奥迪A4",500,4),
                new Car_zkche("马自达6",400,4),
                new Car_zkche("金龙",800,20),
                new Car_pkche("皮卡雪6",400,4,2),
        };

        System.out.println("------------------");
        System.out.println("");
        System.out.println("欢迎使用哒哒打车系统!");
        System.out.println("请输入您是否需要租车?");
        System.out.println("1:是");
        System.out.println("0:不是");
        System.out.println("");
        System.out.println("------------------");

        Scanner in = new Scanner(System.in);
        int i = in.nextInt();

        if(i == 1){

            System.out.println("您可以租车的价目表:");
            System.out.println("序号  汽车名字  租金    容量");
            Car_Car c1 = new Car_zhche("松花江",400,4);
            Car_Car c2 = new Car_zhche("依维柯",1000,20);
            Car_Car c3 = new Car_zkche("奥迪A4",500,4);
            Car_Car c4 = new Car_zkche("马自达6",400,4);
            Car_Car c5 = new Car_zkche("金龙",800,20);
            Car_Car c6 = new Car_pkche("皮卡雪6",400,4,2);
            for(int m=0;m<6;m++){
                System.out.println((m+1)+".\t"+car[m].toString());
            }

            System.out.println("请输入您要租车的数量:");
            int n = in.nextInt();

            int danjia=0;
            int zren=0;
            int zhuo=0;
            String zrenche="";
            String zhuoche="";

            for(int m=1;m<=n;m++){

                System.out.println("请输入第"+m+"辆车的序号");

                int num =in.nextInt();
                switch (num) {
                case 1:
                    danjia=c1.getPrice()+danjia;
                    zhuo=c1.getLiang()+zhuo;
                    zhuoche=zhuoche+" "+c1.getName();
                    break;
                case 2:
                    danjia=c2.getPrice()+danjia;
                    zhuo=c2.getLiang()+zhuo;
                    zhuoche=zhuoche+" "+c2.getName();
                    break;
                case 3:
                    danjia=c3.getPrice()+danjia;
                    zren=c3.getRen()+zren;
                    zrenche=zrenche+" "+c3.getName();
                    break;
                case 4:
                    danjia=c4.getPrice()+danjia;
                    zren=c4.getRen()+zren;
                    zrenche=zrenche+" "+c4.getName();
                    break;
                case 5:
                    danjia=c5.getPrice()+danjia;
                    zren=c5.getRen()+zren;
                    zrenche=zrenche+" "+c5.getName();
                    break;
                case 6:
                    danjia=c6.getPrice()+danjia;
                    zren=c6.getRen()+zren;
                    zhuo=c6.getLiang()+zhuo;
                    zhuoche=zhuoche+" "+c6.getName();
                    zrenche=zrenche+" "+c6.getName();
                    break;
                default:
                    break;
                }

            }

            System.out.println("请输入租车天数:");
            int zday = in.nextInt();
            int zongjia = danjia * zday;

            System.out.println( "您的账单:");
            System.out.println("------------------");
            System.out.println("***可载人的车:");
            System.out.println(zrenche+"\t总载人量:"+zren);
            System.out.println("------------------");
            System.out.println("***可载货的车:");
            System.out.println(zhuoche+"\t总载人量:"+zhuo);
            System.out.println("------------------");
            System.out.println("总租金为:"+zongjia);

        }else if(i == 0){

            System.out.println("  再见!       ");

        }else{

            System.out.println("输入错误");

        }

    }

}

5人推荐
随时随地看视频
慕课网APP

热门评论

在 main方法中,你直接将根据用户的输入序号用switch case语句来实现,可扩展性不强,如果多加几个对象,你就需要修改那部分的代码

同一辆车如果租赁2辆,会出现同名吧。比如租两辆奥迪A6,可载人的车会变成:奥迪A6  奥迪A6……


哪里写不好请大家指出!!!新生写这个花了好多时间!

查看全部评论