看了很多份代码,终于写完了

来源:12-1 综合练习

慕UI8012440

2021-01-01 18:03

http://img4.sycdn.imooc.com/5feef21c0001459203430590.jpg

package com.imooc.car_rental;
 /*  
 * 汽车父类  
 */
 public class Car {
 	private String name;//车名	
 	private float rental;//租金		
 	public Car(String name,float rental){		
 	this.name=name;		
 	this.rental=rental;	
 	}		
 	public String getName() {		
 	return name;	
 	}		
 	public float getRental() {		
 	return rental;	
 	}	

package com.imooc.car_rental;

/*

 * 轿车类

 */

public class Sedan extends Car {

private int seatingCapacity;//载客人数

public Sedan(String name,float rental,int seatingCapacity){

super(name,rental);

this.seatingCapacity=seatingCapacity;

}

public int getSeatingCapacity(){

return seatingCapacity;

}

}


package com.imooc.car_rental;

/*

 * 货车类

 */

public class Truck extends Car {

private float cargoCapacity;//载货量

public Truck(String name, float rental,float cargoCapacity) {

super(name, rental);

this.cargoCapacity=cargoCapacity;

// TODO Auto-generated constructor stub

}

public float getCargoCapacity(){

return cargoCapacity;

}

}

package com.imooc.car_rental;
/* 
*  皮卡类
*/
public class Pickup extends Car {	
private int seatingCapacity;//载客人数	
private float cargoCapacity;//载货量		
public Pickup(String name, float rental,int seatingCapacity,float cargoCapacity) {		
super(name, rental);		
this.seatingCapacity=seatingCapacity;		
this.cargoCapacity=cargoCapacity;		
// TODO Auto-generated constructor stub	
}		
public int getSeatingCapacity(){		
return seatingCapacity;	}		
public float getCargoCapacity(){		
return cargoCapacity;	
}
}

package com.imooc.car_rental;


import java.util.Scanner;


/*

 * 租车系统

 */

public class CarRentalSystem {

//菜单

public void menu(){

//创建Scanner对象

Scanner scan=new Scanner(System.in);

System.out.println("欢迎使用答答租车系统:");

System.out.println("您是否要租车:1是  0否");

int num=scan.nextInt();

if(num==1){

//进入租车菜单

carRentalMenu();

}else{

System.out.println("为您退出答答租车");

System.exit(0);

}

}

//汽车列表

public void carRentalMenu(){

//创建Scanner对象

Scanner scan=new Scanner(System.in);

System.out.println("您可租车的类型及其价目表:");

Car[] cars={new Sedan("奥迪A4", 500, 4),

new Sedan("马自达6", 400, 4),

new Pickup("皮卡雪6",450,4,2),

new Sedan("金龙 ",800,20),

new Truck("松花江",400,4),

new Truck("依维柯",1000,20)};

System.out.println("序号\t名称\t\t租金\t\t\t容量");

//输出汽车列表

for(int i=0;i<cars.length;i++){

//判断汽车类型

if(cars[i] instanceof Sedan){

Sedan car=(Sedan)cars[i];

System.out.println((i+1)+"\t"+car.getName()+"\t"+car.getRental()+"元/天\t载人:"+car.getSeatingCapacity()+"人");

}else if(cars[i] instanceof Pickup){

Pickup car=(Pickup)cars[i];

System.out.println((i+1)+"\t"+car.getName()+"\t"+car.getRental()+"元/天\t载人:"+car.getSeatingCapacity()+"人"+"载货:"+car.getCargoCapacity()+"吨");

}else if(cars[i] instanceof Truck){

Truck car=(Truck) cars[i];

System.out.println((i+1)+"\t"+car.getName()+"\t"+car.getRental()+"元/天\t载货:"+car.getCargoCapacity()+"吨");

}

}

/*for(Car car:cars){//需要重写toString()方法

System.out.println(car.toString());

}*/

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

int rent_num=scan.nextInt();

int id;//定义编号

Car[] rent_cars=new Car[rent_num];//定义数组保存租出的车

//定义总租金、总载人量、总载货量变量

float totalRent=0;

int totalSeating=0;

float totalCargo=0;

for(int i=0;i<rent_num;i++){

System.out.println("请输入第"+(i+1)+"辆车的编号");

id=scan.nextInt();

rent_cars[i]=cars[id-1];

//判断汽车类型

if(cars[id-1] instanceof Sedan){

Sedan car=(Sedan) cars[id-1];

totalRent+=car.getRental();

totalSeating+=car.getSeatingCapacity();

}else if(cars[id-1] instanceof Pickup){

Pickup car=(Pickup)cars[id-1];

totalRent+=car.getRental();

totalSeating+=car.getSeatingCapacity();

totalCargo+=car.getCargoCapacity();

}else if(cars[id-1] instanceof Truck){

Truck car=(Truck) cars[id-1];

totalRent+=car.getRental();

totalCargo+=car.getCargoCapacity();

}

}

System.out.println("请选择要租用的天数");

int rent_day=scan.nextInt();

totalRent*=rent_day;//计算总金额

System.out.println("**************************");

System.out.println("您的账单:");

System.out.println("***可载人的车有:");

for(int i=0;i<rent_cars.length;i++){

if(!(rent_cars[i] instanceof Truck)){

System.out.print(rent_cars[i].getName()+"\t");

}

}

System.out.println("共载人:"+totalSeating+"人");

System.out.println("***可载货的车有:");

for(int i=0;i<rent_cars.length;i++){

if(!(rent_cars[i] instanceof Sedan)){

System.out.print(rent_cars[i].getName()+"\t");

}

}

System.out.println("共载货:"+totalCargo+"吨");

System.out.println("***租车总金额为:"+totalRent+"元");

}


}


/*测试类*/

package com.imooc.car_rental;


public class Test {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

CarRentalSystem car=new CarRentalSystem();

car.menu();

}


}


写回答 关注

1回答

  • 殇TAT
    2021-02-22 18:44:57

    牛牪犇

Java入门第二季 升级版

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

530559 学习 · 6091 问题

查看课程

相似问题