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

初学java, update答答租车小程序。

不要记得我
关注TA
已关注
手记 3
粉丝 6
获赞 7
    今天学了老师教的JAVA集合的使用,将前两天做的答答租车程序进行了修改。里面使用了集合方法来保存、遍历元素。完善了部分功能和代码。在本地机上使用通过。很感谢老师们的学习指导。因为是初学JAVA,代码还有重复的。希望能在以后的学习中,再改进。再次感谢 IMOOC的老师 !
package cn.davisli.collection;
/**
 * 客车类
 * @author Administrator
 *
 */
public class Passengers {
    private String brand;
    private int passengerNum;

    public Passengers(String brand, int passengerNum) {
        this.brand = brand;
        this.passengerNum = passengerNum;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPassengerNum() {
        return passengerNum;
    }

    public void setPassengerNum(int passengerNum) {
        this.passengerNum = passengerNum;
    }
}

package cn.davisli.collection;

/**

  • 货车的父类。
  • @author Administrator
  • */
    public class Cargo {
    private String brand;
    private double cargoCount;

    public Cargo(String brand, double cargoCount) {
    this.brand = brand;
    this.cargoCount = cargoCount;
    }

    public String getBrand() {
    return brand;
    }

    public void setBrand(String brand) {
    this.brand = brand;
    }

    public double getCargoCount() {
    return cargoCount;
    }

    public void setCargoCount(double cargoCount) {
    this.cargoCount = cargoCount;
    }
    }

package cn.davisli.collection;
/**
 * 客车类
 * @author Administrator
 *
 */
public class SaloonCar extends Passengers {
    private double rental;

    public SaloonCar(String brand, int passengers, double rental) {
        super(brand, passengers);
        this.rental = rental;
    }

    public double getRental() {
        return rental;
    }

    public void setRental(double rental) {
        this.rental = rental;
    }

}

package cn.davisli.collection;

/**

  • 货车类
  • @author Administrator
  • */
    public class Coach extends Cargo {
    private double rental;

    public Coach(String brand, double cargo, double rental) {
    super(brand, cargo);
    this.rental = rental;
    }

    public double getRental() {
    return rental;
    }

    public void setRental(double rental) {
    this.rental = rental;
    }

}

package cn.davisli.collection;

/**
 * 皮卡车类,
 * @author Administrator
 *
 */
public class Pickup {
    private String brand;
    private int passengers;
    private double cargo;
    private double rental;

    public Pickup(String brand, int passengers, double cargo,double rental) {
        this.brand = brand;
        this.passengers = passengers;
        this.cargo = cargo;
        this.rental=rental;

    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPassengers() {
        return passengers;
    }

    public void setPassengers(int passengers) {
        this.passengers = passengers;
    }

    public double getCargo() {
        return cargo;
    }

    public void setCargo(double cargo) {
        this.cargo = cargo;
    }

    public double getRental() {
        return rental;
    }

    public void setRental(double rental) {
        this.rental = rental;
    }

}
package cn.davisli.collection;
/**
 * 客车集合类,
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SaloonCarList {
    private  List<SaloonCar> sal;

    //初始化ArrayList.
    public SaloonCarList() {
        this.sal = new ArrayList<SaloonCar>();
    }

    //添加元素方法.
    public void salAdd(SaloonCar obj) {
        sal.add(obj);
    }

    //客车遍历方法。
    public void getSal() {
        Iterator<SaloonCar> itSal=sal.iterator();
        System.out.println("客(轿)车类:");
        System.out.println("\t序号\t品牌\t乘员人数\t每日租金");
        int noNum=0;                                    //定义序列号.
        while(itSal.hasNext()){
            SaloonCar sl=itSal.next();
            noNum++;
            System.out.println("\t"+noNum+"\t"+sl.getBrand()+"\t"+sl.getPassengerNum()+"\t"+sl.getRental());
        }
    }

    //客车租金计算方法。
    public void calculateRental(int No,int number){
        SaloonCar sl=sal.get(No-1);
        System.out.println();
        System.out.println("\t根据租车选择,您的租金总额为:"+sl.getRental()*number+"元");
        System.out.println();
        System.out.println("车型及数量为:");
        System.out.println("\t车型:"+sl.getBrand()+"  ,乘员人数:"+sl.getPassengerNum()+" 人 ,"+"  每日租金: "+sl.getRental()+" 元"+"   租车数量:"+number+" 辆.");
    }
}
package cn.davisli.collection;
/**
 * 货车集合类
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CargoCarList {
    private List<Coach> ch;

    //初始化ArrayList.
    public CargoCarList() {
        this.ch= new ArrayList<Coach>();
    }

    //添加元素方法。
    public void chAdd(Coach obj){
        ch.add(obj);
    }

    //货车集合遍历方法。
    public void getCoach(){
        Iterator<Coach> itCoach=ch.iterator();
        System.out.println("货车类:");
        System.out.println("\t序号\t品牌\t载货吨位\t每日租金");
        int noNum=0;
        while(itCoach.hasNext()){
            Coach cl=itCoach.next();
            noNum++;
            System.out.println("\t"+noNum+"\t"+cl.getBrand()+"\t"+cl.getCargoCount()+"\t"+cl.getRental());
        }

    }

    //货车租金计算方法。
    public void calculateRental(int No,int number){
        Coach cl=ch.get(No-1);
        System.out.println();
        System.out.println("\t根据租车选择,您的租金总额为:"+cl.getRental()*number+"元");
        System.out.println();
        System.out.println("车型及数量为:");
        System.out.println("\t车型:"+cl.getBrand()+"  ,载货吨位:"+cl.getCargoCount()+" 吨 ,"+"  每日租金: "+cl.getRental()+" 元"+"   租车数量:"+number+" 辆.");

    }
}
package cn.davisli.collection;
/**
 * 皮卡车集合类
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PickupList {
    private List<Pickup> pc;

    //初始化ArrayList.
    public PickupList() {
        this.pc= new ArrayList<Pickup>();
    }

    //皮卡车元素添加方法。
    public void pcAdd(Pickup obj){
        pc.add(obj);
    }

    //集合遍历方法。
    public void getPickup(){
        Iterator<Pickup> itPickup=pc.iterator();
        System.out.println("皮卡车类:");
        System.out.println("\t序号\t品牌\t乘员人数\t载货吨位\t每日租金");
        int noNum=0;
        while(itPickup.hasNext()){
            Pickup pk=itPickup.next();
            noNum++;
            System.out.println("\t"+noNum+"\t"+pk.getBrand()+"\t"+pk.getPassengers()+"\t"+pk.getCargo()+"\t"+pk.getRental());
        }

    }

    //皮卡车租金计算方法。
    public void calculateRental(int No,int number){
        Pickup pk=pc.get(No-1);
        System.out.println();
        System.out.println("\t根据租车选择,您的租金总额为:"+pk.getRental()*number+"元");
        System.out.println();
        System.out.println("车型及数量为:");
        System.out.println("\t车型:"+pk.getBrand()+"  ,乘员人数:"+pk.getPassengers()+" 人 ,"+" 载货吨位:"+pk.getCargo()+" 吨"+"  每日租金: "+pk.getRental()+" 元"+"   租车数量:"+number+" 辆.");

    }
}
package cn.davisli.collection;

/**
 * 进入程序执行界面
 */
import java.util.Scanner;

public class Login {
    public static void login() {
        System.out.println("========欢迎光临答答租车========");
        System.out.println();
        System.out.print("是否进入租车系统?(y/n)");
        Scanner input = new Scanner(System.in);
        String in_out = input.next();
        if (in_out.equalsIgnoreCase("n")) { // 判断输入条件,忽略大小写。
            input.close(); // 关闭输入流
            System.exit(0); // 退出程序
        } else if (in_out.equalsIgnoreCase("y")) {
            System.out.println();
            System.out.println("可供租车类型:");
            System.out.println("\t1.  客(轿)车类");
            System.out.println("\t2.  货       车      类");
            System.out.println("\t3.  皮       卡      类");
            System.out.println("\t4.  退                  出");
            System.out.println();
            System.out.print("\t\t请选择(1-4):");
        } else {
            System.out.println("输入错误!程序退出!");
            input.close(); // 关闭输入流
            System.exit(0); // 退出程序执行
        }
    }

    public static void select() {
        System.out.println();
        System.out.print("\t请选择租车序号:");
    }

    public static void number() {
        System.out.println();
        System.out.print("\t请输入租车数量:");
    }

    public static void end() {
        System.out.println("程序正常退出。");
    }
}
package cn.davisli.collection;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Login.login();
        SaloonCar s1 = new SaloonCar("大众", 5, 200); // 构造客车类实例。
        SaloonCar s2 = new SaloonCar("别克", 5, 220);
        SaloonCar s3 = new SaloonCar("宝马", 5, 400);
        SaloonCar s4 = new SaloonCar("吉利", 5, 180);
        Coach c1 = new Coach("江铃", 3, 200); // 构造货车类实例。
        Coach c2 = new Coach("东风", 15, 260);
        Coach c3 = new Coach("沃尔沃", 50, 500);
        Coach c4 = new Coach("斯特林", 50, 600);
        Pickup p1 = new Pickup("长城", 5, 1.5, 200); // 构造皮卡车实例。
        Pickup p2 = new Pickup("福特", 5, 2.5, 300);
        Pickup p3 = new Pickup("中兴", 5, 1.5, 200);
        PickupList pup = new PickupList();
        CargoCarList coach = new CargoCarList();
        SaloonCarList salcar = new SaloonCarList();
        salcar.salAdd(s1); // 将客车实例增加到客车ArrayList集合中.
        salcar.salAdd(s2);
        salcar.salAdd(s3);
        salcar.salAdd(s4);
        coach.chAdd(c1); // 将货车实例增加到货车ArrayList集合中。
        coach.chAdd(c2);
        coach.chAdd(c3);
        coach.chAdd(c4);
        pup.pcAdd(p1); // 将皮卡车实例增加到皮卡ArrayList集合中。
        pup.pcAdd(p2);
        pup.pcAdd(p3);
        Scanner input = new Scanner(System.in); // 判断输入选择的车辆类型。
        int decide = input.nextInt();
        switch (decide) {
        case 1: // 条件为1时,列出客车类实例。
            salcar.getSal();
            break;
        case 2:
            coach.getCoach(); // 条件为2时,列出货车类实例。
            break;
        case 3:
            pup.getPickup();// 条件为3时,列出皮卡车实例。
            break;
        case 4:
            input.close(); // 条件为4时,退出程序。
            System.exit(0);
        }
        Login.select();
        int vehicleClass = input.nextInt();
        Login.number();
        int carNumber = input.nextInt();

        switch (decide) { // 根据选择不同类的租金计算方法。
        case 1:
            salcar.calculateRental(vehicleClass, carNumber);
            break;
        case 2:
            coach.calculateRental(vehicleClass, carNumber);
            break;
        case 3:
            pup.calculateRental(vehicleClass, carNumber);
            break;

        }
        Login.end();
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP