写的第一个项目大家看看

来源:12-2 项目问题解析 1

慕仰9267954

2019-11-17 19:52

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.mukewang.com/5dd134800001787410010646.jpg

写回答 关注

3回答

  • 慕斯卡0004266
    2019-12-15 02:02:57

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

    慕斯卡000...

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

    2019-12-15 02:05:55

    共 1 条回复 >

  • 慕村2513920
    2019-12-10 13:01:12

    载客量为啥用double?


  • 龙喆
    2019-11-20 20:43:00

    不错啊

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530650 学习 · 6091 问题

查看课程

相似问题