手记

Java入门第二季—嗒嗒租车系统 简单版

首先创建了抽象类Che

package com.zuche;
public abstract class Che {
      String name;
      int xuhao;
      float price;
      int zairen;
      int zaihuo;
      public abstract void chenhuo();
      public abstract void chenren();
}

其次创建了三个子类继承
(我的子类继承的函数没怎么用,只作了信息输出)
(1)Bus

package com.zuche;
public  class Bus extends Che {
    @Override 
    public void chenhuo() {
        // TODO Auto-generated method stub
        System.out.println(xuhao+"\t"+name+"\t"+price+"元/天\t"+zaihuo+"吨");
    }
    @Override
    public void chenren() {
        // TODO Auto-generated method stub  
    }
}

(2)Car

package com.zuche;
public  class Car extends Che {
    @Override 
    public void chenren() {
        // TODO Auto-generated method stub
        System.out.println(xuhao+"\t"+name+"\t"+price+"元/天\t"+zairen+"人");
    }
    @Override
    public void chenhuo() {
        // TODO Auto-generated method stub  
    }
}

(3)PiCa

package com.zuche;
public class PiCa extends Che {
    @Override 
    public void chenhuo() {
        // TODO Auto-generated method stub

    }
    @Override 
    public void chenren() {
        // TODO Auto-generated method stub
        System.out.println(xuhao+"\t"+name+"\t"+price+"元/天\t"+zairen+"人\t"+zaihuo+"吨");
    }
}

最后正文:

package com.zuche;
import java.util.Scanner;
public class Dada  { 

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Che a1=new Car();a1.xuhao=1;a1.name="奥迪A4";a1.price=500;a1.zairen=4;
         Che a2=new Car();a2.xuhao=2;a2.name="马自达6";a2.price=400;a2.zairen=4;
         Che a3=new PiCa();a3.xuhao=3;a3.name="皮卡雪";a3.price=500;a3.zairen=4;a3.zaihuo=2;
         Che a4=new Car();a4.xuhao=4;a4.name="金龙";a4.price=800;a4.zairen=20;
         Che a5=new Bus();a5.xuhao=5;a5.name="松花江";a5.price=400;a5.zaihuo=4;
         Che a6=new Bus();a6.xuhao=6;a6.name="依维柯";a6.price=1000;a6.zaihuo=20;
        // a1.chenren();
         System.out.println("欢迎使用答答租车系统"); 
         System.out.println("您是否要租车:1是 0否"); 
         Scanner scanner = new Scanner(System.in); 
         int flag = scanner.nextInt(); 
         if (flag == 1) { System.out.println("您可租车的类型及其价目表:"); 
         System.out.println("序号"+"\t"+"汽车名称 "+"\t"+"租金"+"\t"+"\t容量");
         a1.chenren();
         a2.chenren();
         a3.chenren();
         a4.chenren();
         a5.chenhuo();
         a6.chenhuo();
         System.out.println("输入租车数量:");
         Scanner scanner2 = new Scanner(System.in); 
          flag = scanner.nextInt();
          int[]a=new int[flag];   //定义一个数组存储租车序号
         for(int i=1;i<=flag;i++){
             System.out.println("第"+i+"的序号为");
             a[i-1]=scanner.nextInt();  //将租车序号存入数组
         }    
         System.out.println("租车天数为");  
         int b=scanner.nextInt();      //b为天数
         System.out.println(b+"天");
         System.out.println("您的账单:"); 

         int x=0;//all zairen
         int y=0;//all zaihuo
         int z=0;//all price
         int ZR1=0;//确定数组的个数
         int ZH1=0;//确定数组的个数
         for(int i=1;i<=flag;i++){
             switch(a[i-1]){ 
            case 1:ZR1+=1;x+=4;z+=500;break; 
            case 2:ZR1+=1;x+=4;z+=400;break; 
            case 3:ZR1+=1;ZH1+=1;x+=4;y+=2;z+=450;break;
            case 4:ZR1+=1;x+=20;z+=800;break;
            case 5:ZH1+=1;y+=4;z+=400;break;
            case 6:ZH1+=1;y+=20;z+=1000;break;
            }
            }

         String []ZR=new String[ZR1];//all zairen ming
         String []ZH=new String[ZH1];//all zaihuo ming
    /*   int q=0; int p=0; for(int i=1;i<=flag;i++){

            if (a[i-1]==1) {q++; ZR[q-1]="奥迪A4,";}
            if (a[i-1]==2) {q++; ZR[q-1]="马自达6,";}
            if (a[i-1]==3) {q++; ZR[q-1]="皮卡雪6,";}
            if (a[i-1]==4) {q++; ZR[q-1]="金龙,"; }               

            if (a[i-1]==3) {p++; ZH[p-1]="皮卡雪6,";}
            if (a[i-1]==5) {p++; ZH[p-1]="松花江,";}
            if (a[i-1]==6) {p++; ZH[p-1]="依维柯,";}               
          }*/ //可以替换下面的switch
       int q=0;int p=0;  for(int i=1;i<=flag;i++){
         switch(a[i-1]){   
        case 1:q++;ZR[q-1]="奥迪A4,"; break;
        case 2:q++;ZR[q-1]="马自达6,";break;
        case 3:q++;ZR[q-1]="皮卡雪6,";p++;ZH[p-1]="皮卡雪6,";break;
        case 4:q++;ZR[q-1]="金龙,";break;
        case 5:p++;ZH[p-1]=("松花江,");break;
        case 6:p++;ZH[p-1]=("依维柯,");break;
        default:break;
        };}//zairen,zaiwu 数组填充

         for(int i=1;i<=ZR1;i++){System.out.println("***可载人的车有:"+ZR[i-1]);}
         System.out.println(" 共载人:"+x+"人"); 
         for(int i=1;i<=ZH1;i++){System.out.println("***可载货的车有:"+ZH[i-1]);}
         System.out.println(" 共载货:"+y+"吨"); 
         System.out.println("租车总价格:"+z*b+"元"); 
         System.out.print("感谢使用!"); }
         else{System.out.println("感谢使用!欢迎再来使用!"); }

         }

    }

测试结果如下:

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