父类Car
public abstract class Car {
public String carType;
public int rentPrice;
}
载人接口
public interface IPassengerCapacity {
public int getcarrypassenger();
}
载货接口
public interface IGoodsCapacity {
public int getcarrygoods();
}
载人类
public class passengerCar extends Car implements IPassengerCapacity {
private int carrypassenger;//载客量
//构造方法
passengerCar(String type,int rentprice,int carrypassenger){
this.carType=type;
this.rentPrice=rentprice;
this.carrypassenger=carrypassenger;
}
public int getcarrypassenger() {
// TODO Auto-generated method stub
return carrypassenger;
}
}
卡车类
public class Trunk extends Car implements IGoodsCapacity {
private int carrygoods;
Trunk(String type,int rentprice,int carrygoods){
this.carType=type;
this.rentPrice=rentprice;
this.carrygoods=carrygoods;
}
public int getcarrygoods() {
// TODO Auto-generated method stub
return carrygoods;
}
}
皮卡类
public class PickUp extends Car implements IPassengerCapacity, IGoodsCapacity {
private int carrypassenger;//载客量
private int carrygoods;
PickUp(String type,int rentprice,int carrypassenger,int carrygoods){
this.carType=type;
this.rentPrice=rentprice;
this.carrypassenger=carrypassenger;
this.carrygoods=carrygoods;
}
public int getcarrypassenger() {
// TODO Auto-generated method stub
return carrypassenger;
}
public int getcarrygoods() {
// TODO Auto-generated method stub
return carrygoods;
}
}
主函数
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car[] carsforRnet={new passengerCar("奥迪A4",500,4),new passengerCar("马自达6",400,4),new PickUp("皮卡雪6",450,4,2),new passengerCar("金龙",800,20),new Trunk("松花江",400,4),new Trunk("依维柯",1000,20)};
System.out.println("欢迎进入答答租车系统:");
System.out.println("您是否要租车:1:是 0:否");
Scanner sn=new Scanner(System.in);
String input=sn.next();
int num=1;
if (input.equals("1")) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
for (Car newcar:carsforRnet){
if (newcar instanceof passengerCar) {
System.out.println(num + ".\t" + newcar.carType + "\t" + newcar.rentPrice + "元/天" + "\t" + "载人:"+ ((passengerCar)newcar).getcarrypassenger() + "人"); num++; }
if (newcar instanceof PickUp) {
System.out.println(num + ".\t" + newcar.carType + "\t" + newcar.rentPrice + "元/天" + "\t" + "载人:"+ ((PickUp)newcar).getcarrypassenger() + "人"+ "载货:"+ ((PickUp)newcar).getcarrygoods() + "吨"); num++; }
if (newcar instanceof Trunk) {
System.out.println(num + ".\t" + newcar.carType + "\t" + newcar.rentPrice + "元/天" + "\t" + "载货:"+ ((Trunk)newcar).getcarrygoods() + "吨"); num++; }
}
System.out.println("请输入要租汽车的数量:");
int carNum= sn.nextInt();//使用汽车数量
if(carNum<0){
System.out.println("输入有误,请重新输入!");
System.out.println("请输入要租汽车的数量:");
carNum= sn.nextInt();//使用汽车数量
}
int[] carNums = new int[carNum];
for (int i = 0; i < carNums.length; i++) {
System.out.println("请输入第"+(i+1)+"辆车的序号");
carNums[i]=sn.nextInt();
if((carNums[i]>6)||(carNums[i]<0)){
System.out.println("输入有误,请重新输入第"+(i+1)+"辆车的序号");
if(i>=0){
i--;
}
else {
i=0;
}
continue;
}
}
System.out.println("请输入用车天数:");
int useDays=sn.nextInt();
if(useDays<0){
System.out.println("输入有误,请重新输入!");
System.out.println("请输入用车天数:");
useDays= sn.nextInt();//使用汽车数量
}
int Sumcountpeople = 0;
int Sumcountgoods = 0;
int rent=0;
System.out.println("您的账单:\n***可载人的车有:");
for (int i = 0; i < carNums.length; i++) {
if ((carsforRnet[carNums[i] - 1] instanceof passengerCar) || (carsforRnet[carNums[i] - 1] instanceof PickUp))
{
System.out.print(carsforRnet[carNums[i] - 1].carType + "\t");
if((carsforRnet[carNums[i] - 1] instanceof passengerCar)){
Sumcountpeople = Sumcountpeople+((passengerCar)carsforRnet[carNums[i] - 1]).getcarrypassenger(); }
if((carsforRnet[carNums[i] - 1] instanceof PickUp)){
Sumcountpeople = Sumcountpeople+((PickUp)carsforRnet[carNums[i] - 1]).getcarrypassenger(); }
}
}
System.out.println("共可载人:" + Sumcountpeople + "人!");
System.out.println("***可载货的车有:");
for (int i = 0; i < carNums.length; i++) {
if ((carsforRnet[carNums[i] - 1] instanceof Trunk) || (carsforRnet[carNums[i] - 1] instanceof PickUp))
{
System.out.print(carsforRnet[carNums[i] - 1].carType + "\t");
if((carsforRnet[carNums[i] - 1] instanceof Trunk)){
Sumcountgoods = Sumcountgoods+((Trunk)carsforRnet[carNums[i] - 1]).getcarrygoods(); }
if((carsforRnet[carNums[i] - 1] instanceof PickUp)){
Sumcountgoods = Sumcountgoods+((PickUp)carsforRnet[carNums[i] - 1]).getcarrygoods(); }
}
}
System.out.println("共可载货:" + Sumcountgoods + "吨!");
for (int i = 0; i < carNums.length; i++) {
rent=rent+(carsforRnet[carNums[i] - 1]).rentPrice;
}
rent=rent*useDays;
System.out.println("***租车总价格:" + rent + "元!");
}
else {
System.out.println("感谢使用再见");
}
}
}
点击查看更多内容
45人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦