继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

这三天看完Java入门第一季和第二季的成果!纪念一下!感谢Java入门的作者!

慕无忌8775282
关注TA
已关注
手记 1
粉丝 0
获赞 9

//main入口
package RentCar;
import java.util.Scanner;
public class Dada {

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    //各类汽车的属性的定义

        Car audi = new Audi();
        Car mazda = new Mazda();
        Car pickup = new Pickup();
        Car kingLong = new KingLong();
        Car songHuaJiang = new SongHuaJiang();
        Car naveco = new Naveco();

        audi.num = 1;
        audi.name = "奥迪A4";
        audi.price = 500 ;
        audi.amount = 20 ;

        mazda.num = 2;
        mazda.name ="马自达6";
        mazda.price = 400;
        mazda.amount = 20;

        pickup.num = 3;
        pickup.name ="皮卡雪6";
        pickup.price = 450;
        pickup.amount = 20;

        kingLong.num = 4;
        kingLong.name ="金龙";
        kingLong.price = 800;
        kingLong.amount = 20;

        songHuaJiang.num = 5;
        songHuaJiang.name ="松花江";
        songHuaJiang.price = 400;
        songHuaJiang.amount = 20;

        naveco.num = 6;
        naveco.name ="依维柯";
        naveco.price = 1000;
        naveco.amount = 20;

    int i,price=0,num=0,amount=0,amountRemain=0;

    Scanner input = new Scanner(System.in);
    do{
        System.out.println("请问您是否要租车:1.是    2.否");
        i = input.nextInt();
        if(i == 1)
        {
            System.out.println("序号  汽车名称    价格  数量  容量");
            System.out.print(audi.num+".    "+audi.name+"   "+audi.price+"元/天   "+audi.amount+" ");
            audi.volume();
            System.out.print(mazda.num+".   "+mazda.name+"  "+mazda.price+"元/天  "+mazda.amount+"    ");
            mazda.volume();
            System.out.print(pickup.num+".  "+pickup.name+" "+pickup.price+"元/天 "+pickup.amount+"   ");
            pickup.volume();
            System.out.print(kingLong.num+".    "+kingLong.name+"   "+kingLong.price+"元/天   "+kingLong.amount+" ");
            kingLong.volume();
            System.out.print(songHuaJiang.num+".    "+songHuaJiang.name+"   "+songHuaJiang.price+"元/天   "+songHuaJiang.amount+" ");
            songHuaJiang.volume();
            System.out.print(naveco.num+".  "+naveco.name+" "+naveco.price+"元/天"+naveco.amount+"    ");
            naveco.volume();
            do{
                System.out.print("请输入您要租车的序号:");
                num = input.nextInt();
                if(num<1 || num>6)
                    System.out.println("您输入的信息非法,请重新输入!");
            }while(num<1 || num>6);
            switch(num){
            case 1 :
                amountRemain = audi.amount;
                price = audi.price;
                break;
            case 2 :
                amountRemain = mazda.amount;
                price = mazda.price;
                break;
            case 3 :
                amountRemain = pickup.amount;
                price = pickup.price;
                break;
            case 4 :
                amountRemain = kingLong.amount;
                price = kingLong.price;
                break;
            case 5 :
                amountRemain = songHuaJiang.amount;
                price = songHuaJiang.price;
                break;
            case 6 :
                amountRemain = naveco.amount;
                price = naveco.price;
                break;
            }
            do{
                System.out.print("请输入您要租车的数量:");
                amount = input.nextInt();
                if(amount < 0 || amount > amountRemain)
                    System.out.println("您输入的信息非法,请重新输入!");
            }while(amount < 0 || amount > amountRemain);

            System.out.println("此次您需要支付"+(price*amount)+"元/天");
            switch(num){
            case 1 :
                audi.amount -= amount;
                break;
            case 2 :
                mazda.amount -= amount;
                break;
            case 3 :
                pickup.amount -= amount;
                break;
            case 4 :
                kingLong.amount -= amount;
                break;
            case 5 :
                songHuaJiang.amount -= amount;
                break;
            case 6 :
                naveco.amount -= amount;
                break;
            }
            price=0;
            num=0;
            amount=0;
            amountRemain=0;
        }
        else{
            System.out.println("感谢您使用嗒嗒租车系统");
            break;
        }
    }while(true);

}

}

//父类Car
package RentCar;

public abstract class Car {
public int num;
public String name;
public int price;
public int amount;
public abstract void volume();
}

//子类Mazda
package RentCar;

public class Mazda extends Car {

public void volume() {
    // TODO 自动生成的方法存根
    System.out.println("载人:4人");
}

}

//子类Pickup
package RentCar;

public class Pickup extends Car {

public void volume() {
    // TODO 自动生成的方法存根
    System.out.println("载人:4人,载货:2吨");
}

}

//子类KingLong
package RentCar;

public class KingLong extends Car {

public void volume() {
    // TODO 自动生成的方法存根
    System.out.println("载人:20人");
}

}
//子类Audi
package RentCar;

public class Audi extends Car {

public void volume() {
    // TODO 自动生成的方法存根
    System.out.println("载人:4人");
}

}

//子类SongHuaJiang
package RentCar;

public class SongHuaJiang extends Car {

public void volume() {
    // TODO 自动生成的方法存根
    System.out.println("载货:4吨");
}

}

//子类Naveco
package RentCar;

public class Naveco extends Car {

public void volume() {
    // TODO 自动生成的方法存根
    System.out.println("载货:20吨");
}

}

打开App,阅读手记
6人推荐
发表评论
随时随地看视频慕课网APP

热门评论

父类这么写不是不用搞那么多System.out.println吗

public class Car {

 int      count;

 String   name;

 int      price;

 String     lex ;

      public Car(int newcount,String newname,int newprice,String newlex){

    count=newcount;

    name=newname;

    price=newprice;

    lex=newlex;

    System.out.println(count+"\t"+name+"\t"+price+"/天\t"+lex);

         }

   }

你不觉得你写的好冗杂,太麻烦了吗


查看全部评论