2.5 阶段练习(面向对象)

2016-09-22  本文已影响0人  夏沫xx
第一种
Person.java
public class Person {
    public void say(){
        System.out.println("人类具有说话的能力");    
    }
}

American.java
public class American {
    public void say(){
        System.out.println("美国人说英语");   
    }
}

Chinese.java
public class Chinese  {
    public void say(){
        System.out.println("中国人说中国话");  
    }
}


Test.java
public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chinese p=new Chinese();
        American p2=new American();
        p.say();
        p2.say();
    }
}
第二种
Person.java
public class Person {
    public void say(){
        System.out.println("人类具有说话的能力");    
    }
}

American.java
public class American extends Person {
    public void say(){
        System.out.println("美国人说英语");   
    }
}

Chinese.java
public class Chinese extends Person {
    public void say(){
        System.out.println("中国人说中国话");  
    }
}

Test.java
public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person p=new Chinese();
        Person p2=new American();
        p.say();  
        p2.say();
    }
}

第三种
Person.java
public class Person {
    public void say(){
        System.out.println("人类具有说话的能力");    
    }
}

American.java
public class American extends Aperson {
    public void say(){
        System.out.println("美国人说英语");   
    }
}

Chinese.java
public class Chinese extends Aperson {
    public void say(){
        System.out.println("中国人说中国话");  
    }
}

Aperson.java
public abstract class Aperson {
    public abstract void say();
}

Test.java
public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Aperson p=new Chinese();
        Aperson p2=new American();
        p.say();  
        p2.say();
    }
}

综合练习
一、项目背景介绍
根据所学知识,编写一个控制台版“哒哒租车系统”
1、展示所有可租车辆
2、选择车型、租车量
3、展示租车清单,包含:总金额、总载货量及其车型、总载人量及其车型
二、项目分析
1、数据模型分析

Initail.java

package dada;
import java.util.Scanner;
public class Initail{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        //用户输入序号num
        int num;
        //循环判断用户输入值
        do{
            System.out.println("*****欢迎使用哒哒租车系统*****");
            System.out.println("您是否需要租车:1是    0否");
            num=input.nextInt();
            if(num==1){
                System.out.println("您可租车的类型及其价目表");
                System.out.println("序号   汽车名称    租金              容量");
                Vehicle audi     =new Vehicle(1,"奥迪A4",500,4,0);
                Vehicle mazda    =new Vehicle(2,"马自达6",400,4,0);
                Vehicle pickup   =new Vehicle(3,"皮卡雪6",450,4,2);
                Vehicle kinglong =new Vehicle(4,"金龙",800,20,0);
                Vehicle sonhj    =new Vehicle(5,"松花江",400,0,4);
                Vehicle naveco   =new Vehicle(6,"依维柯",1000,0,20);
                System.out.println("请输入您要租车的数量");
                num=input.nextInt();
                //可载人的车名数组
                String[] personVeName=new String[num];
                //可载货的车名数组
                String[] cagoVeName=new String[num];
                //用户输入的车辆序号veNum
                int veNum;
                //总载人数
                int sumPer=0;
                //总载货量
                double sumCag=0;
                //总单价
                double sumPP=0;
                //总载人数
                int personVeSum=0;
                //总载货量
                int cagoVeSum=0;
                for(int i=1;i<=num;i++){
                    System.out.println("请输入第"+i+"车辆的序号:");
                    veNum=input.nextInt();
                    //合计总载人数、总载货量、总单价、分别总车数
                    switch(veNum){
                        case 1:
                            sumPer+=audi.capPerson;
                            sumPP+=audi.price;
                            personVeSum++;
                            personVeName[personVeSum-1]=audi.name ;
                            break;
                        case 2:
                            sumPer+=mazda.capPerson;
                            sumPP+=mazda.price;
                            personVeSum++;
                            personVeName[personVeSum-1]=mazda.name ;
                            break;
                        case 3:
                            sumPer+=pickup.capPerson;
                            sumPP+=pickup.capCago;
                            sumPP+=pickup.price;
                            personVeSum++;
                            cagoVeSum++;
                            personVeName[personVeSum-1]=pickup.name ;
                            cagoVeName[cagoVeSum-1]=pickup.name;
                            break;
                        case 4:
                            sumPer+=kinglong.capPerson;
                            sumPP+=kinglong.price;
                            personVeSum++;
                            personVeName[personVeSum-1]=kinglong.name ;
                            break;
                        case 5:
                            sumCag+=pickup.capCago;
                            sumPP+=sonhj.price;
                            cagoVeSum++;
                            cagoVeName[cagoVeSum-1]=pickup.name ;
                            break;
                        case 6:
                            sumCag+=pickup.capCago;
                            sumPP+=naveco.price;
                            cagoVeSum++;
                            cagoVeName[cagoVeSum-1]=pickup.name ;
                            break;
                    }
                }
                System.out.println("请输入租车天数:");
                //用户租车天数rentDays
                int rentDays=input.nextInt();
                System.out.println("您的账单:");
                //System.out.println("***共载人数为:"+sumCap);
                System.out.println("***可载人的车有:");
                for(int i=0;i<personVeSum;i++){
                    System.out.println("   "+personVeName[i]);
                }
                System.out.println("   共载人:"+sumPer+"人");
                System.out.println("***可载货的车有:");
                for(int i=0;i<cagoVeSum;i++){
                    System.out.println("   "+cagoVeName[i]);
                }
                System.out.println("   共载货:"+sumCag+"吨");
                System.out.println("***租车总价格:"+sumPP*rentDays);
                input.close();
                break;
            }else if(num==0){
                System.out.println("谢谢您的使用!欢迎再次光临!");
                continue;
            }else{
                System.out.println("您的输入有误请重新输入:");
                continue;
            }
        }while(true);
    }
}




Vehicle.java

package dada;
public class Vehicle {
    int VeNum; //车序号
    String name;  //车名
    int price; //租金
    int capPerson;//载客量
    double capCago;//载货量
    public Vehicle(int VeNum,String name,int price,int capPerson,double capCago){
        this.VeNum=VeNum;
        this.name=name;
        this.price=price;
        this.capPerson=capPerson;
        this.capCago=capCago;
        if(capCago==0){
            System.out.println(VeNum+"."+this.name+"  "+this.price+"元/天"+": "+this.capPerson+"人");
        }else if(capPerson==0){
            System.out.println(VeNum+"."+this.name+"  "+this.price+"元/天"+": "+this.capCago+"吨");
        }else{
            System.out.println(VeNum+"."+this.name+"  "+this.capPerson+"人"+": "+this.capCago+"吨");
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读