HelloAI
2015-03-28 13:28
package com.edu.ncu; import java.util.*; public abstract class Car { private String name; private int size; private int num; private double price; //Creat Car object, name, num, price public Car(String name, int size, int num, double price){ this.name = name; this.size = size; this.num = num; this.price = price; } public abstract void print(); public String getName(){ return name; } public int getSize(){ return size; } public int getNum(){ return num; } public double getPrice(){ return price; } }
public class taxi extends Car {
public taxi(String name, int size, int num, double price){
super(name,size, num, price);
}
public void print(){
System.out.println(getName() +"\t\t"+getSize() +"\t" +getNum() +"\t" +getPrice());
}
}
public class Bus extends Car {
public Bus(String name, int size, int num, double price){
super(name,size, num, price);
}
public void print(){
System.out.println(getName() + "\t\t" +getSize() +"\t" +getNum() +"\t" +getPrice());
}
}
public abstract class Truck {
private String name;
private int size;
private int num;
private double price;
public Truck(String name, int size, int num, double price){
this.name = name;
this.size = size;
this.num = num;
this.price = price;
}
public abstract void print();
public String getName(){
return name;
}
public int getSize(){
return size;
}
public int getNum(){
return num;
}
public double getPrice(){
return price;
}
}
public class BabyTruck extends Truck {
public BabyTruck(String name, int size, int num, double price){
super(name, size, num,price);
}
public void print(){
System.out.println(getName() +"\t" +getSize() +"\t" +getNum() +"\t" +getPrice());
}
}
public class BigTruck extends Truck {
public BigTruck(String name, int size, int num, double price){
super(name, size, num, price);
}
public void print(){
System.out.println(getName() + "\t" +getSize() +"\t" +getNum() +"\t" +getPrice());
}
}
public class Pickup {
private String name;
private int size;
private int num;
private double price;
public Pickup(String name, int size, int num, double price){
this.name = name;
this.size = size;
this.num = num;
this.price = price;
}
public double getPrice(){
return price;
}
public void print(){
System.out.println(name +"\t" +size +"\t" +num +"\t" +price);
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Do you want to come in The Car?(1 == yes/ 2 == no)");
int num1 = input.nextInt();
if(num1 == 1){
System.out.println("======================");
System.out.println("=Welcome to our Car !=");
System.out.println("======================");
System.out.println("Type" +"\t\t" +"Weight" +"\t" +"Person" +"\t" +"Price");
System.out.println("============= ======= ====== ======");
Car taxi = new taxi("1:Car", 0, 5, 12);
Car bus = new Bus("2:Bus", 0, 20, 50);
taxi.print();
bus.print();
Truck babyTruck = new BabyTruck("3:BabyTruck", 12, 0, 65);
Truck bigTruck = new BigTruck("4:BigTruck", 30, 0, 120);
babyTruck.print();
bigTruck.print();
Pickup pickup = new Pickup("5:Pickup", 22, 10, 60);
pickup.print();
System.out.println("\n\n\nPlease choose the size of Vehicle : ");
int num2 = input.nextInt();
switch(num2){
case 1:{
double sum1 = 0;
System.out.println("How many you need to?");
int num3 = input.nextInt();
System.out.println("Please payoff the fare :");
sum1 = fareOfSum(num3, taxi.getPrice());
break;
}
case 2:{
double sum2 = 0;
System.out.println("How many you need to?");
int num3 = input.nextInt();
System.out.println("Please payoff the fare :");
sum2 = fareOfSum(num3, bus.getPrice());
break;
}
case 3:{
double sum3 = 0;
System.out.println("How many you need to?");
int num3 = input.nextInt();
System.out.println("Please payoff the fare :");
sum3 = fareOfSum(num3, babyTruck.getPrice());
}
case 4:{
double sum4 = 0;
System.out.println("How many you need to?");
int num3 = input.nextInt();
System.out.println("Please payoff the fare :");
sum4 = fareOfSum(num3, bigTruck.getPrice());
break;
}
case 5:{
double sum5 = 0;
System.out.println("How many you need to?");
int num3 = input.nextInt();
System.out.println("Please payoff the fare :");
sum5 = fareOfSum(num3, pickup.getPrice());
break;
}
default:
break;
}
}
}
private static double fareOfSum(int num, double price) {
// TODO Auto-generated method stub
double sum = 0;
sum = num * price;
System.out.println("sum = " +sum);
return sum;
}
}
0.0
Java入门第二季 升级版
530653 学习 · 6091 问题
相似问题