手记

答答租车系统的程序设计

生成父类Car 和子类

输入代码package imooc;

public class Car {
    int num;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    String name;
    int money;
    int people;
    int pick;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }
    public int getPick() {
        return pick;
    }
    public void setPick(int pick) {
        this.pick = pick;
    }

}

class AudiA4 extends Car{
    int num;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    String name;
    int money;
    int people;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }
    public AudiA4(int num,String name,int money,int people) {
        this.num= num;
        this.name= name;
        this.money = money;
        this.people=people;
    }
}   
    class Mazida6 extends Car{
        int num;
        public int getNum() {
            return num;
        }
        public void setNum(int num) {
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getMoney() {
            return money;
        }
        public void setMoney(int money) {
            this.money = money;
        }
        public int getPeople() {
            return people;
        }
        public void setPeople(int people) {
            this.people = people;
        }
        public Mazida6(int num, String name,int money,int people) {
            this.num= num;
            this.name= name;
            this.money = money;
            this.people=people;

        }
    }
class Pikaxue6 extends Car{
    int num;
    String name;
    int money;
    int people;
    int pick;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }
    public int getPick() {
        return pick;
    }
    public void setPick(int pick) {
        this.pick = pick;
    }

    public Pikaxue6(int num,String name,int money,int people,int pick) {
        this.num= num;
        this.name= name;
        this.money = money;
        this.people=people;
        this.pick=pick;
    }
}

class Jinlong extends Car{
    int num;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }

    String name;
    int money;
    int people;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }

    public Jinlong( int num, String name,int money ,int people) {
        this.num= num;
        this.name= name;
        this.money = money;
        this.people= people;
    }
}

class Songhuajiang extends Car{
    int num;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }

    String name;
    int money;
    int pick;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getPick() {
        return pick;
    }
    public void setPick(int pick) {
        this.pick = pick;
    }

    public Songhuajiang(int num ,String name,int money,int pick) {
        this.num= num;
        this.name= name;
        this.money = money;
        this.pick=pick;

    }

}

class Yiweike extends Car{
    int num;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }

    String name;
    int money ;
    int pick;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public int getPick() {
        return pick;
    }
    public void setPick(int pick) {
        this.pick = pick;
    }

    public Yiweike(int num,String name,int money,int pick) {
        this.num= num;
        this.name= name;
        this.money = money;
        this.pick = pick;
    }
}

main方法

package imooc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DaDa {

    public static void main(String[] args)throws IOException  {

        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车 : 1是   0否"); 

        BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
        String str = br.readLine();
        int a = Integer.parseInt(str);

        Car[] cars= {new AudiA4(1, "奥迪A4", 500, 4), new Mazida6(2,"马自达6", 400, 4), new Pikaxue6(3,"皮卡雪6", 450, 4, 2), new Jinlong(4,"金龙           ", 800, 20), new Songhuajiang(5,"松花江       ", 400, 4), new Yiweike(6,"依维柯      ", 1000, 20) };

        if (a == 1) {

            System.out.println("你可租的类型及其价目表: ");
            System.out.println("序号 " +"    " +"汽车名称" +"         "+"租金 "  +"                    "+ "容量");
            int num=0;
            for (Car car : cars) {
                num++;
                System.out.println(num +"    "+car.getName()+"    "+car.getMoney()+"/天       载人: "+car.getPeople()+"人    载货:"+car.getPick()+"吨");
        }

            System.out.println("请输入您要租车的数量:");
        }else {
            System.exit(0);

        }
        BufferedReader br1 = new BufferedReader( new InputStreamReader(System.in));
        String str1 = br1.readLine();
        int a1 = Integer.parseInt(str1);

         for (int i = 0; i < a1; i++) {
            System.out.println("请输入第"+(i+1)+"辆车的序号:");

            BufferedReader br2 = new BufferedReader( new InputStreamReader(System.in));
            String str2 = br2.readLine();
            int a2 = Integer.parseInt(str2);
            if (a2==1) {
                cars[0].setNum(0);
            }
            if (a2==2) {
                cars[1].setNum(0);
            }
            if (a2==3) {
                cars[2].setNum(0);
            }
            if (a2==4) {
                cars[3].setNum(0);
            }
            if (a2==5) {
                cars[4].setNum(0);
            }
            if (a2==6) {
                cars[5].setNum(0);
            }

            }

        System.out.println("请输入租车天数:");
        BufferedReader br3 = new BufferedReader( new InputStreamReader(System.in));
        String str3 = br3.readLine();
        int a3 = Integer.parseInt(str3);

        System.out.println("****************************************");
        System.out.println("您的账单:");
        System.out.println("*******可载人车辆有:");
        int totlePeople = 0;
        for (int i = 0; i < cars.length; i++) {

            if (cars[i].getNum() == 0 && i!=5 && i!=4) {
                System.out.print(cars[i].getName()+"   ");
                totlePeople = cars[i].getPeople()+totlePeople;
            } 
        }
        System.out.println("共可以载"+totlePeople+"人!");

        System.out.println();

        System.out.println("*******可载货车辆有:");
        int totlePick= 0;
        for (int i = 0; i < cars.length; i++) {         
            if (cars[i].getNum() == 0 && i!=0 && i!=1 && i!=3) {
                System.out.print(cars[i].getName()+"   ");
                totlePick = cars[i].getPick()+totlePick;
            }

        }
        System.out.println("共可以载"+totlePick+"吨货物!");
        System.out.println("*****租车总价格为:");
            int totleMoney =0;
            for (int i = 0; i < cars.length; i++) {

            if (cars[i].getNum() == 0) {
                totleMoney = cars[i].getMoney()*a3+ totleMoney;
                }   
        }
        System.out.println(totleMoney);

    }

        }

小白感觉这次的代码写得好辛苦,特别是在已选车辆编号与名称对应那里。。。。

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