汽车租赁系统V1.0版,每天租金500元,输入租赁天数,输出租赁总金额。java代码 
时间: 2023-05-14 18:04:52 浏览: 16
以下是汽车租赁系统V1.0版的Java代码:
```
import java.util.Scanner;
public class CarRentalSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入租赁天数:");
int days = scanner.nextInt();
int total = days * 500;
System.out.println("租赁总金额为:" + total + "元");
}
}
```
您可以通过输入租赁天数来计算租赁总金额。每天的租金为500元。
相关问题
用Java写一个汽车租赁系统的代码,要用到GUI
这里给出一个简单的汽车租赁系统的Java代码实现,包含了基本的GUI界面。可以根据需求进行修改和扩展。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class CarRentalSystem extends JFrame implements ActionListener {
private ArrayList<Car> cars; // 汽车列表
private JComboBox<String> carComboBox; // 汽车下拉框
private JTextField rentDaysTextField; // 租车天数文本框
private JLabel priceLabel; // 租车价格标签
public CarRentalSystem() {
cars = new ArrayList<>();
cars.add(new Car("Toyota", "Camry", 300));
cars.add(new Car("Honda", "Accord", 350));
cars.add(new Car("Ford", "Mustang", 400));
// 创建界面
JPanel mainPanel = new JPanel(new GridLayout(4, 2));
JLabel carLabel = new JLabel("选择汽车");
carComboBox = new JComboBox<>();
for (Car car : cars) {
carComboBox.addItem(car.toString());
}
mainPanel.add(carLabel);
mainPanel.add(carComboBox);
JLabel rentDaysLabel = new JLabel("租车天数");
rentDaysTextField = new JTextField();
mainPanel.add(rentDaysLabel);
mainPanel.add(rentDaysTextField);
JButton rentButton = new JButton("租车");
rentButton.addActionListener(this);
mainPanel.add(rentButton);
JLabel priceTextLabel = new JLabel("租金");
priceLabel = new JLabel();
mainPanel.add(priceTextLabel);
mainPanel.add(priceLabel);
add(mainPanel, BorderLayout.CENTER);
setTitle("汽车租赁系统");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new CarRentalSystem();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("租车")) {
int index = carComboBox.getSelectedIndex();
Car car = cars.get(index);
int days = Integer.parseInt(rentDaysTextField.getText());
int price = car.rent(days);
priceLabel.setText(String.valueOf(price));
}
}
}
class Car {
private String make; // 品牌
private String model; // 型号
private int dailyPrice; // 每日租金
public Car(String make, String model, int dailyPrice) {
this.make = make;
this.model = model;
this.dailyPrice = dailyPrice;
}
public int rent(int days) {
return days * dailyPrice;
}
@Override
public String toString() {
return make + " " + model;
}
}
```
这个代码实现了一个简单的汽车租赁系统,用户可以选择要租赁的汽车和租车天数,系统会根据选择计算出租金并显示在界面上。在这个示例中,使用了JComboBox、JTextField、JButton和JLabel等Swing组件来构建GUI界面。可以根据需求进行修改和扩展。
java 计算一次租赁多辆汽车的总租金
在Java中计算一次租赁多辆汽车的总租金可以通过以下步骤实现:
1. 定义汽车对象和租车信息类:首先需要定义一个汽车对象类,其中包括汽车品牌、租金/天、数量等属性;然后定义一个租车信息类,包括租车天数、租车数量、所租汽车类型等属性。
2. 获取租车信息:通过用户输入或者从数据库中获取租车信息,对租车信息类进行赋值。
3. 计算租车总费用:根据所租车辆类型和租车天数计算每辆车的租车费用,然后将各辆车的租车费用相加得出总租金。
4. 输出计算结果:将计算得出的总租金打印输出,或者将其存入数据库等方便用户查看。
代码实现的一个例子如下所示:
public class Car {
String brand;
int rentPrice;
int amount;
public Car(String brand, int rentPrice, int amount) {
this.brand = brand;
this.rentPrice = rentPrice;
this.amount = amount;
}
}
public class RentCar {
int days;
int num;
Car car;
public RentCar(int days, int num, Car car) {
this.days = days;
this.num = num;
this.car = car;
}
public int getTotalFee() {
return this.days * this.num * this.car.rentPrice;
}
}
public class RentTest {
public static void main(String[] args) {
Car car1 = new Car("Audi", 500, 3);
Car car2 = new Car("BMW", 600, 2);
RentCar rentCar1 = new RentCar(5, 2, car1);
RentCar rentCar2 = new RentCar(7, 1, car2);
int totalFee = rentCar1.getTotalFee() + rentCar2.getTotalFee();
System.out.println("总租金为:" + totalFee);
}
}
在上述代码中,我们定义了Car类和RentCar类,其中Car类记录了汽车的品牌、租金和数量,RentCar类记录了租车天数、租车数量和租用的汽车类型。在RentTest类中,我们创建了两个Carbon类型的实例对象,并计算了总租金。最后输出总租金即可。
相关推荐








