为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

//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吨");
}

}

点击查看更多内容
6人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消