jason_zhai
2014-12-28 17:16
package com.jason_zhai; /** * @author Jason * Define Auto Class and methods. */ public abstract class Auto { //定义父类Auto public String name; //define name public float price; //define price public float cargoLoad; public float peopleLoad; public Auto(String name, float price, float cargoLoad, float peopleLoad) { //定义父类构造方法 this.name = name; this.price = price; this.cargoLoad = cargoLoad; this.peopleLoad = peopleLoad; } } package com.jason_zhai; public class Truck extends Auto { //定义Truck子类 public Truck(String name, float price, float cargoLoad, float peopleLoad) { super(name, price, cargoLoad, peopleLoad); } } package com.jason_zhai; public class Sedan extends Auto { //定义轿车子类 public Sedan(String name, float price, float cargoLoad, float peopleLoad) { super(name, price, cargoLoad, peopleLoad); } } package com.jason_zhai; public class Pickup extends Auto { //定义皮卡子类 public Pickup(String name, float price, float cargoLoad, float peopleLoad) { super(name, price, cargoLoad, peopleLoad); } } package com.jason_zhai; import java.util.Scanner; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input1 = new Scanner(System.in); System.out.print("Do you want to rent a car? 1--yes, 2--no"); int userChoice1 = input1.nextInt(); if(userChoice1 == 1) { System.out.println("you choose to rent a car!"); System.out.println("we have below choices for you"); Truck truck1 = new Truck("changan", 200, 5, 0); Truck truck2 = new Truck("volvo", 400, 20, 0); Sedan sedan1 = new Sedan("audi", 500, 0, 4); Sedan sedan2 = new Sedan("mazda", 300, 0, 10); Pickup pickup1 = new Pickup("dodge", 400, 4, 10); System.out.println("1. " + truck1.name + " price:" + truck1.price); System.out.println("2. " + truck2.name + " price:" + truck2.price); System.out.println("3. " + sedan1.name + " price:" + sedan1.price); System.out.println("4. " + sedan2.name + " price:" + sedan2.price); System.out.println("5. " + pickup1.name + " price:" + pickup1.price); System.out.println("please enter how many cars you want to rent?"); Scanner input2 = new Scanner(System.in); int userChoice2 = input2.nextInt(); float sumCargo = 0; float sumPeople = 0; float sumPrice = 0; for (int i = 1; i <= userChoice2; i++) { System.out.println("please choose your " + i + " vehicle!"); Scanner input3 = new Scanner(System.in); int userChoice3 = input3.nextInt(); switch (userChoice3) { case 1: System.out.println("You chose " + truck1.name + " with price " + truck1.price + " per day loading " + truck1.cargoLoad + "T cargo and " + truck1.peopleLoad + " people!"); sumCargo = truck1.cargoLoad + sumCargo; sumPeople = truck1.peopleLoad + sumPeople; sumPrice = truck1.price + sumPrice; System.out.println("your total cargo load is " + sumCargo); System.out .println("your total people load is " + sumPeople); System.out.println("your total price is " + sumPrice); System.out.println(); break; case 2: System.out.println("You chose " + truck2.name + " with price " + truck2.price + " per day loading " + truck2.cargoLoad + "T cargo and " + truck2.peopleLoad + " people!"); sumCargo = truck2.cargoLoad + sumCargo; sumPeople = truck2.peopleLoad + sumPeople; sumPrice = truck2.price + sumPrice; System.out.println("your total cargo load is " + sumCargo); System.out .println("your total people load is " + sumPeople); System.out.println("your total price is " + sumPrice); System.out.println(); break; case 3: System.out.println("You chose " + sedan1.name + " with price " + sedan1.price + " per day loading " + sedan1.cargoLoad + "T cargo and " + sedan1.peopleLoad + " people!"); sumCargo = sedan1.cargoLoad + sumCargo; sumPeople = sedan1.peopleLoad + sumPeople; sumPrice = sedan1.price + sumPrice; System.out.println("your total cargo load is " + sumCargo); System.out .println("your total people load is " + sumPeople); System.out.println("your total price is " + sumPrice); System.out.println(); break; case 4: System.out.println("You chose " + sedan2.name + " with price " + sedan2.price + " per day loading " + sedan2.cargoLoad + "T cargo and " + sedan2.peopleLoad + " people!"); sumCargo = sedan2.cargoLoad + sumCargo; sumPeople = sedan2.peopleLoad + sumPeople; sumPrice = sedan2.price + sumPrice; System.out.println("your total cargo load is " + sumCargo); System.out .println("your total people load is " + sumPeople); System.out.println("your total price is " + sumPrice); System.out.println(); break; case 5: System.out.println("You chose " + pickup1.name + " with price " + pickup1.price + " per day loading " + pickup1.cargoLoad + "T cargo and " + pickup1.peopleLoad + " people!"); sumCargo = pickup1.cargoLoad + sumCargo; sumPeople = pickup1.peopleLoad + sumPeople; sumPrice = pickup1.price + sumPrice; System.out.println("your total cargo load is " + sumCargo); System.out .println("your total people load is " + sumPeople); System.out.println("your total price is " + sumPrice); System.out.println(); break; } } System.out.println("please enter how many days you want to rent?"); Scanner input4 = new Scanner(System.in); int userChoice4 = input4.nextInt(); sumPrice = sumPrice * userChoice4; System.out.println("Summarizaiton of your bill is:"); System.out.println("Number of vehicles: " + userChoice2); System.out.println("Days of rentle: " + userChoice4); System.out.println("Total price is: " + sumPrice); }else if (userChoice1 == 2) { System.out.println("you choose not to rent a car!"); System.exit(0); }else { System.out.println("bad input, please re-enter your choice!"); } } }
不知道为什么,自己在建立了一个父类并定义了一个构造方法同时建立三个子类后就感觉类里面没什么写得了。其余的功能都在主函数里面实现了。 总感觉这样不算是面向对象的感觉,有没有大神指导一下是思路哪里出了问题。 应该怎么优化?
你可以看一下本章节置顶的帖子
Java入门第二季 升级版
530656 学习 · 6091 问题
相似问题