java 写的租车系统

//创建车的父类
public class Car {
    private String name;
    private int people;
    private int burden;
    private double salary;
    
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getBurden() {
return burden;
}
public void setBurden(int burden) {
this.burden = burden;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
    

}

//创建具体实现类


//汽车类
public class PassagerCar extends Car{
    String name;
    int people;
    double salary;
    public PassagerCar(String name,int people,double salary) {
    this.name=name;
    this.people=people;
    this.salary=salary;
    }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}

}


//皮卡类
public class Pickup extends Car {
String name;
int people;
int burden;
double salary;


public Pickup(String name, int people, int burden, double salary) {
this.name = name;
this.people = people;
this.burden = burden;
this.salary = salary;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getPeople() {
return people;
}


public void setPeople(int people) {
this.people = people;
}


public int getBurden() {
return burden;
}


public void setBurden(int burden) {
this.burden = burden;
}


public double getSalary() {
return salary;
}


public void setSalary(double salary) {
this.salary = salary;
}

}

//卡车类



public class Truck extends Car {
String name;
int burden;
double salary;


public Truck(String name, int burden, double salary) {
this.name = name;
this.burden = burden;
this.salary = salary;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getBurden() {
return burden;
}


public void setBurden(int burden) {
this.burden = burden;
}


public double getSalary() {
return salary;
}


public void setSalary(double salary) {
this.salary = salary;
}


}

//测试类

import java.lang.reflect.Executable;
import java.util.Scanner;


import org.omg.CORBA.PUBLIC_MEMBER;


public class Test {
int number = 1;
int people = 0;
int burden = 0;
double salary = 0.0;
public void execute() {
System.out.println("欢迎进入打车系统");
Car[] pp = { new PassagerCar("奥迪A8", 5, 800), new Truck("大卡车", 500, 550), new Truck("小卡车", 300, 450),
new Pickup("皮卡", 4, 251, 300), new PassagerCar("迈凯伦570GT", 2, 3000) };
System.out.println("你需要租车吗?1需要,2不需要");
Scanner a = new Scanner(System.in);
int input = a.nextInt();
if (input == 1) {
for (Car che : pp) {
System.out.print(number + ".");
if (che instanceof PassagerCar) {
System.out.println(che.getName() + "\t" + che.getPeople() + "人\t" + che.getSalary() + "元/天\t");
}
if (che instanceof Truck) {
System.out.println(che.getName() + "\t" + che.getBurden() + "吨\t" + che.getSalary() + "元/天\t");
}
if (che instanceof Pickup) {
System.out.println(che.getName() + '\t' + che.getPeople() + "人\t" + che.getBurden() + "吨"
+ che.getSalary() + "元/天\t");
}
number++;
}
System.out.println("请输入租车数量");
int b = a.nextInt();
for (int i = 1; i <= b; i++) {
System.out.println("请输入第" + i + "辆车序号:");
int choose = a.nextInt();
System.out.println("你选择的是" + choose + "号车型");
if (choose == 1) {
people += pp[0].getPeople();
salary += pp[0].getSalary();
}
if (choose == 2) {
burden += pp[1].getBurden();
salary += pp[1].getSalary();


}
if (choose == 3) {
burden += pp[2].getBurden();
salary += pp[2].getSalary();
}
if (choose == 4) {
people += pp[3].getPeople();
burden += pp[3].getBurden();
salary += pp[3].getSalary();
}
if (choose == 5) {
people += pp[4].getPeople();
salary += pp[4].getSalary();
}
}
System.out.println("请输入租车天数:");
int allday = a.nextInt();
people = people * allday;
burden = burden * allday;
salary = salary * allday;
System.out.println("您本次共租 " + b + " 辆车,租期为" + allday + "  天,总载客量为:" + people + "  人,总载货量为:" + burden
+ " 吨, 总金额为:" + salary+"元");
System.out.println("如果无误请选择 1确认 ,有问题请选择 2重新租车");
int c = a.nextInt();
if(c==1) {
System.out.println("欢迎下次光临");
}
else {
execute();
}
} else {
System.out.println("Bye!Bye!");
}
}
public static void main(String args[]) {
Test object = new Test();
object.execute();
}
}

猜你喜欢

转载自blog.csdn.net/struggle_pan/article/details/79654772