栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

java入门第二季实战

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java入门第二季实战

```//父类
package com.buaa.car;

public class Car {
protected int id;
protected String name;
protected int price;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}   

@Override
public String toString() {
    // TODO Auto-generated method stub
    return "车名:"+name+",租金:"+price+"元";
}

}
子类
``package com.buaa.car;

import com.buaa.car.impl.Zaihuo;
import com.buaa.car.impl.Zaike;

public class PickUp extends Car implements Zaike,Zaihuo{

public PickUp() {
    name = "F150";
    price = 5000;
}

@Override
public double cargo() {
    int weight = 3;
    return weight;
}

@Override
public int passengers() {
    int num = 5;
    return num;
}
@Override
public String toString() {
    // TODO Auto-generated method stub
    return super.toString()+"载重:"+cargo()+"吨,载客:"+passengers()+"人";
}

}

package com.buaa.car;

import com.buaa.car.impl.Zaike;

public class Bus extends Car implements Zaike{
    public Bus(){

 name = "宇通";
 price = 6000;
    }
    @Override
    public int passengers() {
 int num = 45 ;
 return num;
    }
    @Override
    public String toString() {
 // TODO Auto-generated method stub
 return super.toString()+"载客量:"+passengers()+"人";
    }
}

```输入代码
package com.buaa.car;

import com.buaa.car.impl.Zaihuo;

public class Truck extends Car implements Zaihuo{

    public Truck(){

 name = "擎天柱";
 price = 8000;
    }

    @Override
    public double cargo() {
 int weight = 25;
 return weight;
    }
    @Override
    public String toString() {
 // TODO Auto-generated method stub
 return super.toString()+"载重:"+cargo()+"吨";
    }
}
业务逻辑
``package com.buaa.main;

import java.util.Scanner;

import com.buaa.car.Bus;
import com.buaa.car.Car;
import com.buaa.car.PickUp;
import com.buaa.car.Truck;

public class Main {
    static int totalPrice = 0;
    static int totalCargo = 0;
    static int totalPass = 0;
    static Scanner input1;
    static Scanner input2;
    static Car[] c;

    public static void main(String[] args) {

 // 欢迎页
 System.out.println("选择租车类型");
 c = new Car[] { new Bus(), new Truck(), new PickUp() };
 int count = 0;
 // 遍历所有的车
 for (Car List : c) {
     count++;
     System.out.println(count + ":" + List.toString());
 }
 System.out.print("请输入选择车型:    ");
 input1 = new Scanner(System.in);
 int type = input1.nextInt();
 if (type > 0 && type <= c.length) {
     choose(type);
 } else {
     System.out.println("请输入正确的编号!");
     main(args);
 }
    }

    public static void choose(int type) {

 System.out.print("请输入租赁数量:    ");
 input2 = new Scanner(System.in);
 int num = input2.nextInt();
 if (type == 1) {
     Bus b = new Bus();
     totalPrice = (int) b.getPrice() * num;
     totalPass = b.passengers() * num;
     System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载客" + totalPass + "人");
 }
 if (type == 2) {
     Truck t = new Truck();
     totalPrice = (int) t.getPrice() * num;
     totalCargo = (int) t.cargo() * num;
     System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载重" + totalCargo + "吨");
 }
 if (type == 3) {
     PickUp p = new PickUp();
     totalPrice = (int) p.getPrice() * num;
     totalCargo = (int) p.cargo() * num;
     totalPass = p.passengers() * num;
     System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载重" + totalCargo + "吨,可载客" + totalPass + "人");
 }

    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233824.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号