手记

答答租车系统代码操作

接口

package car;

public abstract interface Show {
   public abstract void show();
}

抽象类

package car;

public abstract class Cars implements Show {
      private double money;//租金     
      private String name;//汽车名
      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;
    }
    @Override
    public void show() {
        // TODO Auto-generated method stub

    }
}

三种车的类型:
第一种Bus

package car;

public class Bus extends Cars implements Show {

    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() {
        // TODO Auto-generated method stub
        System.out.println(getNumber()+"\t"+getName()+"\t"+getMoney()+"元/天\t"+"载人:"+getPerson());
    }       
}

Larry

package car;

public class Larry extends Cars implements Show {

    public Larry(int number,String name,double money,double weight){
        this.setMoney(money);
        this.setName(name);
        this.setNumber(number);
        this.setWeight(weight);
    }

    public void show(){
        System.out.println(getNumber()+"\t"+getName()+"\t"+getMoney()+"元/天\t"+"载货:"+getWeight()+"吨");
    }
}

Picars

package car;

public class Picars extends Cars implements Show {

    public Picars(int number,String name,double money,int person,double weight){
        this.setMoney(money);
        this.setName(name);
        this.setNumber(number);
        this.setPerson(person);
        this.setWeight(weight);
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub
        System.out.println(getNumber()+"\t"+getName()+"\t"+getMoney()+"元/天\t"+"载人:"+getPerson()+"\t"+"载货:"+getWeight()+"吨");
    }

}

主函数

package car;

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",400,4,2),
                 new Bus(4,"金龙",800,20),
                 new Larry(5,"松花江",400,4),
                 new Larry(6,"依维柯",1000,20),

         };
         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容量\t");
             for(int i = 0; i <= 5; i++)
                 Cars[i].show();
             System.out.println("请输入您要租几辆车?");
             //Scanner nn= new Scanner(System.in);
             int a1 = in.nextInt();
             int[] carn = new int[a1];
             for(int i=0; i<a1; 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++){
                 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("租车总价格:" + sumy);
         }
         in.close();
    }

}
3人推荐
随时随地看视频
慕课网APP

热门评论

carn[i]-1

请问数组下标-1到底是啥意思,我现在还没搞懂

查看全部评论