6-2 停车场收费系统-21-jc-ST
分数 20
作者 yy
单位 西南石油大学
在停车场收费系统中,根据车型的不同按不同的单价和计费方式收取不同的停车费,程序设计如下:
有一个 Vehicle类,有如下属性和方法:
(1) price(单价)和minute(分钟)
(2) 构造函数:带2个输入参数:price 和minute;
(3)price 和minute的get/set方法
(4) float computeFee()计算停车费
Vehicle类和主类代码已经给出,你结合给出的代码,设计Vehicle的子类:Car和Bus。
Bus类 有私有成员:座位数 int seats
两个子类都重写继承的方法computeFee()来计算停车费:
轿车Car:每小时收费price元,未达1小时但超过30分钟按一小时费用计算,30分钟以内(包括30分钟)不收费
客车Bus:每小时收费price元,30分钟以内(包括30分钟)按半小时费用计费,未达1小时但超过30分钟按1小时费用计算
(提示 h=getMinute()/60 求得停车小时数,f=getMinute()%60求得超过的分钟数。对应客车Bus计费:如果f值为0,计费按h*price,30分钟以内(包括30分钟) 表示0<f<=30,计费按(h+0.5f)*price, 未达1小时但超过30分钟,表示 f>30,计费按(h+1)*price)
其中,停车费用每小时单价price,停车时间minutes和座位数seats 均通过键盘输入。
裁判测试程序:
在这里给出测试程序:
import java.util.Scanner;
class Vehicle {
private float price;
private int minute;
public Vehicle() {
}
public Vehicle(float price, int minute) {
this.price= price;
this.minute = minute;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public float computeFee(){
return 20.0f*minute/60;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Car c = new Car(4,78);
c.setPrice(sc.nextFloat());
c.setMinute(sc.nextInt());
System.out.printf("Car fee:%.2f\n",c.computeFee());
Bus b = new Bus();
b.setPrice(sc.nextFloat());
b.setMinute(sc.nextInt());
b.setSeats(sc.nextInt());
System.out.printf("Bus seats:%d,fee:%.2f\n",b.getSeats(),b.computeFee());
}
}
/* 请在这里填写答案 编写Car类和Bus类 */
输入样例:
第1行为car类型车的单价和停车分钟数
第2行为Bus类型车的单价和停车分钟数及座位数,例如:
8 40
15 40 30
输出样例:
在这里给出相应的输出。例如:
Car fee:8.00
Bus seats:30,fee:15.00
下面给出解答
class Car extends Vehicle{
//<=30不收费,>30 <60一个小时算
public Car(float price, int minute) {
super(price, minute);
}
public float computeFee(){
int h;
h=getMinute()/60;
int f=getMinute()%60;
if(f<=30&&f>=0){
return (float)(1.0*(h)* super.getPrice());
}else{
return (float)(1.0*(h+1)*super.getPrice());
}
}
}
class Bus extends Vehicle{
int seats;
public void setSeats(int seats) {
this.seats = seats;
}
public int getSeats() {
return seats;
}
public Bus() {
}
public Bus(float price, int minute, int seats) {
super(price, minute);
this.seats = seats;
}
public float computeFee(){
int h;
h=getMinute()/60;
int f=getMinute()%60;
if(f<=30&&f>0){
return (float) ((h+0.5)* super.getPrice());
}else if(f==0){
return (float)(1.0*(h)* super.getPrice());
//考虑一下f=0的情况
}
else{
return (float)(1.0*(h+1)* super.getPrice());
}
}
}
文章描述了一个基于Java的停车场收费系统的设计,Vehicle类具有单价和分钟属性以及计算费用的方法。Car类和Bus类作为Vehicle的子类,根据不同的计费规则重写了computeFee()方法。Car类的计费规则是每小时收费,不足1小时但超过30分钟按1小时计算,30分钟内免费。Bus类则根据座位数和超过30分钟的时间来计算费用,30分钟内按半小时费用,超过30分钟但不足1小时按1小时费用计算。

被折叠的 条评论
为什么被折叠?



