交作业!大家来交流交流,提提意见

来源:12-1 综合练习

qq_况_0

2018-09-21 20:45

测试类

import java.util.Scanner;

public class Play {

    public static void main(String[] args) {
        Car car1 = new Truck("货车1号", 100, 4);
        Car car2 = new Truck("货车2号", 200, 15);
        Car car3 = new MannedCar("客车1号", 300, 15);
        Car car4 = new MannedCar("客车2号", 400, 4);
        Car car5 = new PickupTruck("皮卡车1号", 400, 2,4);
        Car car6 = new PickupTruck("皮卡车2号", 500, 3,2);

        Car[] cars = new Car[6];
        cars[0] = car1;
        cars[1] = car2;
        cars[2] = car3;
        cars[3] = car4;
        cars[4] = car5;
        cars[5] = car6;

        System.out.println("欢迎使用嗒嗒租车系统:");
        System.out.println("您是否要租车:1是 0否");
        Scanner scanner = new Scanner ( System.in );
        int yes_no = scanner.nextInt ();
        if (yes_no == 1) {
            System.out.println ( "您可以租车的类型及价目表:" );
            System.out.println ( "序号 名称    租金     容量" );
            for (int i = 0; i < cars.length; i++) {
                System.out.print ( i + 1 + ".   " );
                cars[i].show ();
            }
            //eg1.foreeach遍历
            /*for (Car car : cars) {
                   car.show ();
            }*/

            //eg2.while遍历
            /*int i = 0;
            while(i<cars.length){
                cars[i].show();
                i++;
            }*/

            System.out.print ( "请输入您要租汽车的数量: " );
            int lease = scanner.nextInt ();
            //生成储存租借汽车的数组
            Car[] zcar = new Car[lease];//zcar数组长度取决于用户输入的lease变量值

            for (int n = 1; n < lease + 1; n++) {
                System.out.print ( "请输入第" + n + "车的序列号: " );
                int serial = scanner.nextInt ();
                zcar[n-1] = cars[serial-1];//把cars数组中[serial-1]传入zcar数组中[n-1]去
            }
            //测试zcar(租借车辆数组)
            /*for (int i = 0; i<lease; i++){
                zcar[i].show ();
            }*/
            //day变量存储输入天数
            System.out.print ( "请输入租车的天数: " );
            int day = scanner.nextInt ();

            //遍历筛选载人汽车
            System.out.println ("***可载人的汽车有: ");
            for (int i = 0; i<lease; i++){
                if (zcar[i].manned != 0){
                    System.out.print (zcar[i].name + " ");
                }
            }
            //遍历筛选载货汽车
            System.out.println ();
            System.out.println ("***可载货的汽车有: ");
            for (int i = 0; i<lease; i++){
                if (zcar[i].goods != 0){
                    System.out.print (zcar[i].name + " ");
                }
            }
            //计算总价格
            System.out.println ();
            System.out.println ("***租车总价格为: ");
            int moneny = 0;
            for (int i = 0; i<lease;i++){
                moneny += zcar[i].price * day;
            }
            System.out.println (moneny + "元");


        }else {
            System.out.println ("好的,再见");

            System.exit ( 0 );
        }
    }
}

Car类(父类)

public class Car {
    public String name;
    public int price;
    public int goods;
    public int manned;

    public void show(){
    }
}

Truck类(子类)

public class Truck extends Car{
    //private int goods;

    public Truck(String newName,int newPrice,int newgoods){
        name = newName;
        price = newPrice;
        goods = newgoods;
    }

    public void show(){
        System.out.println ( name + " " + price + "元/天 " +"载货:"  + goods + "吨" );
    }

    //没用的
    /*public int getGoods() {
        return goods;
    }
    public void setGoods(int goods) {
        this.goods = goods;
    }*/
}

MannedCar类(子类)

public class MannedCar extends Car{
    //private int manned;

    public MannedCar(String newName,int newPrice,int newManned){
        name = newName;
        price = newPrice;
        manned = newManned;
    }
    public void show(){
        System.out.println ( name + " " + price + "元/天 " +"载人:"  + manned + "人" );
    }

    public int getManned() {
        return manned;
    }
    public void setManned(int manned) {
        this.manned = manned;
    }
}

PickupTruck类(子类)

public class PickupTruck extends Car{
    //private int goods;
    //private int manned;

    public PickupTruck(String newName,int newPrice,int newGoods,int newManned){
        name = newName;
        price = newPrice;
        goods = newGoods;
        manned = newManned;
    }
    public void show(){
        System.out.println ( name + " " + price + "元/天 " +"载人:"  + manned + "人" + " 载货:" + goods + "吨");
    }

    //没用的
    /*public int getGoods() {
        return goods;
    }
    public int getManned() {
        return manned;
    }
    public void setGoods(int goods) {
        this.goods = goods;
    }
    public void setManned(int manned) {
        this.manned = manned;
    }*/
}

这个abstract类到底怎么用,改来改去都不行,还有其他的地方有没有什么错误哈,互相交流交流

写回答 关注

2回答

  • 慕码人7135883
    2018-10-27 17:45:24

    注意要体现程序的持续性,程序需要一直运行,不是说一个客人交易完后就结束了,还要让程序继续下去,这时候需要在main里面使用while(){},把代码都写进while()里。

  • esperanze203
    2018-09-24 09:58:51

    我的代码 用到了abstract类和接口 供你参考

    public interface IZaiRen {

       void zaiRen();

    }

    public interface IZaiHuo {
       void zaiHuo();

    }

    public interface IDouZai extends IZaiHuo, IZaiRen {

    }

    public abstract class Car {
        public int number;
        public String carName;
        public int price;
        public String carType ;
        
        public abstract void print();

    }

    public class PersonCar extends Car implements IZaiRen {
       public int capacityPerson;
     
       
       public PersonCar(int number,String carName,int price,int capacityPerson) {
           this.number=number;
           this.carName=carName;
           this.price=price;
           this.capacityPerson=capacityPerson;
           carType = "PersonCar";
       }
       
       public void zaiRen() {
          System.out.print("  载人:"+capacityPerson+"人 ");
       }
       
       public void print() {
           System.out.print(number+".  "+carName+"  "+price+"元/天 ");
           this.zaiRen();
       }

    }

    public class StuffCar extends Car implements IZaiHuo {
       public int capacityStuff;
       
       public StuffCar(int number,String carName,int price,int capacityStuff) {
           this.number=number;
           this.carName=carName;
           this.price=price;
           this.capacityStuff=capacityStuff;
           carType = "StuffCar";
       }
       
       public void zaiHuo() {
           System.out.print("  载货:"+capacityStuff+"吨 ");
       }
       
       public void print() {
           System.out.print(number+".  "+carName+"  "+price+"元/天 ");
           this.zaiHuo();;
       }

    }

    public class PersonStuffCar extends Car implements IDouZai {
       public int capacityPerson;
       public int capacityStuff;
       
       public PersonStuffCar(int number,String carName,int price,int capacityPerson,int capacityStuff) {
           this.number=number;
           this.carName=carName;
           this.price=price;
           this.capacityPerson=capacityPerson;
           this.capacityStuff=capacityStuff;
           carType = "PersonStuffCar";

       }
       
       public void zaiRen() {
           System.out.print("  载人:"+capacityPerson+"人 ");
       }
       
       public void zaiHuo() {
           System.out.print("  载货:"+capacityStuff+"吨 ");
       }
       
       public void print() {
           System.out.print(number+".  "+carName+"  "+price+"元/天 ");
           this.zaiRen();
           this.zaiHuo();
       }

    }

    import java.util.Scanner;

    public class Test {
        public static void main(String[] args) {
            System.out.println("欢迎使用答答租车系统:");
            System.out.println("您是否要租车:1是 0否");
            Scanner scanner = new Scanner(System.in);
            int sellect = scanner.nextInt();
            if (sellect==0)
                return;
            
            int totalPersonC = 0;
            int totalStuffC = 0;
            int totalPrice =  0;
            int pCarNum=0;
            int sCarNum=0;
            
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号  汽车名称      租金          容量");
            Car cars[] =new Car[6];
            String pcars[]=new String[6];
            String scars[]=new String[6];
            
            cars[0]=new PersonCar(1,"奥迪A4",500,4);
            cars[1]=new PersonCar(2,"马自达6",400,4);
            cars[2]=new PersonStuffCar(3,"皮卡雪    ",450,4,2);
            cars[3]=new PersonCar(4,"金龙     ",800,20);
            cars[4]=new StuffCar(5,"松花江   ",400,4);
            cars[5]=new StuffCar(6,"依维柯   ",1000,20);
            for(Car a : cars) {
                a.print();
                System.out.println("");
                //System.out.print(a instanceof PersonCar);
                //System.out.print(a instanceof StuffCar);
                //System.out.print(a instanceof PersonStuffCar);
            }
            System.out.println("请输入您要租汽车的数量");
            int num=scanner.nextInt();
            for (int i=1;i<=num;i++) {
                System.out.println("请输入第"+i+"量车的序号");
                int n = scanner.nextInt();
                totalPrice+=cars[n-1].price;
                
                switch (cars[n-1].carType) {
                case "PersonCar":pCarNum+=1;
                    pcars[pCarNum]=cars[n-1].carName;
                    totalPersonC+=((PersonCar)cars[n-1]).capacityPerson;
                    break;
                case "StuffCar":sCarNum+=1;
                    scars[sCarNum]=cars[n-1].carName;
                    totalStuffC+=((StuffCar)cars[n-1]).capacityStuff;
                    break;
                case "PersonStuffCar":
                    pCarNum+=1;
                    pcars[pCarNum]=cars[n-1].carName;
                    totalPersonC+=((PersonStuffCar)cars[n-1]).capacityPerson;
                    sCarNum+=1;
                    scars[sCarNum]=cars[n-1].carName;
                    totalStuffC+=((PersonStuffCar)cars[n-1]).capacityStuff;
                }
            }
            System.out.println("请输入租车天数:");
            totalPrice*=scanner.nextInt();
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            for (String s:pcars) {
                if(s!=null)
                    System.out.print(s+"  ");
            }
            System.out.println("共载人:"+totalPersonC+"人");
            
            System.out.println("***可载货的车有:");
            for (String s:scars) {
                if(s!=null)
                    System.out.print(s+"  ");
            }
            System.out.println("共载货:"+totalStuffC+"吨");
            
            System.out.println("***租车总价格:"+totalPrice+"元");
            
        }
    }

    慕瓜4533...

    可以指导指导我吗????

    2018-10-08 22:32:22

    共 1 条回复 >

Java入门第二季 升级版

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

530105 学习 · 6086 问题

查看课程

相似问题