首先创建一个汽车的抽象类CarStore,里面包含汽车的编号ID,车名Name以及价格Price,并且创建set和get方法。
package cw.lojako.carrent;
public abstract class CarStore {
private int id;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
之后我们可以创建两个接口,载客量(CarryingPeople),载货量(CarryingGoods)。
这里引出一个问题:什么时候用接口呢?就是一个功能并不是所有对象都拥有时,可以把这个功能写进接口中,如若该对象需要此功能则实现该接口即可。就拿我们这个例子而言,货车的载货功能客车并不具备,所以需要用接口来实现。
接口(CarryingPeople)
package cw.lojako.carrent;
public interface CarryingPeople {
public void setPeople(int n);
public int getPeople();
}
接口(CarryingGoods)
package cw.lojako.carrent;
public interface CarryingGoods {
public void setGoods(int n);
public int getGoods();
}
第三步就是创建客车类,货车类,皮卡类并继承抽象类CarStore,实现相对应的接口。代码如下:
客车类(Car.java)
package cw.lojako.carrent;
public class Car extends CarStore implements CarryingPeople{
private int people;
//初始化值
public Car(int id,String name,double price,int people){
super.setId(id);
super.setName(name);
super.setPrice(price);
this.setPeople(people);
}
@Override
public void setPeople(int people) {
this.people = people;
}
@Override
public int getPeople() {
return people;
}
@Override
public String toString() {
return "编号:"+super.getId()+"\t"+"车名:"+super.getName()+"\t"+"价格:"+super.getPrice()+"元/天\t"+"载客量:"+this.getPeople()+"人";
}
}
货车类(Van)
package cw.lojako.carrent;
public class Van extends CarStore implements CarryingGoods {
private int goods;
//初始化值
public Van(int id,String name,double price,int goods){
super.setId(id);
super.setName(name);
super.setPrice(price);
this.setGoods(goods);
}
@Override
public void setGoods(int goods) {
this.goods = goods;
}
@Override
public int getGoods() {
return goods;
}
@Override
public String toString() {
return "编号:"+super.getId()+"\t"+"车名:"+super.getName()+"\t"+"价格:"+super.getPrice()+"元/天\t"+"载货量:"+this.getGoods()+"T";
}
}
皮卡类(PickUp)
package cw.lojako.carrent;
public class PickUp extends CarStore implements CarryingGoods,CarryingPeople{
private int goods;
private int people;
//初始化值
public PickUp(int id,String name,double price,int people,int goods){
super.setId(id);
super.setName(name);
super.setPrice(price);
this.setPeople(people);
this.setGoods(goods);
}
@Override
public void setGoods(int goods) {
this.goods = goods;
}
@Override
public int getGoods() {
return goods;
}
@Override
public void setPeople(int people) {
this.people = people;
}
@Override
public int getPeople() {
return people;
}
@Override
public String toString() {
return "编号:"+super.getId()+"\t"+"车名:"+super.getName()+"\t"+"价格:"+super.getPrice()+"元/天\t"+"载客量:"+this.getPeople()+"人"+" "+"载货量:"+this.getGoods()+"T";
}
}
最后一步是写主页面
package cw.lojako.carrent;
import java.util.Scanner;
public class RentSystem {
CarStore[] cars ={new Car(1,"奥迪A4",500,4),
new Car(2,"马自达",400,4),
new PickUp(3,"皮卡雪",450,4,20),
new Car(4,"保时捷",800,20),
new Van(5,"松花江",400,4),
new Van(6,"依维柯",1000,20)};
Scanner in = new Scanner(System.in);
public void rent(){
System.out.println("欢迎使用答答租车系统: \n是否租车??? 1(是) 0(否)");
int input = in.nextInt();
if(input==1){
System.out.println("您可租车的类型和价目表: "+"\n 序号"+"======"+"车名"+"==============="+"租金"+"==========="+"容量");
for(int i=0;i<cars.length;i++){
System.out.println(cars[i]);
}
}else if(input==0){
System.out.println("欢迎下次光临,亲(づ ̄3 ̄)づ╭~");
System.exit(0);
}else{
System.out.println("程序自爆系统已经启动,要命的赶紧跑!!!");
System.exit(0);
}
String carName = " "; //车名
double totalPrice = 0; //租金总额
int totalGoods = 0; //总载货量
int totalPeople = 0; //总载客量
System.out.println("=============================================");
System.out.println("请输入您要租车的数量:");
int num = in.nextInt();
if(num<=0){
System.out.println("您不买进来干嘛,我反手就是一巴掌");
System.exit(0);
}
int count = 1;
int j;
while(count<=num){
do{ //do-while循环判断输入是否正确。
System.out.println("请输入第"+(count)+"辆车的序号:"+"\t"+"(温馨提示有效序号为1到"+cars.length+")");
j=in.nextInt()-1;
}while(j>=cars.length||j<0);
//获取对应序号的车名,租金,容量
if(cars[j].getClass()==Car.class){
System.out.println("车名:"+cars[j].getName()+"\t"+"载客量:"+((Car)cars[j]).getPeople());
totalPrice = cars[j].getPrice()+totalPrice;
totalPeople = ((Car)cars[j]).getPeople()+totalPeople;
}else if(cars[j].getClass()==Van.class){
System.out.println("车名:"+cars[j].getName()+"\t"+"载货量:"+((Van)cars[j]).getGoods()+"T");
totalPrice = cars[j].getPrice()+totalPrice;
totalGoods = ((Van)cars[j]).getGoods()+totalGoods;
}else if(cars[j].getClass()==PickUp.class){
System.out.println("车名:"+cars[j].getName()+"\t"+"载客量:"+((PickUp)cars[j]).getPeople()+"载货量:"+((PickUp)cars[j]).getGoods()+"T");
totalPrice = cars[j].getPrice()+totalPrice;
totalPeople = ((PickUp)cars[j]).getPeople()+totalPeople;
totalGoods = ((PickUp)cars[j]).getGoods()+totalGoods;
}
count++;
}
System.out.println("=============================================");
System.out.println("租一天的总金额是:");
System.out.println(totalPrice);
System.out.println("=============================================");
System.out.println("您要租的天数:");
int day = in.nextInt();
System.out.println("=============================================");
System.out.println("您的账单是:");
System.out.println("载客数"+totalPeople+"人"+"\t"+"载货量:"+totalGoods+"T"+"\n"+"总金额:"+totalPrice*day+"元");
System.out.println("欢迎下次光临,亲(づ ̄3 ̄)づ╭~");
}
public static void main(String[] args) {
RentSystem rs = new RentSystem();
rs.rent();
System.exit(0);
}
}
点击查看更多内容
65人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦