车类
package com.DadaRentCar;
public class Car {
private int id;//定义编号
private int num;//定义数量
private double prince;//定义金额
private String name;//定义名称
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrince() {
return prince;
}
public void setPrince(double prince) {
this.prince = prince;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
货车类
package com.DadaRentCar;
public class Trunk extends Car implements IGodcarry{
//货车类
private int carryGo;//运货
public Trunk(int id,String name,int number,double prince,int carryGo){
this.setId(id);
this.setName(name);
this.setNum(number);
this.setPrince(prince);
this.carryGo=carryGo;
}
public String toString(){
return "编号:"+getId()+"\t车名为:"+getName()+ "\t剩余数量:"+getNum()+"\t租金:"
+getPrince()+"\t载货量:"+carryGo;
}
@Override
public int getgodCarry() {
// TODO Auto-generated method stub
return carryGo;
}
}
客车类
package com.DadaRentCar;
public class Bus extends Car implements IPepocarry{
//客车类
int carryPep;
public Bus(int id,String name,int number,double prince,int carryPep){
this.setId(id);
this.setName(name);
this.setNum(number);
this.setPrince(prince);
this.carryPep=carryPep;
}
public String toString(){//返回对象中的信息
return "编号:"+getId()+"\t车名为:"+getName()+"\t剩余数量:"+getNum()+"\t租金:"
+getPrince()+"\t载客量:"+carryPep;
}
@Override
public int getPepocarry() {
// TODO Auto-generated method stub
return carryPep;
}
}
载人接口
package com.DadaRentCar;
//载人接口
public interface IPepocarry {
public int getPepocarry();
}
载货接口
package com.DadaRentCar;
//载货接口
public interface IGodcarry {
public int getgodCarry();
}
皮卡类//可载人和货
package com.DadaRentCar;
public class Pickup extends Car implements IPepocarry,IGodcarry {
int carrygod,carrypepo;
public Pickup(int id,String name,int number,double prince,int carrygod,int carrypepo){
this.setId(id);
this.setName(name);
this.setNum(number);
this.setPrince(prince);
this.carrygod=carrygod;
this.carrypepo=carrypepo;
}
public String toString(){
return "编号:"+getId()+"\t车名为:"+getName()+ "\t剩余数量:"+getNum()+"\t租金:"
+getPrince()+"\t载客量:"+carrypepo+"\t载货量"+carrygod;
}
@Override
public int getgodCarry() {
// TODO Auto-generated method stub
return carrygod;
}
@Override
public int getPepocarry() {
// TODO Auto-generated method stub
return carrypepo;
}
}
主函数
package com.DadaRentCar;
import java.util.Scanner;
public class Tigger {
public static void main(String[] args) {
Tigger t=new Tigger();
t.show();
}
public void show(){//显示车辆信息
int sumcarRent=0;//定义单日总租金
int totalRent=0;//定义总金额
int sumpeople=0;//总载人量
int sumgood=0;//总载货量
String peoplecar=" ";//保存载人车变量
String goodcar=" ";//载货车变量
//通过数组保存实例化的车辆对象
System.out.println("******************************欢迎使用达达租车系统******************************");
System.out.println("如果需要租车请按1,不需要请按其他键");
//读取控制台输入数据
Scanner input1=new Scanner(System.in);
if(input1.nextInt()==1){
System.out.println("——————可租用车辆信息如下所示——————");
Car[] cars={new Bus(1,"金龙",5, 4000, 60),
new Bus(2,"海鸥",13,2000,12),
new Trunk(3,"红岩",17,3000,50),
new Trunk(4,"农用车", 4,1200, 4),
new Pickup(5,"长城皮卡",9,200,2,1)
};
for(int i=0;i<cars.length;i++){
System.out.println(cars[i]);
}
System.out.println("输入你要租用车辆的总数量");
Scanner input2=new Scanner(System.in);
int carNum=input2.nextInt();//保存输入的车辆总数
if(carNum>0){
for(int i=1;i<=carNum;i++){
System.out.println("输入租用第"+i+"辆车编号");
Scanner input3=new Scanner(System.in);
int carId=input3.nextInt();
//计算租金
/**
* 如何找到输入的值与数中中相应对象匹配的情况2.27 23:07
* 蠢哭了,carId-1就是数组中的对象
*/
//打印选中车辆信息
System.out.println("选租车信息"+"\n"+cars[carId-1].toString());
//计算所选车辆总租金
sumcarRent +=cars[carId-1].getPrince();
//计算所选车辆载荷量
//如果选的是载客车,载客车数量+对象载客量,如果载货车,载货车数量+对象载货量
if(cars[carId-1].getClass()==Bus.class){
peoplecar +=cars[carId-1].getName()+ "\t";
//计算总载客量******非常重要**********将父类car对象强转为子类Bus类,使用子类中属性
sumpeople +=((Bus)cars[carId-1]).getPepocarry();
}else if(cars[carId-1].getClass()==Trunk.class){
goodcar +=cars[carId-1].getName()+"\t";
sumgood +=((Trunk)cars[carId-1]).getgodCarry();
}else if(cars[carId-1].getClass()==Pickup.class){//皮卡车型,既可以是载客,也可以载货
peoplecar +=cars[carId-1].getName()+ "\t";
goodcar +=cars[carId-1].getName()+"\t";
sumpeople +=((Pickup)cars[carId-1]).getPepocarry();
sumgood +=((Pickup)cars[carId-1]).getgodCarry();
}
}
//选择租用车辆完成
System.out.println("***********输入你要租用的天数***********");
Scanner inputday=new Scanner(System.in);
int days=inputday.nextInt();
if(days>0){
totalRent=sumcarRent*days;//天数乘单日总租金
}else{
System.out.println("输入数据有误");
System.exit(0);//退出系统
}
System.out.println("*************信息输出*************");
System.out.println("你选择的载人汽车有:"+peoplecar);
System.out.println("总载客量为:"+sumpeople+"人");
System.out.println("你选择的载货汽车有:"+goodcar);
System.out.println("总载货量是:"+sumgood+"吨");
System.out.println("总租金是:"+totalRent);
System.out.println("**************************感谢使用达达租车系统**************************");
}else{
System.out.println("输入错误");
System.exit(0);//退出系统
}
}else{
System.out.println("您不需要租车");
System.exit(0);//退出系统
}
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦