父类
package com.dadarentcar;
public abstract class Vehicle {
protected String name;
protected double price;
protected int mannedNumber;
protected int carryFreightNumber;
public abstract boolean manned();
public abstract boolean carry();
}
子类
package com.dadarentcar;
/*
* 汽车可以载人,但是不能载物;
*/
public class Car extends Vehicle {
public Car(String name,double price,int mannedNumber){
this.name = name;
this.price = price;
this.mannedNumber = mannedNumber;
}
@Override
public boolean manned() {
// TODO Auto-generated method stub
System.out.println("可以载人数为:" + mannedNumber);
return true;
}
@Override
public boolean carry() {
// TODO Auto-generated method stub
System.out.println("错误,不可以载物");
return false;
}
}
package com.dadarentcar;
/*
* 可以载物,但是不可以载人
*/
public class Van extends Vehicle {
public Van(String name,double price,int carryFreightNumber){
this.name = name;
this.price = price;
this.carryFreightNumber = carryFreightNumber;
}
@Override
public boolean manned() {
// TODO Auto-generated method stub
System.out.println("错误,不可以载人");
return false;
}
@Override
public boolean carry() {
// TODO Auto-generated method stub
System.out.println("可以载物的重量为:"+carryFreightNumber);
return true;
}
}
package com.dadarentcar;
/*
* 皮卡可以载物也可以载人
*/
public class PickUpTruck extends Vehicle {
public PickUpTruck(String name,double price,int mannedNumber,int carryFreightNumber) {
this.name = name;
this.price = price;
this.carryFreightNumber = carryFreightNumber;
this.mannedNumber = mannedNumber;
}
@Override
public boolean manned() {
// TODO Auto-generated method stub
System.out.println("可以载人数为:" + mannedNumber);
return true;
}
@Override
public boolean carry() {
// TODO Auto-generated method stub
System.out.println("可以载物的重量为:"+carryFreightNumber);
return true;
}
}
main方法
package com.dadarentcar;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
int number = 0;//租车的数量
int days = 0;//租车的时间
double totalPrice = 0;//租车的总价格
Vehicle[] choose = { new Car("奥迪A4", 500, 4), new Car("马自达6", 400, 4), new PickUpTruck("皮卡雪6", 450, 4, 2),
new Car("金龙", 800, 20), new Van("松花江", 400, 4), new Van("依维柯", 1000, 20) };
int i = choose.length;
System.out.println("欢迎使用达达租车系统:");
while(true) {// 菜单,可以用方法写,这里直接写在main里
System.out.println("您是否需要租车:1是 0否");
Scanner input = new Scanner(System.in);
int comfirm = input.nextInt();
if (comfirm == 0) {
break;// 跳出租车系统
}else if(comfirm != 1){
System.out.println("请输入正确的数字");
continue;
}
System.out.println("您可租车的类型及价目表如下:");
System.out.println("序号\t汽车名称\t租金\t容量");
for (int l = 0; l < i; l++) {
System.out.println(l+1 + ".\t" + inf(choose[l]));
}
System.out.println("请输入租车的数量:");
number = input.nextInt();
int[] vehicleNumber = new int[number];
Vehicle[] vehicleChoose = new Vehicle[number];
for(int l = 0; l < number; l++){
int h = l+1;
System.out.println("请输入第" + h + "辆车的序号");
int m = input.nextInt();
if(m>=1&&m<=i){
vehicleChoose[l] = choose[m-1];
}else{
System.out.println("输入错误,请重新输入....");
l--;
}
}
System.out.println("请输入租车的天数:");
days = input.nextInt();
for(int l = 0; l < number; l++){
totalPrice += vehicleChoose[l].price ;
}
totalPrice *= days;
bill(vehicleChoose);
System.out.println("***租车的总价格:" + totalPrice + "元");
break;
}
System.out.println("按任意键退出。。。。");
}
public static String inf(Vehicle veh) {// 显示交通工具信息
String a = "";
if (veh instanceof Car) {
a = veh.name + "\t" + veh.price + "元/天\t载人:" + veh.mannedNumber + "人";
}
if (veh instanceof Van) {
a = veh.name + "\t" + veh.price + "元/天\t载货:" + veh.carryFreightNumber + "吨";
}
if (veh instanceof PickUpTruck) {
a = veh.name + "\t" + veh.price + "元/天\t载人:" + veh.mannedNumber + "人 载货:" + veh.carryFreightNumber + "吨";
}
return a;
}
public static void bill(Vehicle[] veh){
int l = veh.length;
String a1 = "";
String a2 = "";
int man = 0;
int goods = 0;
System.out.println("您的账单:");
for(int i = 0; i < l; i++){
if(veh[i] instanceof Carveh[i] instanceof PickUpTruck){
a1 = a1 + veh[i].name + "\t";
man += veh[i].mannedNumber;
}
if(veh[i] instanceof Vanveh[i] instanceof PickUpTruck){
a2 = a2 + veh[i].name + "\t";
goods += veh[i].carryFreightNumber;
}
}
System.out.println("***可载人的车有:");
System.out.println(a1 + "共载人:"+ man + "人");
System.out.println("***载货的车有:");
System.out.println(a2 + "共载货:"+ goods + "吨");
}
}
点击查看更多内容
9人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦