java二嗨租车项目_Java入门第二季6-1租车项目代码

一共有5个类

Car类 //作为父类

package com.imooc;

public class Car {

public int ID;

public String name;

public int rent;

public int PersonCapacity;

public int GoodsCapacity;

}

2.passengerCar //客车

public class passengerCar extends Car{

public passengerCar(int newID,String newName,int newRent,int newPersonCapacity){

this.ID = newID;

this.name = newName;

this.rent = newRent;

this.PersonCapacity = newPersonCapacity;

}

}

3.Pickup //皮卡

public class Pickup extends Car{

public Pickup(int newID,String newName,int newRent,int newPersonCapacity,int newGoodsCapacity){

this.ID = newID;

this.name = newName;

this.rent = newRent;

this.PersonCapacity = newPersonCapacity;

this.GoodsCapacity = newGoodsCapacity;

}

}

4.Trunk //货车

public class Trunk extends Car{

public Trunk(int newID,String newName,int newRent,int newGoodsCapacity){

this.ID = newID;

this.name = newName;

this.rent = newRent;

this.GoodsCapacity = newGoodsCapacity;

}

}

5.StartMain // 主函数

package com.imooc;

import java.util.*;

public class StartMain {

public static void main(String[] args){

Car[] AllCar = {new passengerCar(1,"奥迪A4",500,4),new passengerCar(2,"马自达6",400,4),new Pickup(3,"皮卡雪",450,4,2),new passengerCar(4,"金龙",800,20),new Trunk(5,"松花江",400,4),new Trunk(6,"依维柯",1000,20)};

System.out.println("欢迎来到租车系统");

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

Scanner input = new Scanner(System.in);

int Acess = input.nextInt();

if(Acess!=1)

return;

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

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

for(int i=0;i

if(AllCar[i] instanceof passengerCar) {

System.out.printf("%-1d. %-6s%-4d元/天 载人:%-1d人%n",AllCar[i].ID,AllCar[i].name,AllCar[i].rent,AllCar[i].PersonCapacity);

}

else if(AllCar[i] instanceof Trunk){

System.out.printf("%-1d. %-6s%-4d元/天 载货:%-1d吨%n",AllCar[i].ID,AllCar[i].name,AllCar[i].rent,AllCar[i].GoodsCapacity);

}

else{

System.out.printf("%-1d. %-6s%-4d元/天 载人:%-1d人 载货:%-1d吨%n",AllCar[i].ID,AllCar[i].name,AllCar[i].rent,AllCar[i].PersonCapacity,AllCar[i].GoodsCapacity);

}

}

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

int Num = input.nextInt();

//创建一个Car类,存储被租的汽车对象

Car[] odered = new Car[Num];

//输入汽车的序号

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

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

//汽车序号

int id = input.nextInt();

//通过遍历AllCar里所有汽车的ID,把用户选择的车对象放进odered数组

for(int j = 0;j

if(AllCar[j].ID == id){

odered[i]=AllCar[j];

break;

}

}

}

System.out.println("请输入租车天数");

int days = input.nextInt();

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

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

int PassengerNum=0;

int TotalPrice=0;

//这里的算价钱还有问题,皮卡算了两次价格,待修改。

//随便修改了一下,第二次屏蔽了皮卡的租金计算,还待优化

for(int i =0;i

if(odered[i] instanceof passengerCar || odered[i] instanceof Pickup){

System.out.println(odered[i].name);

PassengerNum += odered[i].PersonCapacity;

TotalPrice += odered[i].rent;

// System.out.println("第"+(i+1)+"台车的价钱为:"+odered[i].rent);

}

}

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

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

int GoodsNum=0;

for(int i = 0;i

if(odered[i] instanceof Trunk || odered[i] instanceof Pickup){

System.out.println(odered[i].name);

GoodsNum +=odered[i].GoodsCapacity;

if(odered[i] instanceof Trunk) {

TotalPrice += odered[i].rent;

}

// System.out.println("第"+(i+1)+"台车的价钱为:"+odered[i].rent);

}

}

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

System.out.printf("***租车总价格:"+(TotalPrice*3)+"元");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值