答答租车系统 java and python 题解

面向对象编程的练习

题目描述

X - 答答租车系统(面向对象综合练习) Description 各位面向对象的小伙伴们,在学习了面向对象的核心概念——类的封装、继承、多态之后,答答租车系统开始营运了。

请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。

公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。

下面是答答租车公司的可用车型、容量及价目表:
序号 名称 载客量 载货量 租金
(人) (吨) (元/天)
1 A 5 800
2 B 5 400
3 C 5 800
4 D 51 1300
5 E 55 1500
6 F 5 0.45 500
7 G 5 2.0 450
8 H 3 200
9 I 25 1500
10 J 35 2000

要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数、货物总数量及租车费用总金额。

Input
首行是一个整数:代表要不要租车 1——要租车(程序继续),0——不租车(程序结束);
第二行是一个整数,代表要租车的数量N;

接下来是N行数据,每行2个整数m和n,其中:m表示要租车的编号,n表示租用该车型的天数。

Output
若成功租车,则输出一行数据,数据间有一个空格,含义为:
载客总人数 载货总重量(保留2位小数) 租车金额(整数)

若不租车,则输出:

0 0.00 0(含义同上)

Sample
Input
1
2
1 1
2 2
Output
15 0.00 1600
Hint

题解

本体主要是练习面向对象的编程思维,主要应用了类的继承,这里用 javapython来做一下对比,不恰当的地方还请大佬指正。

Java代码

import java.math.BigInteger;
import java.util.Scanner;

class car 
{
    
    
	int rent;
	public car(int rent) {
    
    
		super();
		this.rent = rent;
	}
}

class bus extends car 
{
    
    
	int person;
	public bus( int person,int rent) {
    
    
		super(rent);
		this.person = person;
	}
}

class truck extends car 
{
    
    
	double cargo;
	public truck(double cargo,int rent) 
	{
    
    
		super(rent);
		this.cargo = cargo;
	}
}

class pick extends car 
{
    
    
	double cargo;
	int person;

	public pick( int person,double cargo,int rent) {
    
    
		super(rent);
		this.cargo = cargo;
		this.person = person;
	}
}

public class Main {
    
    
	public static void main(String[] args) 
	{
    
    
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) 
		{
    
    
			car[] cars = {
    
     new bus(5, 800), new bus(5, 400), new bus(5, 800), new bus(51, 1300),
					new bus(55, 1500), new pick(5, 0.45, 500), new pick(5, 2.0, 450),
					new truck(3, 200), new truck(25, 1500), new truck(35, 2000), };
			int psum = 0, rsum = 0;
			double csum = 0;
			int flag = cin.nextInt();
			if (flag == 0) {
    
    
				System.out.printf("%d %.2f %d\n", psum, csum, rsum);
				break;
			} 
			else 
			{
    
    
				int n = cin.nextInt();
				for (int i = 0; i < n; i++) 
				{
    
    
					int num = cin.nextInt();
					int day = cin.nextInt();
					if (cars[num - 1] instanceof bus) 
					{
    
    
						bus buss = (bus) cars[num - 1];
						psum += buss.person * day;
					} 
					else if (cars[num - 1] instanceof pick) 
					{
    
    
						pick picks = (pick) cars[num - 1];
						psum += picks.person * day;
						csum += picks.cargo * day;
					} 
					else if (cars[num - 1] instanceof truck) 
					{
    
    
						truck trucks = (truck) cars[num - 1];
						csum += trucks.cargo * day;
					}
					rsum += cars[num - 1].rent * day;
				}
				System.out.printf("%d %.2f %d\n", psum, csum, rsum);
			}
		}
	}
}

Python代码

class car:
    def __init__(self, num, money):
        self._num = num
        self._money = money

class bus(car):
    def __init__(self, num, money, people):
        car.__init__(self, num, money)
        self._people = people

class truck(car):
    def __init__(self, num, money, huo):
        car.__init__(self, num, money)
        self._huo = huo

class pick(car):
    def __init__(self, num, money, people, huo):
        car.__init__(self, num, money)
        self._people = people
        self._huo = huo

k1 = bus(1, 800, 5)
k2 = bus(2, 400, 5)
k3 = bus(3, 800, 5)
k4 = bus(4, 1300, 51)
k5 = bus(5, 1500, 55)
k6 = pick(6, 500, 5, 0.45)
k7 = pick(7, 450, 5, 2)
k8 = truck(8, 200, 3)
k9 = truck(9, 1500, 25)
k10 =truck(10, 2000, 35)

che = [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10]

while True:
    t=input().split()
    psum=0
    hsum=0
    msum=0
    if int(t[0])==0:
        print(psum, "%.2lf" % hsum, msum)
        break 
    else:
        n=int(input())
        for i in range(0,n):
                num,day=map(int,input().split(' '))
                if isinstance(che[num-1], bus):
                    psum+=che[num-1]._people*day
                if isinstance(che[num-1],truck):
                    hsum+=che[num-1]._huo*day
                if isinstance(che[num-1],pick):
                    psum+=che[num-1]._people*day
                    hsum += che[num - 1]._huo*day
                msum+=che[num-1]._money*day
        print(psum,"%.2lf" % hsum,msum)

猜你喜欢

转载自blog.csdn.net/rookie636/article/details/109535750