目录
一丶功能展示
1、汽车租赁系统界面展示
2、类图
二、收获,体会及问题
拿道题先将工程和所有的包创建完成,先编写的父类的代码,没啥难度。然后让子类继承了父类,在子类给父类赋值时出现了问题,然后想到要用get()和set()方法,解决了问题。
在如何将子类的信息批量存储时遇到了问题,想到了用集合解决。创建了三个集合。
遇到问题,怎么在Main里调用class的Arraylist,查阅了相关资料,发现可以用数组来代替,于是换成了数组。
在父类中重写toString()是为了让父类返回值变成汽车相关的信息,否则返回的是这个类的命名空间的名字。
在父类中构建总租金的方法。
Abstract修饰符可以用于类、方法、事件和索引指示器(indexer),表示其为抽象成员,抽象方法是没有方法体的方法。抽象成员,即抽象类、抽象方法、抽象事件。
数组越界问题。发现自己少定义了一个数组元素。
要计算数组长度时,不知该用哪一个。故如下:
java中length,length(),和size()的区别:
1 java中的length属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性.
2 java中的length()方法是针对字符串String说的,如果想看这个字符串的长度则用到length()这个方法.
3.java中的size()方法是针对泛型集合说的,如果想看这个泛型有多少个元素,就调用此方法来查看!
Vehicle类
package RentCar;
public abstract class Vehicle {
private String vehicleId; //车牌号
private String brand; //品牌
private int perRent; //日租金
//get()和set()方法
public String getVehicleId() {
return vehicleId;
}
public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPerRent() {
return perRent;
}
public void setPerRent(int perRent) {
this.perRent = perRent;
}
//无参构造
public Vehicle() {
}
//有参构造
public Vehicle(String vehicleId, String brand,int perRent) {
this.vehicleId = vehicleId;
this.perRent = perRent;
this.brand = brand;
}
//抽象类方法totalmoney得总金额
public abstract double totalmoney(int days , double rent);
//重写toString(),得到返回值如下的方法
@Override
public String toString() {
return "汽车{" +
"车牌号='" + vehicleId + '\'' +
", 品牌='" + brand + '\'' +
", 日租金=" + perRent +
'}';
}
}
Bus类
package RentCar;
public class Bus extends Vehicle {
//添加客车座位这一属性
private String seat;
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
public Bus() {
super();
// TODO Auto-generated constructor stub
}
public Bus(String vehicleId, String brand, int rent, String seat) {
super(vehicleId, brand, rent);
this.seat = seat;
}
@Override
public double totalmoney(int days, double rent) {
// TODO Auto-generated method stub
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;
}
}
Car类
package RentCar;
public class Car extends Vehicle{
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car() {
super();
}
public Car(String vehicleId, String brand, int perRent, String type) {
super(vehicleId, brand, perRent);
this.type = type;
}
@Override
public String toString() {
return "Car{" +
"type='" + type + '\'' +
'}';
}
@Override
public double totalmoney(int days, double rent) {
// TODO Auto-generated method stub
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;
}
}
Trunk类
package RentCar;
public class Trunk extends Vehicle {
private int capacity;
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public Trunk() {
super();
// TODO Auto-generated constructor stub
}
public Trunk(String vehicleId, String brand, int rent,int capacity) {
super(vehicleId, brand, rent);
this.capacity = capacity;
}
@Override
public double totalmoney(int days, double rent) {
// TODO Auto-generated method stub
//与Bus相同
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;
}
}
RentMgrSys类
package cars;
import java.util.Scanner;
import RentCar.Bus;
import RentCar.Car;
import RentCar.Trunk;
public class RentMgrSys {
public static void main(String[] args) {
Car[] Cars = new Car[4];
Cars[0] = new Car("京NY28588", "宝马", 800, "x6");
Cars[1] = new Car("京CNY3284", "宝马", 600, "550i");
Cars[2] = new Car("京NT37465", "别克", 300, "林荫大道");
Cars[3] = new Car("京NT96928", "别克", 600, "GL8");
Bus[] Buses = new Bus[4];
Buses[0] = new Bus("京6566754", "金杯", 800, "16座");
Buses[1] = new Bus("京8696667", "金龙", 800, "16座");
Buses[2] = new Bus("京9696996", "金杯", 1500, "34座");
Buses[3] = new Bus("京8696998", "金龙", 1500, "34座");
Trunk[] Trunks = new Trunk[4];
Trunks[0] = new Trunk("川YX183296","奥铃",900,5);
Trunks[1] = new Trunk("川XG695640","奥铃",1000,5);
Trunks[2] = new Trunk("陕PC921127","域虎",1200,8);
Trunks[3] = new Trunk("陕BH920506","域虎",1300,8);
System.out.println("---------欢迎光临灿白汽车租赁公司---------");
System.out.println("1.轿车 2.客车 3.客车");
System.out.print("请选择你要租赁的汽车类型:");
Scanner sc = new Scanner(System.in);
int choose1 = sc.nextInt();
Car car1 = new Car();
Bus bus1 = new Bus();
Trunk trunk1 = new Trunk();
if(choose1 == 1) {
System.out.println("1.宝马 2.别克");
System.out.print("请选择你要租赁的品牌:");
int choose2 = sc.nextInt();
if(choose2 == 1) {
System.out.println("1.x6 2.550i");
}else {
System.out.println("1.林荫大道 2.GL8");
}
System.out.print("请输入你要租赁的汽车型号:");
String choose3 = sc.next();
for(int i = 0;i < Cars.length;i++) {
if(Cars[i].getType().equals(choose3)) {
car1 = Cars[i];
break;
}
}
System.out.print("请输入你要租赁的天数:");
int day = sc.nextInt();
System.out.print("分配给你的汽车牌号是:");
System.out.println(car1.getVehicleId());
double totalrent = 0;
totalrent = car1.totalmoney(day, car1.getPerRent());
System.out.print("你需要支付的租赁分费用是:");
System.out.print(totalrent);
}else if(choose1 == 2) {
System.out.println("1.金杯 2.金龙");
System.out.print("请选择你要租赁的汽车品牌:");
String choose2 = sc.next();
System.out.println("1.16座 2.34座");
System.out.print("请输入你要租赁的汽车座位数:");
String choose3 = sc.next();
for(int i = 0;i < Buses.length;i++) {
if(Buses[i].getSeat().equals(choose3)) {
bus1 = Buses[i];
break;
}
}
System.out.print("请输入你要租赁的天数:");
int day = sc.nextInt();
System.out.print("分配给你的汽车牌号是:");
System.out.println();
System.out.println(bus1.getVehicleId());
double totalrent = 0;
totalrent = bus1.totalmoney(day, bus1.getPerRent());
System.out.print("你需要支付的租赁分费用是:");
System.out.print(totalrent);
}else if(choose1 == 3) {
System.out.println("1.奥铃 2.域虎");
System.out.print("请选择你要租赁的品牌:");
int choose2 = sc.nextInt();
System.out.println("1.5 2.8");
System.out.print("请输入你要租赁的汽车承载量:");
int choose3 = sc.nextInt();
for(int i = 0;i < Trunks.length;i++) {
if(Trunks[i].getCapacity() == choose3) {
trunk1 = Trunks[i];
break;
}
}
System.out.print("请输入你要租赁的天数:");
int day = sc.nextInt();
System.out.print("分配给你的汽车牌号是:");
System.out.println(trunk1.getVehicleId());
double totalrent = 0;
totalrent = trunk1.totalmoney(day, trunk1.getPerRent());
System.out.print("你需要支付的租赁分费用是:");
System.out.print(totalrent);
}
}
}