Object-oriented programming exercises
Title description
Please make full use of object-oriented thinking to solve the problem of smart car rental for the company. According to the car model selected by the customer and the number of rental days, you can calculate the rental fee, the maximum number of passengers, and the maximum load.
The company currently has three models (bus, pickup truck, and truck), each of which has the attributes of a name and rent; among them: passenger cars can only carry people, trucks can only carry goods, and pickup trucks are both passenger and cargo vehicles. It can carry people and cargo.
The following is the available model, capacity and price list of DaDa Car Rental Company:
serial number name passenger capacity and cargo capacity rent
(person) (ton) (yuan/day)
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
Requirement: Calculate the total number of people that can be carried, the total quantity of goods and the total amount of the rental car according to the serial number and the number of days of the leased vehicle input by the customer.
The
first line of Input is an integer: it represents whether you want to rent a car or not 1—you want to rent a car (the program continues), 0—not to rent a car (the program ends); the
second line is an integer, which represents the number of cars to be rented N;
Next are N rows of data, each with 2 integers m and n, where m represents the number of the car to be rented, and n represents the number of days to rent the car.
Output
If the car is successfully rented, a row of data will be output, with a space between the data, meaning: the
total number of passengers and the total weight of the cargo (with 2 decimal places) rental amount (integer)
If not renting a car, output:
0 0.00 0 (the meaning is the same as above)
Sample
Input
1
2
1 1
2 2
Output
15 0.00 1600
Hint
answer
Java code
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 code
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)