java第二季答答租车系统作业,在参考了几位同学的作业后完成,还有待完善!
Car.java
package com.imooc;
public class Car {
int id;
String name;
double price;
int cargoCapacity;
int personCapacity;
public double getPrice(){
return price;
}
public void getMessage(){
System.out.println(id + "\t" + name + "\t" + price + "元/天 \t" + "载人:" + personCapacity + "人; " + "载货:" + cargoCapacity + "吨 ");
}
}
PersonCar.java
package com.imooc;
public class PersonCar extends Car {
public PersonCar(int id,String name,double price,int personCapacity){
this.id=id;
this.name=name;
this.price=price;
this.personCapacity=personCapacity;
}
public int getPersonCapacity(){
return personCapacity;
}
}
CargoCar.java
package com.imooc;
public class CargoCar extends Car {
public CargoCar(int id,String name,double price,int cargoCapacity){
this.id=id;
this.name=name;
this.price=price;
this.cargoCapacity=cargoCapacity;
}
public int gerCargoCapacity(){
return cargoCapacity;
}
}
PickUp.java
package com.imooc;
public class PickUp extends Car {
public PickUp(int id,String name,double price,int cargoCapacity,int personCapacity){
this.id=id;
this.name=name;
this.price=price;
this.cargoCapacity=cargoCapacity;
this.personCapacity=personCapacity;
}
public int gerCargoCapacity(){
return cargoCapacity;
}
public int gerPersonCapacity(){
return personCapacity;
}
}
CarRental.java
package com.imooc;
import java.util.Scanner;
public class CarRental {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car car1 = new PersonCar(1,"奥迪A4",500.0,4);
Car car2 = new PersonCar(2,"马自达6",400.0,4);
Car car3 = new PickUp(3,"皮卡雪6",450.0,2,4);
Car car4 = new PersonCar(4,"金龙",800.0,20);
Car car5 = new CargoCar(5,"松花江",400,4);
Car car6 = new CargoCar(6,"依维柯",1000,20);
Car[] cars = {car1,car2,car3,car4,car5,car6};
int personNum=0;
int cargoNum=0;
double priceNum=0;
String cargoCarName="";
String personCarName="";
System.out.println("********欢迎使用答答租车系统********");
System.out.println("您是否需要租车:1.是 0.否");
Scanner input = new Scanner(System.in);
int step = input.nextInt();
switch(step){
case 0:
System.out.println("期待您下次使用答答租车系统!");
break;
case 1:
System.out.println("您可租用的车辆类型及价格为:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
for(int i=0;i<cars.length;i++){
cars[i].getMessage();
}
System.out.println("请输入您需要租车的数量:");
int rentNum=input.nextInt();
for(int j=1;j<=rentNum;j++){
System.out.println("请输入第"+j+"量车的序号");
int id=input.nextInt();
priceNum+=cars[id-1].getPrice();
if(cars[id-1].personCapacity!=0){
personCarName+=cars[id-1].name;
personNum+=cars[id-1].personCapacity;
}
if(cars[id-1].cargoCapacity!=0){
cargoCarName+=cars[id-1].name;
cargoNum+=cars[id-1].cargoCapacity;
}
}
System.out.println("请输入您要租车的天数:");
int day=input.nextInt();
System.out.println("您的账单:");
System.out.println("可载人的车有:"+personCarName);
System.out.println("共载人:"+personNum+"人");
System.out.println("可载货的车有:"+cargoCarName);
System.out.println("共载货:"+cargoNum+"吨");
System.out.println("租车总价格为:"+priceNum*day+"元");
break;
default:
System.out.println("您的输入不正确,请重新输入!");
}
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦