Java编写汽车租赁系统
来源:blog.csdn.net/weixin_44889894
题目要求:
1、汽车租赁信息表如下:
2、类和属性:
3、运行效果:
效果实现:
代码实现:
1、车类:
package homework.exam;
public abstract class Vehicle {
private String num;
private String brand;
private double rent;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getRent() {
return rent;
}
public void setRent(double rent) {
this.rent = rent;
}
public Vehicle() {
}
//含参构造
public Vehicle(String num, String brand, double rent) {
this.num = num;
this.brand = brand;
this.rent = rent;
}
public String toString() {
return "汽车{" +
"车牌号='" + num + '\'' +
", 品牌='" + brand + '\'' +
", 日租金=" + rent +
'}';
}
public abstract double totalmoney(int days , double rent);
public abstract boolean equals(Vehicle o);
}
2、汽车类:
package homework.exam;
public class Cars extends Vehicle{
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Cars(String brand,String type) {
this.type = type;
}
public Cars(String num, String brand, double rent, String type) {
super(num, brand, rent);
this.type = type;
}
public String toString() {
return "Cars{" +
"type='" + type + '\'' +
'}';
}
//计算小汽车的总租金
public double totalmoney(int days, double rent) {
if (days>7){
return days*rent*0.9;
}else if (days>30){
return days*rent*0.8;
}else if (days>150){
return days*rent*0.7;
}
return days*rent;
}
//重写equals方法
public boolean equals(Vehicle o) {
if (o instanceof Cars){
Cars cars= (Cars) o;
return this.getType().equals(cars.getType())&&this.getBrand().equals(o.getBrand());
}
return false;
}
}
3、客车类:
package homework.exam;
public class Bus extends Vehicle {
private String seat;
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
public Bus(String num, String brand, double rent, String seat) {
super(num, brand, rent);
this.seat = seat;
}
//计算客车的租金
public double totalmoney(int days, double rent) {
if (days>=3){
return days*rent*0.9;
}else if (days>=7){
return days*rent*0.8;
}else if (days>=30){
return days*rent*0.7;
}else if (days>=150){
return days*rent*0.6;
}
return days*rent;
}
//重写equals方法
public boolean equals(Vehicle o) {
return false;
}
}
4、车辆管理类:
package homework.exam;
public class CarRent {
//创建汽车数组,将汽车的信息放在数组中
public Cars[] carMake(){
Cars c1 = new Cars("京NY28588", "宝马", 800, "x6");
Cars c2 = new Cars("京CNY3284", "宝马", 600, "550i");
Cars c3 = new Cars("京NT37465", "别克", 300, "林荫大道");
Cars c4 = new Cars("京NT96928", "别克", 600, "GL8");
Cars[] arr1 ={c1,c2,c3,c4};
return arr1;
}
//创建客车数组,将汽车的信息放在数组中
public Bus[] busMake(){
Bus b1 = new Bus("京6566754", "金杯", 800, "16座");
Bus b2 = new Bus("京8696667", "金龙", 800, "16座");
Bus b3 = new Bus("京9696996", "金杯", 1500, "34座");
Bus b4 = new Bus("京8696998", "金龙", 1500, "34座");
Bus[] arr2={b1,b2,b3,b4};
return arr2;
}
}
5、业务服务类:
package homework.exam;
import java.util.Scanner;
public class CarService {
public void rentcar(){
System.out.println("**********欢迎光临秋名山守望者汽车租赁公司**********");
Scanner sc = new Scanner(System.in);
System.out.println("1,轿车 2,客车");
System.out.print("请输入您要租赁的汽车类型:");
int i = sc.nextInt();
CarRent carRent = new CarRent(); //创建车库对象
Cars[] cars = carRent.carMake(); //拿到轿车数组对象
Cars car=null;
Bus[] buses = carRent.busMake(); //拿到客车数组对象
Bus bus=null;
//判断用户选择的车型
if (i==1){
System.out.print("请选择你要租赁的汽车品牌:(1,别克 2,宝马)");
int i1 = sc.nextInt();
if (i1==1){
System.out.print("请输入你要租赁的汽车类型:(1,林荫大道 2,GL8 )");
}else {
System.out.print("请输入你要租赁的汽车类型:(1,x6 2,550i )");
}
String i2 = sc.next();
//遍历汽车数组,拿到用户选择的汽车
for (int j = 0; j < cars.length; j++) {
if (cars[j].getType().equals(i2)){ //当选择的车的类型与数组中的匹配时
car=cars[j]; //将车赋值给car
break;
}
}
System.out.print("请输入你要租赁的天数:");
int days = sc.nextInt();
System.out.print("分配给你的汽车牌号是:");
System.out.println(car.getNum()); //获取汽车的车牌
double totalmoney =0; //调用total
totalmoney = car.totalmoney(days, car.getRent()); //计算用户的租金
System.out.print("你需要支付的租赁分费用是:");
System.out.print(totalmoney);
}else if (i==2){
System.out.print("请选择你要租赁的汽车品牌:(1,金龙 2,金杯)");
String i2 = sc.next();
System.out.print("请输入你要租赁的汽车座位数:(1,16座 2,34座)");
String i3 = sc.next();
//遍历客车数组,拿到用户选择的客车
for (int j = 0; j < buses.length; j++) {
//当输入的客车的车牌和座位与数组中的相等,就选出用户选择的车
if (buses[j].getBrand().equals(i2)&&buses[j].getSeat().equals(i3)){
bus=buses[j]; //将选择的车辆赋值给bus
break;
}
}
System.out.print("请输入你要租赁的天数:");
int days = sc.nextInt();
System.out.print("分配给你的汽车牌号是:");
System.out.println();
System.out.println(bus.getNum()); //拿到用户选择的车牌号
double totalmoney = 0; //调用totalmoney方法
totalmoney=bus.totalmoney(days, bus.getRent()); //用用户输入的天数。来计算租金
System.out.print("你需要支付的租赁分费用是:");
System.out.print(totalmoney);
}
}
}
6、测试类:
package homework.exam;
public class Test {
public static void main(String[] args) {
CarService cs = new CarService();
cs.rentcar();
}
}
控制台输入的内容,我选择的是输入字符串类型,没有按照效果图上,如果你做的话,你可以用三元运算符来实现哦!
评论