为了账号安全,请及时绑定邮箱和手机立即绑定

写的第一个项目大家看看

package com.dinyanqing;

public abstract class taxi {
public String name;
public double rent;
int id;


public String getName() {
return name;
}

public double getRent() {
return rent;
}

public void show() {
System.out.println("1");
}
public int getId() {
return id;
}

}


package com.dinyanqing;

public class passengerCar extends taxi {

private double peopleCapacity;//载客量
public passengerCar(int id ,String name,double rent,double peopleCapacity) {
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
this.id=id;
}
public void show() {
System.out.println(id+"\t"+name+"\t"+rent+"元/天"+"\t"+"载人:"+peopleCapacity+"人");
}
public double getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity) {
this.peopleCapacity=peopleCapacity;
}

}


package com.dinyanqing;

public class PickUp extends taxi {
private double cargoCapacity;
private double peopleCapacity;
public PickUp(int id,String name,double rent,double peopleCapacity,double cargoCapacity) {
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
this.cargoCapacity=cargoCapacity;
this.id=id;
}
public void show() {
System.out.println(id+"\t"+name+"\t"+rent+"元/天"+"\t"+"载人:"+peopleCapacity+"人"+" 载货:"+cargoCapacity+"吨");
}


public double getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity) {
this.peopleCapacity=peopleCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getRent() {
return rent;
}

}


package com.dinyanqing;

public class Trunk extends taxi {
private double cargoCapacity;

public Trunk(int id,String name,double rent,double cargoCapacity) {
this.name=name;
this.rent=rent;
this.cargoCapacity=cargoCapacity;
this.id=id;
}
public void show() {
System.out.println(id+"\t"+name+"\t"+rent+"元/天"+"\t"+"载货:"+cargoCapacity+"吨");
}
public double getCargoCapacity() {
return cargoCapacity;
}

public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}

}

package com.dinyanqing;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

taxi[] carsForRent={new passengerCar(1,"奥迪A4",500,4),new passengerCar(2,"奔驰S600",600,4),new PickUp(3,"皮卡雪", 450, 4, 2),
new Trunk(4,"松花江",400 , 4)};
System.out.println("欢迎使用答答租车系统:");
System.out.println("你是否需要租车:1是0否");
@SuppressWarnings("resource")
Scanner scan=new Scanner(System.in);
String input=scan.next();
if(input.equals("1")) {
System.out.println("你可租车的类型及其价目表");
System.out.println("序号\t汽车名称\t租金\t\t容量");
}
else {
System.out.println("滚吧!");
System.exit(0);

}

for(taxi cout:carsForRent){

cout.show();

}
System.out.println("请输入您要租车的数量:");

int c[]=new int [] {500,600,450,400} ;
double sum=0;
int input1=scan.nextInt();
if (input1>4||input1<=0) {

System.out.println("输入错误请退出重试");
System.exit(0);
}
else {
for(int j=1;j<=input1;j++) {
System.out.println("请输入第"+j+"辆车的序号:");
int input2=scan.nextInt();

sum+=c[(input2)-1];

}

}

System.out.println("请输入租车天数:");
int input3=scan.nextInt();
double price=sum*input3;
System.out.println("租车总价格:");
System.out.println(price);
}
}
http://img1.sycdn.imooc.com//5dd134800001787410010646.jpg

正在回答

3 回答

租车的价格,可以通过数组中的对象(数组名[下标索引])获取,这里的价格写死了不利于扩展。还有选择租车类型的编号也是固定的,可以通过获取数组长度来做条件判断。

0 回复 有任何疑惑可以回复我~
#1

慕斯卡0004266

有个问题,按照原题实现了功能,问题是车型编号为4的金龙,使用了一个\t制表符空格,会导致上下格式不一致。其他格式正常,原因是金龙是两个字符,三个字符就没问题。希望能有老铁能解惑!!!感谢~~
2019-12-15 回复 有任何疑惑可以回复我~

载客量为啥用double?


0 回复 有任何疑惑可以回复我~

不错啊

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

写的第一个项目大家看看

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信