控制台测试:
欢迎使用租车系统
你是否要租车:1是 0否
1
您可租车的类型及其价目表
序号:1 汽车名称:奥迪A4 租金500/天 容量:最多载人4人
序号:2 汽车名称:马自达6 租金400/天 容量:最多载人4人
序号:3 汽车名称:皮卡雪6 租金450/天 容量:最多载人4人,最多载货2吨
序号:4 汽车名称:金龙 租金800/天 容量:最多载人20人
序号:5 汽车名称:松花江 租金400/天 容量:最多载货4吨
序号:6 汽车名称:依维柯 租金1000/天 容量:最多载货20吨
请输入您要租汽车的数量:
4
请输入您要租车的序号和天数
1 3
2 3
3 3
4 3
您的账单:
马自达6 3天
皮卡雪6 3天
金龙 3天
松花江 3天
总价格:6150元
1,父类
package rentcar;
//所有车的父类
public class Car {
private int num;//编号
private String name;
private int price;
public Car(int num, String name, int price) {
super();
this.num = num;
this.name = name;
this.price = price;
}
public Car() {}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String toString() {
return "序号:" + num +" "+ " 汽车名称:" + name+" "+ "租金" + price +"/天"+" ";
}
}
2,子类1
package rentcar;
//只用于载人的车
public class Manned extends Car{
private int personNum;
public Manned(int num, String name, int price, int personNum) {
super(num,name,price);
this.personNum = personNum;
}
public Manned() {}
public int getPersonNum() {
return personNum;
}
public void setPersonNum(int personNum) {
this.personNum = personNum;
}
public String toString() {
return super.toString()+"容量:"+"最多载人"+personNum+"人"+" ";
}
}
3,子类2
package rentcar;
//只用于载货的车
public class Goods extends Car{
private int weight;
public Goods(int num, String name, int price, int weight) {
super(num,name,price);
this.weight = weight;
}
public Goods() {}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String toString() {
return super.toString()+"容量:"+"最多载货"+weight+"吨"+" ";
}
}
4,子类3
package rentcar;
//既可以载人也可以载货
public class Pcar extends Car{
private int personNum;
private int weight;
public Pcar(int num, String name, int price, int personNum,int weight) {
super(num,name,price);
this.personNum=personNum;
this.weight = weight;
}
public Pcar(){}
public int getPersonNum() {
return personNum;
}
public void setPersonNum(int personNum) {
this.personNum = personNum;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String toString() {
return super.toString()+"容量:"+"最多载人"+personNum+"人,"+"最多载货"+weight+"吨";
}
}
5,主方法
package rentcar;
import java.util.*;
public class Test {
public static void main(String[] args) {
Car c1=new Manned(1,"奥迪A4",500,4);
Car c2=new Manned(2,"马自达6",400,4);
Car c3=new Pcar(3,"皮卡雪6",450,4,2);
Car c4=new Manned(4,"金龙",800,20);
Car c5=new Goods(5,"松花江",400,4);
Car c6=new Goods(6,"依维柯",1000,20);
Car[] cars = {c1,c2,c3,c4,c5,c6};
Scanner in=new Scanner(System.in);
System.out.println("欢迎使用租车系统");
System.out.println("你是否要租车:1是 0否");
int a=in.nextInt();
if(a==1)
{
System.out.println("您可租车的类型及其价目表");
for(int i=0;i
System.out.println(cars[i].toString());
}
System.out.println("请输入您要租汽车的数量:");
int b=in.nextInt();
System.out.println("请输入您要租车的序号和天数");
Car[] cars2=new Car[b];
int[] day=new int[b];
int s=0;
for(int i=0;i {
int c=in.nextInt();
int d=in.nextInt();
cars2[i]=cars[c];
day[i]=d;
s=s+cars[c].getPrice()*d;
}
System.out.println("您的账单:");
for(int i=0;i
System.out.println(cars2[i].getName()+" "+day[i]+"天");
}
System.out.println("总价格:"+s+"元");
}
else
System.out.println("再见");
}
}