虽然已经有了那么多的答答租车系统的文章,但还是决定写一写,算是对自己的总结吧~ 强行利用了抽象类和接口(并不确定用的好不好~),求指点。
首先,一个抽象类Cars:
public abstract class Cars implements ShowCar{
private String name;//汽车名
private double money;//租金
private int person;//载客量
private double weight;//载货量
private int number;//序号
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getPerson() {
return person;
}
public void setPerson(int person) {
this.person = person;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
为了熟悉接口而强行使用的接口类ShowCar
package com.imooc;
public interface ShowCar {
public void show();
}
然后3个不同的汽车类型:
public class Bus extends Cars implements ShowCar{
//创建载人汽车列表
public Bus(int number,String name,double money,int person){
this.setMoney(money);
this.setName(name);
this.setNumber(number);
this.setPerson(person);
}
public void show(){
System.out.println(getNumber()+"\t"+getName()+"\t"+getMoney()+"\t"+"载人:"+getPerson());
}
}
public class Larry extends Cars implements ShowCar{
public Larry(int number,String name,double money,double weight){
this.setName(name);
this.setMoney(money);
this.setWeight(weight);
this.setNumber(number);
}
public void show(){
System.out.println(getNumber()+"\t"+getName()+"\t"+getMoney()+"\t"+"载货:"+getWeight()+"吨");
}
}
public class Picars extends Cars implements ShowCar{
public Picars(int number,String name,double money,int person,double weight){
this.setNumber(number);
this.setName(name);
this.setMoney(money);
this.setPerson(person);
this.setWeight(weight);
}
public void show(){
System.out.println(getNumber()+"\t"+getName()+"\t"+getMoney()+"\t"+"载人:"+getPerson()+"载货:"+getWeight()+"吨");
}
}
最后测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义车
Cars[] cars={
new Bus(1,"奥迪A4",500,4),
new Bus(2,"马自达6",400,4),
new Picars(3,"皮卡雪6",450,4,2),
new Bus(4,"金龙\t",800,20),
new Larry(5,"松花江",400,4),
new Larry(6,"依维柯",1000,20),
};
//程序开始
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
if(a==1){
System.out.println("您可租车的类型和价格表:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
for(int i=0;i<cars.length;i++){
cars[i].show();
}
System.out.println("请输入您要租几辆车?");
int n=in.nextInt();
int[] carn=new int[n];
for(int i=0;i<n;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号:");
carn[i]=in.nextInt();
}
System.out.println("请输入租车天数:");
int day=in.nextInt();
int sump=0; //计算人数
int sumw=0; //计算货物重量
int sumy=0;//计算租金
//计算账单
System.out.println("***可载人的车有:");
for(int i=0;i<carn.length;i++){
if(cars[carn[i]-1].getPerson()!=0)
System.out.print(cars[carn[i]-1].getName()+"\t");
sump+=cars[carn[i]-1].getPerson();
}
System.out.println("共载人:"+sump);
System.out.println("***在货的车有:");
for(int i=0;i<carn.length;i++){
//没有为bus的getWeight赋值,默认为0
if(cars[carn[i]-1].getWeight()!=0)
System.out.print(cars[carn[i]-1].getName()+"\t");
sumw+=cars[carn[i]-1].getWeight();
}
System.out.println("共载货:"+sumw+"吨");
for(int i=0;i<carn.length;i++){
sumy+=cars[carn[i]-1].getMoney();
}
System.out.println("租车总价格:"+day*sumy+"元");
}
in.close();
}
}
执行结果:
点击查看更多内容
27人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦