打了半天总算差不多搞定了,总感觉代码好多能优化一下,但是又不知道该怎么弄

来源:12-1 综合练习

Yignore

2019-09-19 14:40

// 车车的父类
package rentCar;
public class AutoCar {		
    private boolean flagPerson;
    private boolean flagGoods;	
    private int capacityPer;	
    private int capacityGoods;	
    private int price;	
    private String name;		
    
    public AutoCar(boolean flagPerson, boolean flagGoods, int capacityPer, int capacityGoods, int price, String name) {
        this.flagPerson = flagPerson;
        this.flagGoods = flagGoods;
        his.capacityPer = capacityPer;
        this.capacityGoods = capacityGoods;		
        this.price = price;		
        this.name = name;	
    }		
    
    // getter & setter 方法
    public boolean isFlagPerson() {		return flagPerson;	}	
    public void setFlagPerson(boolean flagPerson) {		this.flagPerson = flagPerson;	}	
    public boolean isFlagGoods() {		return flagGoods;	}	
    public void setFlagGoods(boolean flagGoods) {		this.flagGoods = flagGoods;	}	
    public int getCapacityPer() {		return capacityPer;	}	
    public void setCapacityPer(int capacityPer) {		this.capacityPer = capacityPer;	}	
    public int getCapacityGoods() {		return capacityGoods;	}	
    public void setCapacityGoods(int capacityGoods) {		this.capacityGoods = capacityGoods;	}		
    public int getPrice() {		return price;	}	public void setPrice(int price) {		this.price = price;	}	
    public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	
    
    // toString 方法
    @Override	public String toString() {		return "AutoCar [capacityPer=" + capacityPer + ", capacityGoods=" + capacityGoods + "]";	}}
    
    
// 6个子类,分别对应6种车车
package rentCar;
public class AodiA4 extends AutoCar{	
    public AodiA4() {		
        super(true,false,4,0,500,"奥迪A4");	
     }
}

package rentCar;
public class Mazida6 extends AutoCar {		
public Mazida6() {		
    super(true,false,4,0,400,"马自达6");	
}}

package rentCar;
public class Pikaxue6 extends AutoCar {		
    public Pikaxue6() {		
        super(true,true,4,2,450,"皮卡学6");	
}}

package rentCar;
public class Jinlong extends AutoCar {	
    public Jinlong() {		
        super(true,false,20,0,800,"金龙");	
    }
}

package rentCar;
public class Songhuajiang extends AutoCar {	
    public Songhuajiang() {		
    super(false,true,0,4,400,"松花江");	
    }
}

package rentCar;
public class Yiweike extends AutoCar{	
    public Yiweike() {		
        super(false,true,0,20,1000,"依维柯");	
        }
}

//Main 方法
package rentCar;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {	
    public static void main(String[] args) {				
        // 菜单		
        System.out.println("欢迎使用");		
        System.out.println("您是否要租车: 1是 0否");				
        Scanner in = new Scanner(System.in);		
        int choose = in.nextInt();		
        if( choose == 1 ) {			
            System.out.println("您可租的车车类型及价目表");			
            System.out.println("序号       汽车名称        租金            容量");			
            System.out.println("1      奥迪a4    500/天   载人4人\n"					
                + "2      马自达6     400/天      载人4人 \n"					
                + "3      金龙奥迪a4 800/天   载人20人 \n"					
                + "4      松花江      400/天       载货4吨 \n"					
                + "5      依维柯      1000/天     载货20吨 \n"					
                + "6      皮卡雪      450/天       载货2吨载人4人 \n");			
           
            System.out.println("请输入你要租车的数量");						
            Scanner in2 = new Scanner(System.in);			
            int quantity = in2.nextInt();						
            
            List<AutoCar> listCar = new ArrayList<AutoCar>();			
            for( int i=1; i<=quantity; i++) {				
                System.out.println("请输入第" + i + "辆车的序号");								
                Scanner in3 = new Scanner(System.in);				
                int number = in3.nextInt();								
                
                switch(number) {				
                case 1: listCar.add(new AodiA4()); break;				
                case 2: listCar.add(new Mazida6()); break;				
                case 3: listCar.add(new Jinlong()); break;				
                case 4: listCar.add(new Songhuajiang()); break;				
                case 5: listCar.add(new Yiweike()); break;				
                case 6: listCar.add(new Pikaxue6()); break;				
                }			
            }						
            
            System.out.println("请输入租车天数");			
            Scanner in4 = new Scanner(System.in);			
            int days = in4.nextInt();						
            
            System.out.println("您的账单");			
            System.out.println("***可载人的车有:");			
            for(int i=0; i<quantity; i++) {				
                if(listCar.get(i).isFlagPerson())					
                System.out.print(listCar.get(i).getName() + "   ");			
             }						
             
             System.out.println();			
             System.out.println("可载人:");			
             int totalPer = 0;			
             for(int i=0; i<quantity; i++) {				
                 totalPer += listCar.get(i).getCapacityPer();			
             }			
             System.out.print(totalPer);						
             
             System.out.println();			
             System.out.println("***可载货的车有:");			
             for(int i=0; i<quantity; i++) {				
                 if(listCar.get(i).isFlagGoods())					
                 System.out.print(listCar.get(i).getName());			
             }						
             
             System.out.println();			
             System.out.println("可载货:");			
             int totalGoods = 0;			
             for(int i=0; i<quantity; i++) {				
                 totalGoods += listCar.get(i).getCapacityGoods();			
             }			
             System.out.print(totalGoods);						
             
             System.out.println();			
             int totalPrice = 0;			
             for(int i=0; i<quantity; i++)				
             totalPrice += listCar.get(i).getPrice();			
             System.out.println("租车总价格:" + totalPrice + "元");					
             }		
         else			
             System.out.println("Bye~");				
         }
     }


写回答 关注

2回答

  • qq_慕后端7456332
    2019-10-26 16:29:55

    你的显示应该车型应该用数组吧,Array,不然用的用的都是基础代码,没有体现到你学的新内容

  • Yignore
    2019-09-19 14:43:59
    package rentCar;public class AodiA4 extends AutoCar{	public AodiA4() {		super(true,false,4,0,500,"奥迪A4");	}}


Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530093 学习 · 6086 问题

查看课程

相似问题