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

Dada租车系统(抽象类编程练习)

qq_菲魚_0
关注TA
已关注
手记 1
粉丝 0
获赞 4

package dada;

import java.util.Scanner;
abstract class Car{ //定义一个车的抽象类,包括车名、租金、载人数、载货数,抽象方法
public String name;
public int rent;
public int numper;
public double capacity;
public abstract String toString();
}

class Coach extends Car{ //客车类
public Coach(String name,int rent,int numper){
this.name = name;
this.rent = rent;
this.numper = numper;
}
public String toString(){
return(name+"\t"+rent+"/天\t"+numper+"人");
}
}

class Truck extends Car{ //货车类
public Truck(String name,int rent,double capacity){
this.name = name;
this.rent = rent;
this.capacity = capacity;
}
public String toString(){
return(name+"\t"+rent+"/天\t"+capacity+"吨");
}
}

class Pickub extends Car{ //皮卡车类
public Pickub(String name,int rent,int numper,double capacity){
this.name = name;
this.rent = rent;
this.numper = numper;
this.capacity = capacity;
}
public String toString(){
return(name+"\t"+rent+"/天\t"+numper+"人"+capacity+"吨");
}
}

public class Rentcar { //主类
public static void main(String[] args){
Car[] car = { //利用数组给每个类中的构造方法赋值
new Coach("奥迪A4",500,4),
new Coach("马自达6",400,4),
new Coach("金龙",800,20),
new Pickub("皮卡雪6",450,4,2),
new Truck("松花江",400,4),
new Truck("依维柯",1000,20)
};

    System.out.println("***欢迎使用嗒嗒租车系统***");
    System.out.println("您是否要租车: 1是\t0否");
    @SuppressWarnings("resource")
    Scanner op = new Scanner(System.in);        //创建Scanner对象
    int option = op.nextInt();      //用户创建并保存信息

    if(option==1){

        System.out.println("您可租车的类型及其价目表");
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        System.out.println("# 序号\t汽车名称\t租金\t容量\t#");
        for(int i=0;i<car.length;i++){      //循环输出各类中各构造方法的内容
            System.out.println("#  "+(i+1)+"\t"+car[i]+"\t#");
        }
        System.out.println("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
    }

    System.out.print("请输入您要租汽车的数量:");
    int a = op.nextInt();
    Car[] num = new Car[a];     //建立一个数组保存车辆数

    double total = 0;
    int people = 0;
    double weight = 0;
    int d = 0;

    int i = 0;      //定义i用来记下车序数
    for(i=0;i<a;i++){
        System.out.print("\r\n请输入第"+(i+1)+"辆车的序号:");
        int b = op.nextInt();//用于接收输入车辆的序号数
        num[i] = car[b-1];
        total = total+num[i].rent;
    }

    System.out.print("\r\n请输入租车的天数:");
    d = op.nextInt();
    System.out.println("\r\n您的账单:");
    System.out.println("***可载人的车有:");//循环并判断人数不为0的车输出,并记人数累加
    for(i=0;i<a;i++){
        if(num[i].numper!=0){
            System.out.print(num[i].name+"\t");
        }
        people = people+num[i].numper;
    }
    System.out.println("\t"+d+"天  "+"共载:"+people*d+"人");

    System.out.println("***可载货的车有:");//循环并判断货物不为0的车输出,并记重量累加
    for(i=0;i<a;i++){
        if(num[i].capacity!=0){
            System.out.print(num[i].name+"\t");
        }
        weight = weight+num[i].capacity;
    }
    System.out.println("\t"+d+"天  "+"共载货:"+weight*d+"吨");

    System.out.println("***租车总价格:"+total*d);    
}

}

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