手记

Java入门第二季-项目实战

本人比较懒,所以没有冗余地去定义三个对象组用来存储。但是直接定义Car类的对象进行重载又很不方便。
这时候我突然想到视频中老师提到的“object类是所有类的父类”这一特性,巧妙地使用了这个特点,直接定义了一个长度确定的object对象,并分别重载,是本次实践的一个特色。
价钱都是随机生成的,也借此机会了解了一下Java的输入流、随机数函数等。
今天是第一天开始学习Java,但是程序设计的课已经不知道上了多少,而且之前用其他各种语言也写了很多代码,所以没有什么值得好骄傲的。唯一值得庆幸的,就是每天都在学习心的东西,把自己的想法用不同的编程语言表现出来。
Java的确是一门优秀的语言,体现在他有丰富的库和接口,特别是“一切面向对象”的特性,非常切合现实生活和生产,让我一写就上瘾了。

附:本次课设源代码

Car基类

package FinalProject;

public abstract class Car {
    String carName;
    boolean isGoods;
    int goodsCount;
    int mannedCount;
    boolean isManned;
    float price;
    public abstract void showInfo();
    public abstract void setName(String thisString);
    public void setCount(int i) {}
    public void setCount(int i,int j) {}
    public abstract void setPrice(float i);
}

载人、载物、双载子类

package FinalProject;

public class Manned extends Car {
    @Override
    public void setName(String thisName){ 
        carName = thisName;
        isGoods = false;
        goodsCount = 0;
        isManned = true;
    }
    @Override
    public void setCount(int thisCount){
        mannedCount = thisCount;
    }
    @Override
    public void setPrice(float thisPrice){
        price = thisPrice;
    }

    @Override
    public void showInfo() {
        // TODO Auto-generated method stub
        System.out.print("车名:"+carName+"  载客量:"+mannedCount+"  价格:"+price);
    }

}

package FinalProject;

public class Goods extends Car {
    @Override
    public void setName(String thisName){
        carName = thisName;
        isGoods = true;
        isManned = false;
        isManned = false;
        mannedCount = 0;
    }
    @Override
    public void setCount(int thisCount){
        goodsCount = thisCount;
    }
    @Override
    public void setPrice(float thisPrice){
        price = thisPrice;
    }

    @Override
    public void showInfo() {
        // TODO Auto-generated method stub

    }

}

package FinalProject;

public class Pika extends Car {
    @Override
    public void setName(String thisName){
        carName = thisName;
        isManned = true;
        isGoods = true;
    }
    @Override
    public void setCount(int thisMannedCount,int thisGoodsCount){
        mannedCount = thisMannedCount;
        goodsCount = thisGoodsCount;
    }
    @Override
    public void setPrice(float thisPrice){
        price = thisPrice;
    }

    @Override
    public void showInfo() {
        // TODO Auto-generated method stub

    }

}

main代码

                                                              package FinalProject;

import java.io.IOException;
import java.util.Scanner;

public class main {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int num;
        Car thisCar;
        Object car[] = new Object[7];
        for(num = 0;num < 3;num+=1){
            thisCar = new Manned();
            car[num] = thisCar;
            ((Car)car[num]).setName("大众Z"+num);
            ((Car) car[num]).setPrice((int)(Math.random()*80+100));
            ((Car) car[num]).setCount((int)(1+Math.random()*(50-10+1)));
        }
        for(;num < 5;num+=1){
            thisCar = new Goods();
            car[num] = thisCar;
            ((Car) car[num]).setName("东风神威"+num);
            ((Car) car[num]).setPrice((int)(1+Math.random()*70+190));
            ((Car) car[num]).setCount((int)(1+Math.random()*(50-10+1)));
        }
        for(;num<7;num+=1){
            thisCar = new Pika();
            car[num] = thisCar;
            ((Car) car[num]).setName("丰田GT"+num);
            ((Car) car[num]).setPrice((int)(1+Math.random()*85+100));
            ((Car) car[num]).setCount((int)(1+Math.random()*(50-10+1)),(int)(1+Math.random()*(50-10+1)));
        }

        int choice = 0;
        int days = 0;
        String getchar;
        Scanner scanner = new Scanner(System.in);
        while(choice!=1){
            System.out.println("欢迎来到英雄联盟");
            System.out.println("输入1租车,输入其他退出");
            choice = scanner.nextInt();
        }

//      print the car list
        for(num = 0;num < 7;num+=1){
            System.out.println(num+"、"+"车名:"+((Car)car[num]).carName+"  价格:"+((Car)car[num]).price);
            if(((Car)car[num]).isManned){
                System.out.println("  载客量:"+((Car)car[num]).mannedCount+"人");
            }
            if(((Car)car[num]).isGoods){
                System.out.println("  载货量:"+((Car)car[num]).goodsCount+"吨");
            }
        }
        do{
            System.out.println("请输入租车代码");
            choice = scanner.nextInt();
        }while(choice>num||choice<0);
        while(days == 0){
            System.out.println("请输入租车时间");
            days = scanner.nextInt();
        }
        System.out.println("租车成功!下面打印租车信息\n");
        System.out.println(((Car)car[choice]).carName);
        if(((Car)car[choice]).isManned){
            System.out.println("  载客量:"+((Car)car[choice]).mannedCount+"人");
        }
        if(((Car)car[choice]).isGoods){
            System.out.println("  载货量:"+((Car)car[choice]).goodsCount+"吨");
        }
        System.out.println("  价格:"+((Car)car[choice]).price*days);
    } 

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