分享我的代码

来源:12-2 项目问题解析 1

天域知语城

2018-06-27 12:32

package dadarent;import java.util.Scanner;public class DadaRentSystem {	public static void main(String[] args) {		// TODO 自动生成的方法存根		System.out.println("欢迎使用答答租车系统:");		System.out.println("您是否要租车:1是 0否");		Scanner ipt = new Scanner (System.in);		int yn = ipt.nextInt();		if (yn==0){			ipt.close();			return;		}		else{			System.out.println("您可租车的类型及其价目表:");			System.out.println("序号	汽车名称	租金	容量");			AudiA4 aud = new AudiA4();			Mazda6 maz = new Mazda6();			ChevroletPickup6 chv = new ChevroletPickup6();			Kinglong klg = new Kinglong();			HAMCSungari sgr = new HAMCSungari();			Iveco ivc = new Iveco();			System.out.println(aud.num()+".	"+aud.type()+"	"+aud.price()+"元/天	"+"载人:"+aud.PassengerCapacity()+"人");			System.out.println(maz.num()+".	"+maz.type()+"	"+maz.price()+"元/天	"+"载人:"+maz.PassengerCapacity()+"人");			System.out.println(chv.num()+".	"+chv.type()+"	"+chv.price()+"元/天	"+"载人:"+chv.PassengerCapacity()+"人 "+"载货:"+chv.CargoCapacity()+"吨");			System.out.println(klg.num()+".	"+klg.type()+" 	"+klg.price()+"元/天	"+"载人:"+klg.PassengerCapacity()+"人");			System.out.println(sgr.num()+".	"+sgr.type()+"	"+sgr.price()+"元/天	"+"载货:"+sgr.CargoCapacity()+"吨");			System.out.println(ivc.num()+".	"+ivc.type()+"	"+ivc.price()+"元/天	"+"载货:"+ivc.CargoCapacity()+"吨");			System.out.println("请输入您要租汽车的数量:");			int n = ipt.nextInt();			String carriage = "" , lorry = "";			int passenger = 0 , vehicleNum = 0;			double cargo = 0.0 , price = 0.0;			for (int i = 1 ; i <= n ; i++ ){				System.out.println("请输入第"+i+"辆车的序号:");				vehicleNum = ipt.nextInt();				switch (vehicleNum){				case 1:					carriage = carriage + "	" + aud.type();					passenger = passenger + aud.PassengerCapacity();					price = price + aud.price();					break;				case 2:					carriage = carriage + "	" + maz.type();					passenger = passenger + maz.PassengerCapacity();					price = price + maz.price();					break;				case 3:					carriage = carriage + "	" + chv.type();					lorry = lorry + "	" + chv.type();					passenger = passenger + chv.PassengerCapacity();					cargo = cargo + chv.CargoCapacity();					price = price + chv.price();					break;				case 4:					carriage = carriage + "	" + klg.type();					passenger = passenger + klg.PassengerCapacity();					price = price + klg.price();					break;				case 5:					lorry = lorry + "	" + sgr.type();					cargo = cargo + sgr.CargoCapacity();					price = price + sgr.price();					break;				case 6:					lorry = lorry + "	" + ivc.type();					cargo = cargo + ivc.CargoCapacity();					price = price + ivc.price();					break;				default:					System.out.println("错误输入");					break;				}			}			System.out.println("请输入租车天数:");			int day = ipt.nextInt();			price = price * day;			ipt.close();			System.out.println("您的账单:");			System.out.println("***可载人的车有:");			System.out.println(carriage+"	共载人:"+passenger+"人");			System.out.println("***载货的车有:");			System.out.println(lorry+"	共载货:"+cargo+"吨");			System.out.println("租车总价格:"+price+"元");		}		return;	}}


写回答 关注

10回答

  • 天域知语城
    2018-06-27 12:49:46

    package dadarent;


    public abstract class Vehicle {

    public abstract int num();

    public abstract String type();

    public abstract double price();

    }


  • 天域知语城
    2018-06-27 12:49:16

    package dadarent;


    public interface IPassengerCap {

    public abstract int PassengerCapacity ();

    }


  • 天域知语城
    2018-06-27 12:48:48

    package dadarent;


    public interface ICargoCap {

    public abstract double CargoCapacity ();

    }


  • 天域知语城
    2018-06-27 12:48:16

    package dadarent;


    public class AudiA4 extends Vehicle implements IPassengerCap {


    @Override

    public int PassengerCapacity() {

    // TODO 自动生成的方法存根

    return 4;

    }


    @Override

    public String type() {

    // TODO 自动生成的方法存根

    return "奥迪A4";

    }


    @Override

    public double price() {

    // TODO 自动生成的方法存根

    return 500.0;

    }


    @Override

    public int num() {

    // TODO 自动生成的方法存根

    return 1;

    }


    }


  • 天域知语城
    2018-06-27 12:47:07

    package dadarent;


    public class Mazda6 extends Vehicle implements IPassengerCap {


    @Override

    public int PassengerCapacity() {

    // TODO 自动生成的方法存根

    return 4;

    }


    @Override

    public String type() {

    // TODO 自动生成的方法存根

    return "马自达6";

    }


    @Override

    public double price() {

    // TODO 自动生成的方法存根

    return 400.0;

    }


    @Override

    public int num() {

    // TODO 自动生成的方法存根

    return 2;

    }


    }


  • 天域知语城
    2018-06-27 12:46:35

    package dadarent;


    public class ChevroletPickup6 extends Vehicle implements IPassengerCap, ICargoCap {


    @Override

    public double CargoCapacity() {

    // TODO 自动生成的方法存根

    return 2.0;

    }


    @Override

    public int PassengerCapacity() {

    // TODO 自动生成的方法存根

    return 4;

    }


    @Override

    public String type() {

    // TODO 自动生成的方法存根

    return "皮卡雪6";

    }


    @Override

    public double price() {

    // TODO 自动生成的方法存根

    return 450.0;

    }


    @Override

    public int num() {

    // TODO 自动生成的方法存根

    return 3;

    }


    }


  • 天域知语城
    2018-06-27 12:46:04

    package dadarent;


    public class Kinglong extends Vehicle implements IPassengerCap {


    @Override

    public int PassengerCapacity() {

    // TODO 自动生成的方法存根

    return 20;

    }


    @Override

    public String type() {

    // TODO 自动生成的方法存根

    return "  金龙";

    }


    @Override

    public double price() {

    // TODO 自动生成的方法存根

    return 800.0;

    }


    @Override

    public int num() {

    // TODO 自动生成的方法存根

    return 4;

    }


    }


  • 天域知语城
    2018-06-27 12:45:38

    package dadarent;


    public class HAMCSungari extends Vehicle implements ICargoCap {


    @Override

    public double CargoCapacity() {

    // TODO 自动生成的方法存根

    return 4.0;

    }


    @Override

    public String type() {

    // TODO 自动生成的方法存根

    return " 松花江";

    }


    @Override

    public double price() {

    // TODO 自动生成的方法存根

    return 400.0;

    }


    @Override

    public int num() {

    // TODO 自动生成的方法存根

    return 5;

    }


    }


  • 天域知语城
    2018-06-27 12:45:04

    package dadarent;


    public class Iveco extends Vehicle implements ICargoCap {

            

             @Override

             public double CargoCapacity() {

                     // TODO 自动生成的方法存根

                     return 20.0;

             }

            

             @Override

             public String type() {

                     // TODO 自动生成的方法存根

                     return " 依维柯";

             }

            

             @Override

             public double price() {

                     // TODO 自动生成的方法存根

                     return 1000.0;

             }

            

             @Override

             public int num() {

                     // TODO 自动生成的方法存根

                     return 6;

             }

            

    }


  • 天域知语城
    2018-06-27 12:43:30
    package dadarent;
    import java.util.Scanner;
    public class DadaRentSystem {
    	public static void main(String[] args) {
        	// TODO 自动生成的方法存根
    		System.out.println("欢迎使用答答租车系统:");
    		System.out.println("您是否要租车:1是 0否");
    		Scanner ipt = new Scanner (System.in);
    		int yn = ipt.nextInt();
    		if (yn==0){			
    		    ipt.close();			
    		    return;		
    	        }		
    	        else{			
    	            System.out.println("您可租车的类型及其价目表:");
    	            System.out.println("序号	汽车名称	租金	容量");	
    	            AudiA4 aud = new AudiA4();			
    	            Mazda6 maz = new Mazda6();			
    	            ChevroletPickup6 chv = new ChevroletPickup6();			
    	            Kinglong klg = new Kinglong();			
    	            HAMCSungari sgr = new HAMCSungari();			
    	            Iveco ivc = new Iveco();			
    	            System.out.println(aud.num()+".	"+aud.type()+"	"+aud.price()+"元/天	"+"载人:"+aud.PassengerCapacity()+"人");
    	            System.out.println(maz.num()+".	"+maz.type()+"	"+maz.price()+"元/天	"+"载人:"+maz.PassengerCapacity()+"人");			
    	            System.out.println(chv.num()+".	"+chv.type()+"	"+chv.price()+"元/天	"+"载人:"+chv.PassengerCapacity()+"人 "+"载货:"+chv.CargoCapacity()+"吨");
    	            System.out.println(klg.num()+".	"+klg.type()+" 	"+klg.price()+"元/天	"+"载人:"+klg.PassengerCapacity()+"人");			
    	            System.out.println(sgr.num()+".	"+sgr.type()+"	"+sgr.price()+"元/天	"+"载货:"+sgr.CargoCapacity()+"吨");			
    	            System.out.println(ivc.num()+".	"+ivc.type()+"	"+ivc.price()+"元/天	"+"载货:"+ivc.CargoCapacity()+"吨");			
    	            System.out.println("请输入您要租汽车的数量:");			
    	            int n = ipt.nextInt();			
    	            String carriage = "" , lorry = "";			
    	            int passenger = 0 , vehicleNum = 0;			
    	            double cargo = 0.0 , price = 0.0;			
    	            for (int i = 1 ; i <= n ; i++ ){				
            	            System.out.println("请输入第"+i+"辆车的序号:");				
            	            vehicleNum = ipt.nextInt();				
            	            switch (vehicleNum){				
                    	            case 1:					
                    	            carriage = carriage + "	" + aud.type();					
                    	            passenger = passenger + aud.PassengerCapacity();					
                    	            price = price + aud.price();					
                    	            break;				
                    	            case 2:					
                    	            carriage = carriage + "	" + maz.type();					
                    	            passenger = passenger + maz.PassengerCapacity();					
                    	            price = price + maz.price();					
                    	            break;				
                    	            case 3:					
                    	            carriage = carriage + "	" + chv.type();					
                    	            lorry = lorry + "	" + chv.type();					
                    	            passenger = passenger + chv.PassengerCapacity();					
                    	            cargo = cargo + chv.CargoCapacity();					
                    	            price = price + chv.price();					
                    	            break;				
                    	            case 4:					
                    	            carriage = carriage + "	" + klg.type();					
                    	            passenger = passenger + klg.PassengerCapacity();					
                    	            price = price + klg.price();					
                    	            break;				
                    	            case 5:					
                    	            lorry = lorry + "	" + sgr.type();					
                    	            cargo = cargo + sgr.CargoCapacity();					
                    	            price = price + sgr.price();					
                    	            break;				
                    	            case 6:					
                    	            lorry = lorry + "	" + ivc.type();					
                    	            cargo = cargo + ivc.CargoCapacity();					
                    	            price = price + ivc.price();					
                    	            break;				
                    	            default:					
                    	            System.out.println("错误输入");					
                    	            break;				
                                }
                        }			
                        System.out.println("请输入租车天数:");			
                        int day = ipt.nextInt();			
                        price = price * day;			
                        ipt.close();			
                        System.out.println("您的账单:");			
                        System.out.println("***可载人的车有:");			
                        System.out.println(carriage+"	共载人:"+passenger+"人");			
                        System.out.println("***载货的车有:");			
                        System.out.println(lorry+"	共载货:"+cargo+"吨");			
                        System.out.println("租车总价格:"+price+"元");		
                }		
                return;	
            }
    }


    壹酒酒柒

    这个输入有误就直接跳出程序了啊

    2018-08-22 21:46:45

    共 1 条回复 >

Java入门第二季 升级版

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

530560 学习 · 6091 问题

查看课程

相似问题