您的位置:首页 > 编程语言 > Java开发

慕课网-Java入门第二季实战练习-答答租车系统下载

2018-02-10 14:00 591 查看
更具所学知识,编写一个控制台版的”答答租车系统”

功能:

1.展示所有可租车辆

2.选择车型、租车量

3.展示租车清单,包含:总金额、总载货量及其车型、总载人量及其车型

代码下载地址:

http://download.csdn.net/download/qq_29132907/10248426

1.公共

package com.imooc.zuche;

public class Car {
private String name;
private int busload;//载客量
private double burden;//载货量
private double rentdaily;//日租金
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getBusload(){
return busload;
}
public double getBurden() {
return burden;
}
public void setBurden(double burden) {
this.burden = burden;
}
public double getRentdaily() {
return rentdaily;
}
public void setRentdaily(double rentdaily) {
this.rentdaily = rentdaily;
}
public void setBusload(int busload){
this.busload=busload;
}
}


2.载客汽车

package com.imooc.zuche;

//载客汽车
public class PassengerCar extends Car {
private String name;
private int busload;//载客量
private double rentdaily;//日租金

//构造函数
public PassengerCar(String name, int busload, double rentdaily) {
super();
this.name = name;
this.busload = busload;
this.rentdaily = rentdaily;
}

public String getName() {
return name;
}

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

public int getBusload() {
return busload;
}

public void setBusload(int busload) {
this.busload = busload;
}

public double getRentdaily() {
return rentdaily;
}

public void setRentdaily(double rentdaily) {
this.rentdaily = rentdaily;
}
}


3.卡车

package com.imooc.zuche;

//卡车:载货
public class Trunk extends Car {
private String name;
private double burden;//载货量
private double rentdaily;//日租金

//构造函数
public Trunk(String name, double burden, double rentdaily) {
super();
this.name = name;
this.burden = burden;
this.rentdaily = rentdaily;
}

public String getName() {
return name;
}

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

public double getBurden() {
return burden;
}

public void setBurden(double burden) {
this.burden = burden;
}

public double getRentdaily() {
return rentdaily;
}

public void setRentdaily(double rentdaily) {
this.rentdaily = rentdaily;
}

}


4.皮卡

package com.imooc.zuche;

//皮卡:载客、载货
public class pickUp extends Car {
private String name;
private int busload;//载客量
private double burden;//载货量
private double rentdaily;//日租金

public String getName() {
return name;
}

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

public int getBusload() {
return busload;
}

public void setBusload(int busload) {
this.busload = busload;
}

public double getBurden() {
return burden;
}

public void setBurden(double burden) {
this.burden = burden;
}

public double getRentdaily() {
return rentdaily;
}

public void setRentdaily(double rentdaily) {
this.rentdaily = rentdaily;
}

public pickUp(String name, int busload, double burden, double rentdaily) {
super();
this.name = name;
this.busload = busload;
this.burden = burden;
this.rentdaily = rentdaily;
}
}


5.租车APP主函数

package com.imooc.zuche;
import java.util.Scanner;//获取控制台输入

public class RentApp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("**********欢迎使用答答租车系统******");
Car[] dada={
new PassengerCar("奥迪A4",3,500),
new Trunk("东风200",10,500),
new pickUp("皮卡",3,5,300),
new PassengerCar("奥迪A6",3,600),
new PassengerCar("丰田",3,300),
new Trunk("解放60",12,500),
new pickUp("皮卡200",4,8,600)
};
System.out.println("你是否需要租车?是:1 否:0");
Scanner scan=new Scanner(System.in);
int inputNum=scan.nextInt();
if(inputNum==1){
int totalBusload=0;//总载客量
double totalBurden=0;//总载货量
double totalRent=0;//日总租金
int rentDays;//租车天数

int number=1;
//循环输出数组
for(Car tempCar:dada){
System.out.println(number+". ");
//instanceof判断是否含有PassengerCar
if(tempCar instanceof PassengerCar){
System.out.println("车型:"+tempCar.getName()+",载客量:"+tempCar.getBusload()+"人"+",日租金:"+tempCar.getRentdaily()+"元/日");
}
if(tempCar instanceof Trunk){
System.out.println("车型:"+tempCar.getName()+",载货量:"+tempCar.getBurden()+"吨"+",日租金:"+tempCar.getRentdaily()+"元/日");
}
if(tempCar instanceof pickUp){
System.out.println("车型:"+tempCar.getName()+",载客量:"+tempCar.getBusload()+"人"+",载货量:"+tempCar.getBurden()+"吨"+",日租金:"+tempCar.getRentdaily()+"元/日");
}
number++;
}
System.out.println("请输入你要租车的数量:");
//租车数量
int count=scan.nextInt();
for(int i=0;i<count;i++){
System.out.println("请输入您要选择的第"+(i+1)+"辆车的序号");
//输入选择车辆型号
int chooseNum=scan.nextInt();
System.out.println("你选择是是第"+chooseNum+"号车型");
//判断是否包含PassengerCar类型的车
if(dada[chooseNum-1] instanceof PassengerCar){
System.out.println("车型:"+dada[chooseNum-1].getName()+",载客量:"+dada[chooseNum-1].getBusload()+"人"+",日租金:"+dada[chooseNum-1].getRentdaily()+"元/日");
//总载客量
totalBusload+=dada[chooseNum-1].getBusload();
}
//选择卡车
if(dada[chooseNum-1] instanceof Trunk){
System.out.println("车型:"+dada[chooseNum-1].getName()+",载货量:"+dada[chooseNum-1].getBurden()+"吨"+",日租金:"+dada[chooseNum-1].getRentdaily()+"元/日");
//总载货量
totalBurden+=dada[chooseNum-1].getBurden();
}
//选择皮卡
if(dada[chooseNum-1] instanceof pickUp){
System.out.println("车型:"+dada[chooseNum-1].getName()+",载客量:"+dada[chooseNum-1].getBusload()+"人"+",载货量:"+dada[chooseNum-1].getBurden()+"吨"+",日租金:"+dada[chooseNum-1].getRentdaily()+"元/日");
//总载客量
totalBusload+=dada[chooseNum-1].getBusload();
//总载货量
totalBurden+=dada[chooseNum-1].getBurden();
}
//日租金
totalRent+=dada[chooseNum-1].getRentdaily();
}
System.out.println("请输入你租车的天数");
//输入租车天数
rentDays=scan.nextInt();
System.out.println("你租赁了:"+count+"辆车");
System.out.println("总载客量:"+totalBusload+"人");
System.out.println("总载货量:"+totalBurden+"吨");
System.out.println("总租金为:"+totalRent*rentDays+"元");
}else{
System.out.println("你退出了租车系统");
}
}

}


6.运行结果



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  答答租车