手记

java入门第二季之达达租车系统

package com.imooc.controller;

import java.util.Scanner;

import com.imooc.model.*;
import com.imooc.view.*;

public class ChoiceCar {

    public final void go(){
        Scanner input = new Scanner(System.in);

        InputTips.inputCarNumber();
        int carNumber = getCarNumber(input);
        Car[] cars = new Car[carNumber];
        int[] days = new int[carNumber];
        for (int i=0; i<carNumber; i++){
            InputTips.inputCarId();
            int carId = getCarId(input);
            cars[i] = ShowCars.CARS[carId-1];
            InputTips.inputDays();
            days[i] = getDays(input);
        }
        GetBill getBill = new GetBill(cars,days);
        getBill.show();
        MyExit.bye();
    }

    public int getCarNumber(Scanner input){
        char carNumber ;
        while(true){
            String s = input.nextLine();
            char[] c = s.toCharArray();
            carNumber = c[0];
            if(carNumber == 'q'){
                MyExit.bye();
            }
            if(!(carNumber >= '1' && carNumber <= '9')){
                //TODO
                //ShowCars.show();
                InputTips.errInput();
            }else{
                break;
            }
        }
        //InputTips.showInput(carNumber);
        return carNumber - 48;
    }

    public int getCarId(Scanner input){
        char carId;
        while(true){
            String s = input.nextLine();
            char[] c = s.toCharArray();
            carId = c[0];
            if(carId == 'q'){
                MyExit.bye();
            }
            if(!(carId >= '1' && carId <= '9')){
                //TODO
                //ShowCars.show();
                InputTips.errInput();
            }else{
                break;
            }
        }
        //InputTips.showInput(carId);
        return carId - 48;
    }

    public int getDays(Scanner input){
        char days;
        while(true){
            String s = input.nextLine();
            char[] c = s.toCharArray();
            days = c[0];
            if(days == 'q'){
                MyExit.bye();
            }
            if(!(days >= '1' && days <= '7')){
                //TODO
                //ShowCars.show();
                InputTips.errInput();
            }else{
                break;
            }
        }
        //InputTips.showInput(days);
        return days - 48;
    }

}
package com.imooc.controller;

import com.imooc.model.Car;
import com.imooc.view.ShowBill;

public class GetBill {
    private Car[] cars;
    private int[] days;
    public GetBill(Car[] cars,int[] days){
        this.cars = cars;
        this.days = days;
    }

    public final void show(){
        ShowBill.showList(cars, days);
        //佣金
        float price = 0;
        for (int i = 0; i<cars.length; i++) {
            price += days[i]*cars[i].getDailyRent();
        }
        ShowBill.showPrice(price);
    }

    public Car[] getCars() {
        return cars;
    }
    public void setCars(Car[] cars) {
        this.cars = cars;
    }
    public int[] getDays() {
        return days;
    }
    public void setDays(int[] days) {
        this.days = days;
    }
}
package com.imooc.controller;

public class Initail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Welcome welcome = new Welcome();
        welcome.go();
    }
}
package com.imooc.controller;

import java.util.Scanner;

import com.imooc.view.InputTips;
import com.imooc.view.MyExit;
import com.imooc.view.ShowCars;

public class Welcome {

    public final void go (){

        Scanner input = new Scanner(System.in);
        InputTips.welcome();
        InputTips.inputChoice();
        char choice;
        while(true){
            String s = input.nextLine();
            char[] c = s.toCharArray();
            choice = c[0];
            if(choice == 'q'){
                MyExit.bye();
            }
            if(!(choice == '1'  choice == '0')){
                InputTips.errInput();
            }else{
                break;
            }
        }
        if (choice == '1'){
            ShowCars.show();
            ChoiceCar choiceCar = new ChoiceCar();
            choiceCar.go();
        }else{
            MyExit.bye();
        }
    }

}
package com.imooc.model;

public abstract class Car {
    private float dailyRent;
    private String brand;

    public Car (String brand,float dailyRent){
        this.brand = brand;
        this.dailyRent = dailyRent;
    }

    public float getDailyRent() {
        return dailyRent;
    }
    public void setDailyRent(float dailyRent) {
        this.dailyRent = dailyRent;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }

}
package com.imooc.model;

public class Coach extends Car implements IManned {
    private int capacity;

    public Coach (String brand,float dailyRent,int capacity){
        super(brand,dailyRent);
        this.capacity = capacity;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    @Override
    public void printCapacity() {
        // TODO Auto-generated method stub
        System.out.print("载客量:" + capacity + "人  ");
    }

}
package com.imooc.model;

public interface IFreignt {
    public void printBurden();
}
package com.imooc.model;

public interface IManned {
    public void printCapacity();
}
package com.imooc.model;

public class Pickup extends Car implements IManned, IFreignt {

    private int capacity;
    private float burden;

    public Pickup (String brand,float dailyRent,float burden,int capacity){
        super(brand,dailyRent);
        this.burden = burden;
        this.capacity = capacity;
    }

    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public float getBurden() {
        return burden;
    }
    public void setBurden(float burden) {
        this.burden = burden;
    }

    @Override
    public void printBurden() {
        // TODO Auto-generated method stub
        System.out.print("载货量:" + burden + "吨  ");
    }

    @Override
    public void printCapacity() {
        // TODO Auto-generated method stub
        System.out.print("载客量:" + capacity + "人  ");
    }

}
package com.imooc.model;

public class Truck extends Car implements IFreignt{
    private float burden;

    public Truck (String brand,float dailyRent,float burden){
        super(brand,dailyRent);
        this.burden = burden;
    }

    public float getBurden() {
        return burden;
    }

    public void setBurden(float burden) {
        this.burden = burden;
    }

    @Override
    public void printBurden() {
        // TODO Auto-generated method stub
        System.out.print("载货量:" + burden + "吨  ");
    }

}
package com.imooc.view;

public class InputTips {
    public static final void welcome(){
        System.out.println("欢迎使用达达租车系统");
        System.out.println("请问您是否需要租车");
        System.out.println("输入:1 表示是,0 表示否,q表示退出程序");
    }
    public static final void inputChoice() {
        System.out.print("请输入您的选择:");
    }
    public static final void errInput() {
        System.out.print("输入有误,请重新输入:");
    }
    public static final void showInput(char input) {
        System.out.println("您的选择是:" + input);
    }
    public static final void inputCarNumber(){
        System.out.print("请输入您需要租几辆车(1~9):");
    }
    public static final void inputCarId(){
        System.out.print("请输入车辆编号(1~9):");
    }
    public static final void inputDays(){
        System.out.print("请输入天数(1~7):");
    }

}
package com.imooc.view;

public class MyExit {
    public static final void bye(){
        System.out.println("欢迎使用,再见!");
        System.exit(0);
    }
}
package com.imooc.view;

import com.imooc.model.Car;

public class ShowBill {

    public static void showList(Car[] cars,int[] days){
        System.out.println("*****************************************");
        System.out.println("您的账单信息如下:");
        for (int i = 0; i < cars.length; i++){
            ShowCars.print(cars[i]);
            System.out.println("租用  " + days[i] + "天  ");
        }
    }

    public static void showPrice(float price) {
        // TODO Auto-generated method stub
        System.out.println("总价格为:" + price);
    }
}
package com.imooc.view;

import com.imooc.model.*;

public class ShowCars {
    public static final Car[] CARS = {
            new Truck("Truck1",999f,20f),
            new Truck("Truck2",888f,16f),
            new Truck("Truck3",777f,10f),

            new Coach("Coach4",666f,40),
            new Coach("Coach5",555f,25),
            new Coach("Coach6",444f,15),

            new Pickup("Pikup7",333f,5f,12),
            new Pickup("Pikup8",222f,4f,10),
            new Pickup("Pikup9",111f,3f,8),
    };

    public static final void show(){
        System.out.println("                您可租车的类型及价目表");
        System.out.println("*****************************************");

        System.out.println("序号  汽车名称  租金  容量");
        for(int i = 0;i<CARS.length;i++){
            System.out.print(i+1 +"  ");
            print(CARS[i]);
            System.out.println();
        }
        System.out.println("*****************************************");

    }

    public static final void print(Car car){
        if(car instanceof Car){
            //输出基本信息
            System.out.print(car.getBrand() + "  " +car.getDailyRent() + "元/每天  ");
        }
        if(car instanceof IFreignt){
            //输出载货信息
            IFreignt f = (IFreignt)car;
            f.printBurden();
        }
        if(car instanceof IManned){
            //输出载人信息
            IManned m = (IManned)car;
            m.printCapacity();
        }

    }

/*
    public void printInformation (int num){
        System.out.println("载客量:" + num + "人  ");
    }

    public void printInformation (float num){
        System.out.println("载货量:" + num + "吨  ");
    }
*/

}
11人推荐
随时随地看视频
慕课网APP