这是我用两种方法写的系统(方法一)

来源:12-1 综合练习

Trafalgar Law

2015-02-09 15:58

package com.DADAzuche;
import java.util.ArrayList;
import java.util.Scanner;
 public class DADArentcar
{
    public static void main(String[] args)
    {
        Car[] car ={new Normalcar(1,"奥迪A4",500,4),new Normalcar(2,"马自达6",400,4)
        ,new Pickup(3,"皮卡",450,4,2),new Normalcar(4,"苏州金龙",800,50)
        ,new Lorry(5,"跃进",400,4),new Lorry(6,"依维柯",999,20)};
         
        System.out.println("\t****欢迎您使用嗒嗒租车系统****");
         
        System.out.println();
         
        System.out.println("1.进入系统,2.退出系统");
         
        Scanner in = new Scanner(System.in);
         
        int str = in.nextInt();
         
        if(str==1)
        {
            System.out.println("\t****欢迎您进入嗒嗒租车系统****");
            System.out.println();
            System.out.println("租车信息列表如下:");
            System.out.println("序号\t\t汽车名称\t\t租金\t\t容量:");
            for(int i = 0;i<car.length;i++)
            {
                car[i].show();
            }
             
            System.out.println();
             
            System.out.println("请输入你要租的汽车数量:");
         
            int number = in.nextInt();
             
            System.out.println();
             
            System.out.println("请输入你要租车的序号:");
             
            System.out.println();
             
            ArrayList<Car> al = new ArrayList<Car>();
 
            for(int i = 0 ;i<number;i++)
            {
                System.out.println("请输入第"+(i+1)+"辆车的序号");
                int num = in.nextInt();
                for(int x=0;x<car.length;x++)
                {
                    if(num==car[x].getID())
                    al.add(car[x]);
                }
            }
     
            System.out.println("请输入您租车的天数:");
             
            int Rentday = in.nextInt();
             
            System.out.println();
             
            System.out.println("租车清单如下,请认真核对:");
             
            System.out.println();
             
            System.out.println("可载人的车:");
             
            int Allprice = 0;
             
            int ALLBusload = 0;
             
            int ALLBoatload = 0;
             
            for(int i=0;i<al.size();i++)
            {
                if(al.get(i)instanceof Normalcar)
                {
                    System.out.print(al.get(i).getCarname()+" ");
                    Allprice+=al.get(i).getPrice();
                    ALLBusload+=((Normalcar) al.get(i)).getBusload();
                }
                else if(al.get(i)instanceof Pickup)
                {
                    System.out.print(al.get(i).getCarname()+" ");
                    Allprice+=al.get(i).getPrice();
                    ALLBusload+=((Pickup) al.get(i)).getBusload();
                }
            }
             
            System.out.println("   总可载人数:"+ALLBusload+"人");
             
            System.out.println();
             
            System.out.println();
             
            System.out.println("可载货的车:");
                     
            for(int i=0;i<al.size();i++)
            {
                if(al.get(i)instanceof Pickup)
                {
                    System.out.print(al.get(i).getCarname()+" ");
                    ALLBoatload+= ((Pickup) al.get(i)).getBoatload();
                }
                else if(al.get(i)instanceof Lorry)
                {
                    System.out.print(al.get(i).getCarname()+" ");
                    ALLBoatload+= ((Lorry) al.get(i)).getBoatload();
                    Allprice+=al.get(i).getPrice();
                }
            }
            System.out.println("   总可载货物:"+ALLBoatload+"吨");
            System.out.println();
             
            System.out.println("一共需付的金额为:"+Allprice*Rentday);
        }
        else if(str==0)
        {
            System.exit(5);
        }
        in.close();
    }
}
 
 
package com.DADAzuche;
 
public abstract class Car
{
    protected int ID;
    protected String Carname;
    protected int Price;
    public Car(int iD, String carname, int price) {
        super();
        this.ID = iD;
        this.Carname = carname;
        this.Price = price;
    }
     
    public int getID() {
        return ID;
    }
 
    public void setID(int iD) {
        ID = iD;
    }
 
    public String getCarname() {
        return Carname;
    }
 
    public void setCarname(String carname) {
        Carname = carname;
    }
 
    public int getPrice() {
        return Price;
    }
 
    public void setPrice(int price) {
        Price = price;
    }
 
    public abstract void show();
     
}
 
 
package com.DADAzuche;
 
public class Normalcar extends Car 
{
    private int Busload;
     
 
    public Normalcar(int iD, String carname, int price, int busload) {
        super(iD, carname, price);
        Busload = busload;
    }
     
 
 
    public int getBusload() {
        return Busload;
    }
 
 
 
    public void setBusload(int busload) {
        Busload = busload;
    }
 
 
 
    @Override
    public void show() 
    {
        System.out.println(ID+"\t\t"+Carname+"\t\t"+Price+"元/天"+"\t\t"+Busload+"人");
    }
}
 
 
package com.DADAzuche;
 
public class Lorry extends Car
{
    private int Boatload;
    public Lorry(int iD, String carname, int price,int Boatload) {
        super(iD, carname, price);
        this.Boatload = Boatload;
    }
     
    public int getBoatload() {
        return Boatload;
    }
 
    public void setBoatload(int boatload) {
        Boatload = boatload;
    }
 
    @Override
    public void show() {
        System.out.println(ID+"\t\t"+Carname+"\t\t"+Price+"元/天"+"\t\t"+Boatload+"吨");
    } 
}
 
 
 
package com.DADAzuche;
 
public class Pickup extends Car
{
    private int Busload;
    private int Boatload;
     
    public Pickup(int iD, String carname, int price, int busload, int boatload) {
        super(iD, carname, price);
        this.Busload = busload;
        this.Boatload = boatload;
    }
 
     
    public int getBusload() {
        return Busload;
    }
 
 
    public void setBusload(int busload) {
        Busload = busload;
    }
 
 
    public int getBoatload() {
        return Boatload;
    }
 
 
    public void setBoatload(int boatload) {
        Boatload = boatload;
    }
 
 
    @Override
    public void show()
    {
        System.out.println(ID+"\t\t"+Carname+"\t\t"+Price+"元/天"+"\t\t"+Busload+"人"+","+Boatload+"吨");
    }
}


写回答 关注

1回答

  • 慕前端5914160
    2015-12-24 12:52:01

    good

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530646 学习 · 6091 问题

查看课程

相似问题