源自:12-1 综合练习
习题,欢迎指出存在的问题
package com.dada;
public abstract class Auto {
public String name;
public int price;
public abstract void showInfo();
}
package com.dada;
public class Car extends Auto{
public int capacity;
public Car(String name,int price,int capacity) {
this.name = name;
this.price = price;
this.capacity = capacity;
}
@Override
public void showInfo() {
// TODO Auto-generated method stub
System.out.println(" "+name+" "+price+"元/天 "+"载人:"+capacity+"人");
}
}
package com.dada;
public class Truck extends Auto{
public int loaded;
public Truck(String name,int price,int loaded) {
// TODO Auto-generated constructor stub
this.name = name;
this.price = price;
this.loaded = loaded;
}
@Override
public void showInfo() {
// TODO Auto-generated method stub
System.out.println(" "+name+" "+price+"元/天 "+"载货:"+loaded+"吨");
}
}
package com.dada;
public class Pickup extends Auto {
public int capacity;
public int loaded;
public Pickup(String name, int price, int capacity, int loaded) {
// TODO Auto-generated constructor stub
this.name = name;
this.price = price;
this.capacity = capacity;
this.loaded = loaded;
}
@Override
public void showInfo() {
// TODO Auto-generated method stub
System.out.println(" "+name+" "+price+"元/天 "+"载人:"+capacity+"人 载货:"+loaded+"吨");
}
}
package com.dada;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Auto[] auto = new Auto[6];
auto[0] = new Car("奥迪A4", 500, 4);
auto[1] = new Car("马自达6", 400, 4);
auto[2] = new Pickup("皮卡雪6", 450, 4, 2);
auto[3] = new Car("金龙 ",800,20);
auto[4] = new Truck("松花江", 400, 4);
auto[5] = new Truck("依维柯", 1000, 20);
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
if(choice == 0) {
System.out.println("感谢使用答答租车系统!");
} else {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
for(int i=0; i<6; i++) {
System.out.print(i+". ");
auto[i].showInfo();
}
System.out.println("请输入您要租汽车的数量:");
int num = input.nextInt();
Auto[] choiceAuto = new Auto[num];
for(int i=0; i<num; i++) {
System.out.println("请输入第"+i+"辆车的序号:");
int id = input.nextInt();
choiceAuto[i] = auto[id];
}
System.out.println("请输入租车天数");
int days = input.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for(int i=0; i<num; i++) {
if(choiceAuto[i] instanceof Car || choiceAuto[i] instanceof Pickup) {
System.out.print(choiceAuto[i].name + " ");
}
}
System.out.println("共载人:"+totalCapacity(choiceAuto, choiceAuto.length)+"人");
System.out.println('\n'+"***可载货的车有:");
for(int i=0; i<num; i++) {
if(choiceAuto[i] instanceof Truck || choiceAuto[i] instanceof Pickup) {
System.out.print(choiceAuto[i].name + " ");
}
}
System.out.println("共载货:"+totalLoaded(choiceAuto, choiceAuto.length)+"吨");
System.out.println("租车的总价格:"+totalPrice(choiceAuto, choiceAuto.length,days));
}
}
private static int totalLoaded(Auto[] auto,int num) {
int sum = 0;
for(int i=0; i<num; i++) {
if(auto[i] instanceof Truck) {
sum = sum + ((Truck)auto[i]).loaded;
}
else if(auto[i] instanceof Pickup) {
sum = sum + ((Pickup)auto[i]).loaded;
}
else {
continue;
}
}
return sum;
}
private static int totalCapacity(Auto[] auto,int num) {
int sum = 0;
for(int i=0; i<num; i++) {
if(auto[i] instanceof Car) {
sum = sum + ((Car)auto[i]).capacity;
}
else if(auto[i] instanceof Pickup) {
sum = sum + ((Pickup)auto[i]).capacity;
}
else {
continue;
}
}
return sum;
}
private static int totalPrice(Auto[] auto,int num, int days) {
int sum = 0;
for(int i=0; i<num; i++) {
sum = sum + auto[i].price;
}
return sum*days;
}
}
提问者:zhengchangtx
2015-03-06 11:45