主程序
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car type[]={new Bus("奥迪A4", 500, 4),new Bus("马自达", 400, 4),
new Bus("金龙", 800, 20), new PickUp("皮卡雪", 450, 4, 2),
new Trunk("松花江", 400, 4),new Trunk("依维柯", 1000, 20)};
System.out.println("欢迎进入租车系统");
System.out.println("租车请按1,退出请按0");
//@SuppressWarnings("resource")
int select=new Scanner(System.in).nextInt();
if(select==1){
System.out.println("下面是可租车的信息");
System.out.println("*****************************************");
System.out.println("序号\t车名\t每日租金\t容量");
for(int i=0;i<type.length;i++){
System.out.println((i+1)+"\t"+type[i]);
}
System.out.println("*****************************************");
System.out.println("您要租几种车?");
int kinds=new Scanner(System.in).nextInt();
if(kinds>6){
System.out.println("本系统只有6类车可供选择,请重新输入6或6以下的数字");
int kinds2=new Scanner(System.in).nextInt();
kinds=kinds2;
}
System.out.println("您需要租多少天?");
int days=new Scanner(System.in).nextInt();
System.out.println("请输入要租车的序号");
int xuhao[]=new int[kinds];
int money=0,numPer=0,numGoo=0;
for(int a=0;a<xuhao.length;a++){
xuhao[a]=new Scanner(System.in).nextInt();
if(xuhao[a]==1){
money=money+500*days;
numPer=numPer+4;
numGoo=numGoo+0;
}else if(xuhao[a]==2){
money=money+400*days;
numPer=numPer+4;
numGoo=numGoo+0;
}else if(xuhao[a]==3){
money=money+800*days;
numPer=numPer+20;
numGoo=numGoo+0;
}else if(xuhao[a]==4){
money=money+450*days;
numPer=numPer+4;
numGoo=numGoo+2;
}else if(xuhao[a]==5){
money=money+400*days;
numPer=numPer+0;
numGoo=numGoo+4;
}else if(xuhao[a]==6){
money=money+1000*days;
numPer=numPer+0;
numGoo=numGoo+20;
}else{
System.out.println("您输入错误,请重新输入");
xuhao[a]=new Scanner(System.in).nextInt();
}
}
System.out.println("-----------------------------------------------------------------------------");
System.out.println("您的订单如下:");
System.out.println("车名\t每日租金\t容量\t\t总容量\t\t\t总金额");
for(int b=0;b<kinds;b++){
if(b==(kinds-1)){
System.out.println(type[xuhao[b]-1]+"\t\t"+"总载人:"+numPer+"人 "+"总载物:"+numGoo+"吨"+"\t"+money+"元");
break;
}
System.out.println(type[xuhao[b]-1]);
}
System.out.println("-----------------------------------------------------------------------------");
System.out.println("感谢您的使用,期待您的下次光临!!!");
}else if(select==0){
System.out.println("期待您的下次使用");
System.exit(0);
}else{
System.out.println("您的输入有误,请仔细查看再次输入");
System.out.println();
System.out.println();
main(args);
}
}
}
抽象父类
public abstract class Car {
private String name;
private int price;
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;
}
@Override
public String toString() {
return name+"\t"+price+"\t";
}
}
两接口
public interface IGoods {
public int getGoods();
public void setGoods(int goods);
}
public interface IPerson {
public int getPerson();
public void setPerson(int person);
}
各子类
public class Bus extends Car implements IPerson{
private int person;
@Override
public int getPerson() {
// TODO Auto-generated method stub
return person;
}
@Override
public void setPerson(int person) {
// TODO Auto-generated method stub
this.person=person;
}
public Bus(String name,int price,int person){
this.setName(name);
this.setPrice(price);
this.setPerson(person);
}
@Override
public String toString() {
return super.toString()+"载人:"+this.getPerson()+"人";
}
}
public class PickUp extends Car implements IPerson,IGoods{
private int person;
private int goods;
@Override
public int getGoods() {
// TODO Auto-generated method stub
return goods;
}
@Override
public void setGoods(int goods) {
// TODO Auto-generated method stub
this.goods=goods;
}
@Override
public int getPerson() {
// TODO Auto-generated method stub
return person;
}
@Override
public void setPerson(int person) {
// TODO Auto-generated method stub
this.person=person;
}
public PickUp(String name,int price,int person,int goods){
this.setName(name);
this.setPrice(price);
this.setPerson(person);
this.setGoods(goods);
}
@Override
public String toString() {
return super.toString()+"载人:"+this.getPerson()+"人"+" "+"载物:"+this.getGoods()+"吨";
}
}
public class Trunk extends Car implements IGoods{
private int goods;
@Override
public int getGoods() {
// TODO Auto-generated method stub
return goods;
}
@Override
public void setGoods(int goods) {
// TODO Auto-generated method stub
this.goods=goods;
}
public Trunk(String name,int price,int goods){
this.setName(name);
this.setPrice(price);
this.setGoods(goods);
}
@Override
public String toString() {
return super.toString()+"载物:"+this.getGoods()+"吨";
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章