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

答答租车系统(Java作业╰( ̄▽ ̄)╮)

sout_main
关注TA
已关注
手记 1
粉丝 8
获赞 0
  • Car(父类)
    
    package com.imooc;

public class Car {
private String name;
private int person;
private double goods;
private double rent;

public String getName() {
    return name;
}

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

public int getPerson() {
    return person;
}

public void setPerson(int person) {
    this.person = person;
}

public double getGoods() {
    return goods;
}

public void setGoods(double goods) {
    this.goods = goods;
}

public double getRent() {
    return rent;
}

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

}


----------

 - PersonCar.java(子类)`

package com.imooc;

public class PersonCar extends Car {
public PersonCar(String name, int person, double goods, double rent) {
this.setName(name);
this.setPerson(person);
this.setGoods(goods);
this.setRent(rent);
}

}


----------

 - TwoCar.java(子类)

package com.imooc;

public class TwoCar extends Car {
public TwoCar(String name, int person, double goods, double rent) {
this.setName(name);
this.setPerson(person);
this.setGoods(goods);
this.setRent(rent);
}
}


----------

 - GoodsCar.java(子类)

package com.imooc;

public class GoodsCar extends Car {
public GoodsCar(String name, int person, double goods, double rent) {
this.setName(name);
this.setPerson(person);
this.setGoods(goods);
this.setRent(rent);
}
}


----------

 - Inital.java(测试类)

package com.imooc;

import java.util.Scanner;

public class Inital {
public static void main(String[] args) {
Car[] cars={ new PersonCar("奥迪A6", 4, 0, 600),
new PersonCar("马自达6", 4, 0, 400),
new PersonCar("金龙", 20, 0, 800),
new TwoCar("皮卡雪", 4, 2.5, 450),
new GoodsCar("松花江", 2, 4.8, 400),
new GoodsCar("依维柯", 2, 20, 1000),
};
System.out.println("欢迎使用天朝租车系统:");
System.out.println("请问是否租车?(1是/0否)");
Scanner input = new Scanner(System.in);
int flag = input.nextInt();
if (flag == 1) {
System.out.println("您可租车的类型及其租用价目表如下:");
System.out.println("序号\t汽车名次\t载人\t载货\t租金");
System.out.println("1\t奥迪A6\t4人\t0吨\t600/天");
System.out.println("2\t马自达6\t4人\t0吨\t400/天");
System.out.println("3\t金龙\t20人\t0吨\t800/天");
System.out.println("4\t皮卡雪6\t4人\t2.5吨\t450/天");
System.out.println("5\t松花江\t2人\t4.8吨\t400/天");
System.out.println("6\t依维柯\t2人\t20吨\t1000/天");
System.out.println("请输入你您要租车的数量:");
int n = input.nextInt();
int[] car_n = new int[n]; //储存各客户 的租车序号
for(int i = 0; i < n; i++){
System.out.println("请输入您第"+(i+1)+"辆租车的序号: ");
int num = input.nextInt();
while(num <= 0 || num > 6){
System.out.println("输入车辆序号错误,请重新输入车辆序号(1~6):");
int num1 = input.nextInt();
num = num1;
}
car_n[i] = num;
}
System.out.println("请输入您要租车的天数: ");
int day = input.nextInt();
System.out.println("您本次的租车清单如下:");
double mrent = 0.0f; //总租金
int mperson = 0; //总载人数
double mgoods = 0.0f; //总载物量
System.out.println("序号\t汽车名称\t载人\t载货\t租金");
for(int i = 0; i < n; i++) {
for(int j = 0; j < cars.length; j++) {
if(car_n[i] == j+1){
System.out.println(car_n[i]+"\t"+cars[j].getName()+"\t"+cars[j].getPerson()+"\t"+cars[j].getGoods()+"\t"+cars[j].getRent());
mrent = mrent + cars[j].getRent();
mperson = mperson + cars[j].getPerson();
mgoods = mgoods + cars[j].getGoods();
}
}
}
System.out.println("租车的天数为: "+day+"天");
System.out.println("合计总的租金为: "+(mrent*day)+"元");
System.out.println("总载人数: "+mperson+"人");
System.out.println("总载货量: "+mgoods+"吨");
input.close();
} else {
System.out.println("已退出租车系统。");
input.close();
}
}
}



----------

 - 测试结果如下图

![图片描述][1]

  [1]: http://img.mukewang.com/5888acba0001b13303880560.png
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP