写完后后来又重新修改了一遍,大概能反映现阶段的水平吧
要继承的父类:
package com.imooc;
public class Car {
int id;
String name;
int people;
double weight;
double price;
//设置一个布尔值,表示车是否可租,默认可租,如果车被租出去,用false表示不可租
boolean rent=true;
public double getPrice(){
return price;
}
//改变车辆租借状态
public void setRent(boolean rent){
this.rent=rent;
}
//为了后面主函数里更加简洁,这里增加getMessage方法
public void getMessage(){
System.out.println(id+"\t"+name+"\t"+price+"元/天 \t"+"载人:"+people+"人 "+" 载货"+weight+"吨");
}
}
载人客车类:
package com.imooc;
public class PeopleCar extends Car {
public PeopleCar(int id,String name,int people,double price){
this.id=id;
this.name=name;
this.people=people;
this.price=price;
}
//增加一个获取载客人数的方法
public int getPeople(){
return people;
}
}
载货货车类
package com.imooc;
public class CargoCar extends Car {
public CargoCar(int id,String name,double weight,double price){
this.id=id;
this.name=name;
this.weight=weight;
this.price=price;
}
//针对货车增加获得载重量的方法
public double getWeight(){
return weight;
}
}
两用车类
package com.imooc;
public class TwoCar extends Car {
public TwoCar(int id,String name,int people,double weight,double price){
this.id=id;
this.name=name;
this.people=people;
this.weight=weight;
this.price=price;
}
//客货两用车同时具有获得载客数和载重数的方法
public int getPeople(){
return people;
}
public double getWeight(){
return weight;
}
}
主函数所在类:
package com.imooc;
import java.util.Scanner;
public class RentCar {
public static void main(String[] args){
//0,输入车辆信息,存入数组中
Car car1=new PeopleCar(1,"奥迪A4",4,500.0);
Car car2=new PeopleCar(2,"马自达6",4,400.0);
Car car4=new PeopleCar(4,"金龙",20,800.0);
Car car5=new CargoCar(5,"松花江",4.0,400.0);
Car car6=new CargoCar(6,"依维柯",20.0,1000.0);
Car car3=new TwoCar(3,"皮卡雪6",4,2.0,450.0);
//将引用的车辆类型存入cars数组中,感觉使用map类型更好,用id作为键,但我不会
Car[] cars={car1,car2,car3,car4,car5,car6};
//1,租车前的互动,打印信息,接受输入
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
if(num==1){
System.out.println("");
}else{
System.out.println("您将退出答答租车系统");
}
//2,展示可租汽车信息,如果车辆rent属性为真,表示可租赁。
//另外,如果某种车不具备某种属性,我们将其值表示为0
System.out.println("您可租车的类型及价目表:");
System.out.println("序号 \t汽车名称 \t租金\t 容量 ");
for(Car each:cars){
if(each.rent==true){
each.getMessage();
}
}
//3,与租车者进行互动
System.out.println("请输入要租汽车的数量");
int rent_num=input.nextInt();
//创建变量来存放总载人数,载货数,总价格
int peoplesum=0;
double weightsum=0.0;
double pricesum=0.0;
//定义两个字符串变量 来存放已选择车辆名字信息,其实使用列表或集合更好,但我还不会
String peoplecarname="";
String cargocarname="";
for(int i=1;i<=rent_num;i++){
System.out.println("请输入第"+i+"辆车的序号:");
int id=input.nextInt();
//因为id与数组索引的内在关系,故像下面这么写,否则就要遍历数组来匹配了
//修改该车租赁状态,改为不可租
cars[id-1].setRent(false);
pricesum+=cars[id-1].getPrice();
if(cars[id-1] instanceof PeopleCarcars[id-1] instanceof TwoCar){
peoplecarname+=cars[id-1].name+"\t";
peoplesum+=cars[id-1].people;
}
if(cars[id-1] instanceof CargoCarcars[id-1] instanceof TwoCar){
cargocarname+=cars[id-1].name+"\t";
weightsum+=cars[id-1].weight;
}
}
System.out.println("请输入要租车的天数:");
int day=input.nextInt();
pricesum=pricesum*day;
System.out.println("您的帐单:");
System.out.println("***可载人的车有:");
System.out.print(peoplecarname);
System.out.println("共载人:"+peoplesum+"人");
System.out.println("***载货的车有:");
System.out.print(cargocarname+"共载货"+weightsum+"吨");
System.out.println("租车总价格为"+pricesum+"元");
}
}
有什么看法和意见,欢迎共同探讨。
热门评论
lz,我有个问题啊,你的父类中有6个属性,子类却少于6个属性,子类的构造函数舍弃了一些东西,子类不是对父类的扩充吗。但我又疑惑我写的,我的父类是提取了公共部分,子类再添加int people;double weight;两个属性,然后测试类中Car car1=new PeopleCar(1,"奥迪A4",4,500.0);car1能不能使用子类的people或weight属性?
楼主很优秀,我已经工作了在看这样的代码,觉得很清晰。希望你会在这条路上走的更远
这里面的两个if判断语句中的判断条件:
应该是:(cars[id-1] instanceof PeopleCar || cars[id-1] instanceof TwoCar),中间是有“||”来连接的,传上来后不知道怎么就没显示 || 符号,看得时候加上||符号就好了。