七毛萨玛
2015-08-05 17:52
//汽车类 package com.oop; public class Auto { private int id; private String name; private int perRent; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPerRent() { return perRent; } public void setPerRent(int perRent) { this.perRent = perRent; } public Auto(int id, String name, int perRent) { this.id = id; this.name = name; this.perRent = perRent; } public Auto() { } }
//小汽车客车类
package com.oop;
public class Car extends Auto {
private int manned;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public Car() {
super();
}
public Car(int id, String name, int perRent,int manned) {
super(id, name, perRent);
this.manned=manned;
}
}
//卡车类
package com.oop;
public class Truck extends Auto {
private int load;
public int getLoad() {
return load;
}
public void setLoad(int load) {
this.load = load;
}
public Truck() {
super();
// TODO 自动生成的构造函数存根
}
public Truck(int id, String name, int perRent,int load) {
super(id, name, perRent);
this.load=load;
// TODO 自动生成的构造函数存根
}
}
//皮卡类
package com.oop;
public class Packup extends Auto {
private int manned;
private int load;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public int getLoad() {
return load;
}
public void setLoad(int load) {
this.load = load;
}
public Packup() {
super();
// TODO 自动生成的构造函数存根
}
public Packup(int id, String name, int perRent,int manned,int load) {
super(id, name, perRent);
this.manned=manned;
this.load=load;
// TODO 自动生成的构造函数存根
}
}
//汽车库类
package com.oop;
public class AutoDemo {
Auto []auto=new Auto[10];
public AutoDemo(){
Auto auto1=new Car(1,"奥迪A4", 500, 4);
Auto auto2=new Car(2,"马自达6",400,4);
Auto auto3=new Packup(3,"皮卡雪6",450,4,2);
Auto auto4=new Car(4,"金龙", 800, 20);
Auto auto5=new Truck(5,"松花江",400,4);
Auto auto6=new Truck(6,"依维柯", 1000, 20);
auto[0]=auto1;
auto[1]=auto2;
auto[2]=auto3;
auto[3]=auto4;
auto[4]=auto5;
auto[5]=auto6;
}
public void show(){
for(int i=0;i<auto.length;i++){
if(auto[i]instanceof Car)
System.out.println(auto[i].getId()+"\t"+auto[i].getName()+"\t"+auto[i].getPerRent()+"\t"+"载人:"+((Car) auto[i]).getManned());
else if(auto[i]instanceof Truck)
System.out.println(auto[i].getId()+"\t"+auto[i].getName()+"\t"+auto[i].getPerRent()+"\t"+"载货"+((Truck) auto[i]).getLoad());
else if(auto[i]instanceof Packup)
System.out.println(auto[i].getId()+"\t"+auto[i].getName()+"\t"+auto[i].getPerRent()+"\t"+"载人:"+((Packup) auto[i]).getManned()+"载货"+((Packup) auto[i]).getLoad());
continue;
}
}
}
//计算类
package com.oop;
import java.util.Scanner;
public class Count {
int number;
int autoid1;
int autoid2;
int day;
public void Flow(){
System.out.println("请输入要租汽车的数量");
Scanner input=new Scanner(System.in);
number=input.nextInt();
System.out.println("请输入第一辆车的序号");
autoid1=input.nextInt();
System.out.println("请输入第二辆车的序号");
autoid2=input.nextInt();
System.out.println("请输入租车天数");
day=input.nextInt();
}
public void show(){
AutoDemo a=new AutoDemo();
int rent=(a.auto[autoid1-1].getPerRent()+a.auto[autoid2-1].getPerRent())*day;
System.out.println("您的账单:");
System.out.println("可载人的有");
if(a.auto[autoid1-1] instanceof Car||a.auto[autoid1-1] instanceof Packup){
System.out.println(a.auto[autoid1-1].getName());
}
if(a.auto[autoid2-1] instanceof Car||a.auto[autoid2-1] instanceof Packup){
System.out.println(a.auto[autoid2-1].getName());
}else{
System.out.println("无");
}
System.out.println();
System.out.println("载货的有");
if(a.auto[autoid1-1] instanceof Truck||a.auto[autoid1-1] instanceof Packup){
System.out.println(a.auto[autoid1-1].getName());
}
if(a.auto[autoid2-1] instanceof Truck||a.auto[autoid2-1] instanceof Packup){
System.out.println(a.auto[autoid2-1].getName());
}else{
System.out.println("无");
}
System.out.println("总价为"+rent);
}
}
//测试类
package com.oop;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("欢迎使用哒哒租车系统");
Scanner input=new Scanner(System.in);
System.out.println("您是否要租车1.是 2.否");
if(input.nextInt()==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
AutoDemo ad=new AutoDemo();
ad.show();
Count ct=new Count();
ct.Flow();
ct.show();
}
else{
System.out.println("谢谢使用");
}
}
}
public class Car { private int carNumber; private String carName; private int carRentMoney; public int getCarNumber() { return carNumber; } public void setCarNumber(int carNumber) { this.carNumber = carNumber; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarRentMoney() { return carRentMoney; } public void setCarRentMoney(int carRentMoney) { this.carRentMoney = carRentMoney; } } public class passengerCar extends Car { private int CarCapacity; public passengerCar(int carNumber,String carName,int carRentMoney,int carCapacity){ this.setCarNumber(carNumber); this.setCarName(carName); this.setCarRentMoney(carRentMoney); CarCapacity = carCapacity; } public int getCarCapacity() { return CarCapacity; } public void setCarCapacity(int carCapacity) { CarCapacity = carCapacity; } }
public class pickUp extends Car { private int CarCapacity; private int CarCarry; public pickUp(int carNumber,String carName,int carRentMoney,int CarCapacity,int CarCarry){ this.setCarNumber(carNumber); this.setCarName(carName); this.setCarRentMoney(carRentMoney); this.CarCapacity = CarCapacity; this.CarCarry = CarCarry; } public int getCarCapacity() { return CarCapacity; } public void setCarCapacity(int carCapacity) { CarCapacity = carCapacity; } public int getCarCarry() { return CarCarry; } public void setCarCarry(int carCarry) { CarCarry = carCarry; } }
public class trunkCar extends Car { private int CarCarry; public trunkCar(int carNumber,String carName,int carRentMoney,int CarCarry){ this.setCarNumber(carNumber); this.setCarName(carName); this.setCarRentMoney(carRentMoney); this.CarCarry = CarCarry; } public int getCarCarry() { return CarCarry; } public void setCarCarry(int carCarry) { CarCarry = carCarry; } }
import java.util.Scanner; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("欢迎使用答答租车系统:"); System.out.println("你是否要租车? 1是 2否"); Scanner s = new Scanner(System.in); int k = s.nextInt(); if(k == 1){ System.out.println("您可租车的类型及其价格表:"); Car RentCar[] = {new passengerCar(1,"奥迪A4",500,4),new passengerCar(2,"马自达6",400,4),new pickUp(3,"皮卡雪6",450,4,2),new passengerCar(4,"金龙",800,20),new trunkCar(5,"松花江",400,4),new trunkCar(6,"依维柯",1000,20)}; System.out.println("序号"+"\t"+"汽车名称"+"\t"+"\t"+"租金"+"\t"+"容量"); for(int i = 0; i < 6; i++){ ////若RentCar[i]它是客车类型的实例,则打印出客车应该有的属性值,这里注意强转,否则不会出现 if(RentCar[i] instanceof passengerCar){ System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"载人:"+((passengerCar)RentCar[i]).getCarCapacity()+"人");//将Car类型强制转换成passengerCar类,得到passegerCard的属性 } if(RentCar[i] instanceof pickUp){ System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"载人:"+((pickUp)RentCar[i]).getCarCapacity()+"人 载货:"+((pickUp)RentCar[i]).getCarCarry()+"吨"); } if(RentCar[i] instanceof trunkCar){ System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"载人:"+((trunkCar)RentCar[i]).getCarCarry()+"吨"); } } System.out.println("请输入您要租汽车的数量:"); //已经有Scanner对象,直接调用 int totalNum = s.nextInt(); int sum = 0; for(int j = 1; j <= totalNum; j++){ System.out.println("请输入第"+j+"辆车的序号"); int q = s.nextInt(); switch(q){ case 1: RentCar[0].getCarRentMoney(); case 2: RentCar[1].getCarRentMoney(); case 3: RentCar[2].getCarRentMoney(); case 4: RentCar[3].getCarRentMoney(); case 5: RentCar[4].getCarRentMoney(); case 6: RentCar[5].getCarRentMoney(); } sum = sum + RentCar[q-1].getCarRentMoney(); } System.out.println("请输入租车的天数:"); int day = s.nextInt(); System.out.println("您的账单:"); System.out.println(sum * day); } } }
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题