继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JAVA入门第二季——答答租车系统

xc_sit4045160
关注TA
已关注
手记 1
粉丝 0
获赞 18

这个小程序其实写了很多天,倒不是因为它特别难,而是因为自己学得太快,前面一二季的知识掌握得快,但运用起来容易忘掉当中的小细节,所以有时候程序报错会感到迷惑。同时这个程序参考了许多其他幕友写的程序,尤其是适合小白看系列,通俗易懂,那么在这个基础上我也写出一个比较通俗易懂的程序,让后面学习的幕友能够尽量明白,我也会尽量加入更多的注释以解惑关键的难点。

首先先创建一个Car类(父类)

package 阶段练习1;

public abstract class Car {
      public int people;//载客人数
      public int loads;//载货数量
      public int rent;//租金
      public String name;//车名
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }
    public int getLoads() {
        return loads;
    }
    public void setLoads(int loads) {
        this.loads = loads;
    }
    public int getRent() {
        return rent;
    }
    public void setRent(int rent) {
        this.rent = rent;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

其次依次创建PassageCar、Truck、Pickup类:
PassageCar类:

package 阶段练习1;

public class PassageCar extends Car {
    private String name;
    private int people;
    private int rent;

    public PassageCar(String name, int people, int rent){
        this.name = name;
        this.people = people;
        this.rent = rent;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPeople() {
        return people;
    }

    public void setPeople(int people) {
        this.people = people;
    }

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

}

Truck类:

package 阶段练习1;

public class Truck extends Car {
    private String name;
    private int rent;
    private int loads;

    public Truck(String name, int rent, int loads){
        this.name = name;
        this.rent = rent;
        this.loads = loads;
    }

    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;
    }

    public int getLoads() {
        return loads;
    }

    public void setLoads(int loads) {
        this.loads = loads;
    }

}

Pickup类:

package 阶段练习1;

public class Pickup extends Car {
    private String name;
    private int people;
    private int rent;
    private int loads;

    public Pickup(String name, int rent, int loads, int people){
        this.name = name;
        this.rent = rent;
        this.loads = loads;
        this.people = people;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPeople() {
        return people;
    }

    public void setPeople(int people) {
        this.people = people;
    }

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

    public int getLoads() {
        return loads;
    }

    public void setLoads(int loads) {
        this.loads = loads;
    }

}

之后创建一个显示页面:

package 阶段练习1;

import java.util.*;

public class 显示页面 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎使用答答租车系统!");
        System.out.println("您是否需要租车:1是\t0否");

        Scanner in1 = new Scanner(System.in);
        int is = in1.nextInt();
        if(is == 1){

        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号\t汽车名称\t日租金\t载客   货物吨数");

        //对各类车实例化并保存到cars数组
        Car[] cars = {new PassageCar("奥迪A4",4,500),
                      new PassageCar("马自达6",4,400),
                      new PassageCar("金龙",20,800),
                      new Pickup("皮卡雪6",450,2,4),
                      new Truck("松花江",400,4),
                      new Truck("依维柯",1000,20)};

        //使用循环方式将各类车输出  
          注意,这里PassageCar没有Loads系统默认为0,不会报错
        for(int i = 0; i < 6; i++){

            System.out.println((i+1)+"\t"+cars[i].getName()+"\t"+cars[i].getRent()+"\t"+cars[i].getPeople()+"\t"+cars[i].getLoads());

        }

        int totalrent = 0;//总租金
        int totalpeople = 0;//总人数
        int totalload = 0;//总载货量

             System.out.println("请输入您要租汽车的数量:");
        Scanner in2 = new Scanner(System.in);
        int num1 = in2.nextInt();

             int[] rentCar = new int[num1];

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

             Scanner inx = new Scanner(System.in);
               int numx = inx.nextInt();

            rentCar[i] = numx - 1; //表示出租的车的顺序序号

            totalrent += cars[numx - 1].getRent();
            totalpeople += cars[numx - 1].getPeople();
            totalload += cars[numx - 1].getLoads();

        }

        System.out.println("请输入租车天数:");
        Scanner in3 = new Scanner(System.in);
        int numt = in3.nextInt();

            totalrent = totalrent * numt;

        System.out.println("您的账单:");

        System.out.println("***可载人的车有:");//载人
        for(int i = 0; i < num1; i++){
            //用instanceof来排除货车类,即不是载人的车类
            if((cars[rentCar[i]] instanceof PassageCar) || cars[rentCar[i]] instanceof Pickup){
                System.out.print(cars[rentCar[i]].getName()+'\t');
            }else continue;
        }

        System.out.print('\n');

        System.out.println("***可载货的车有:");//载货
        for(int i = 0; i < num1; i++){
           //同理,用instanceof来排除载人类的车
            if((cars[rentCar[i]] instanceof Truck) || cars[rentCar[i]] instanceof Pickup){
                System.out.print(cars[rentCar[i]].getName()+'\t');
            }else continue;
        }   

             System.out.println('\n');

        System.out.println("共载人:"+totalpeople+"人");
        System.out.println("共载货:"+totalload+"吨");   
        System.out.println("租车总价格:"+totalrent);

        }else{
            System.out.println("感谢您的光临,若有需要可以随时使用本系统!");
        }
    }

}

以上就是所有代码,欢迎有问题的幕友提出来,我们来共同探讨!

打开App,阅读手记
10人推荐
发表评论
随时随地看视频慕课网APP

热门评论

看了十来个,你是最通俗易懂,适合小白的。

学习学习,谢谢大神分享

查看全部评论