这个小项目是学完java面相对象的练习小项目,还有很多知识点没有用上,是因为并没有完全吸收所学的知识。粗略的完成了这个小项目希望大家指点!
创建Car父类
package com.car;
public class Car {
private String type;
private int index;
private int price;
private int capacity;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public Car(){
}
public Car(String type,int index,int price,int capacity){
this.setType(type);
this.setIndex(index);
this.setPrice(price);
this.setCapacity(capacity);
}
}
创建轿车子类并继承Car父类
package com.car;
public class smallCar extends Car {
}
创建货车子类并继承Car父类
package com.car;
public class Truck extends Car {
}
创建皮卡子类并继承Car父类
package com.car;
public class Pickup extends Car {
}
测试主程序
package com.car;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//欢迎语
System.out.println("欢迎来到答答租车系统!\n您是否要租车:1是 0否");
//输入工具
Scanner input=new Scanner(System.in);
//判断用户是否需要租车
if(input.nextInt()!=1){
return;
}
//展示清单
System.out.println("您可以租的车型及价格表!");
//初始化对象长度
Car [] cars=new Car[6];
//定义数组存放汽车类型
String [] type={
"奥迪a4",
"马自达6",
"皮卡",
"金龙",
"松花江",
"依维柯"
};
//定义数组存放汽车价格
int [] price={
500,
400,
450,
800,
400,
1000
};
//定义数组存放汽车容量
int [] capacity={
4,
4,
4,
20,
4,
20
};
//定义循环变量
int i=0;
//打印表格
System.out.println("类型"+" "+"序号"+" "+"金额"+" "+"容量");
//循环实例化对象并为属性赋值以及显示表格的内容
while(i
cars[i]=new Car(type[i],i,price[i],capacity[i]);
System.out.println(
type[i]+" "+
i+" "+
price[i]+" "+
capacity[i]
);
i++;
}
System.out.println("请输入您要租汽车的序号:");
int index=input.nextInt();
System.out.println("请输入您要租汽车的数量:");
int num=input.nextInt();
System.out.println("请输入您要租汽车的天数:");
int day=input.nextInt();
String carName=null; int carCount=0; int dayCount=0; int priceCount=0;
int capCount=0;
carName=type[index];
carCount+=num;
dayCount+=day;
priceCount=(price[index]*carCount)*dayCount;
capCount+=carCount*capacity[index];
System.out.println(
"您要租的汽车为:"+carName+" "+
"数量为:"+carCount+" "+
"天数为:"+dayCount+" "+
"金额为:"+priceCount+" "+
"总容量为:"+capCount
);
System.out.println("");
System.out.println("感谢您使用答答租车服务,欢迎您的再次光临!");
}
}