求指導啊Orz.....我實在是弄不出來了!!!!
我就學了前兩季,實在是不知道要怎么搞了,誰可以告訴我怎么把數據保存然后在用么Orz!!(不管如何我都做不到讓車的類型在賬單中顯示)
顯示車類及價目表我是借鑒了別人的方法的.......主要是我自己弄的那個在容量那接不上.......
求指導Orz

車的父類
package?com.imooc;
public?class?Car?{
//序號
private??int?num;
public?int?getNum()?{
return?num;
}
public?void?setNum(int?num)?{
this.num?=?num;
}
//汽車的類型
private??String?name;
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
//租金
private??double?price;
public?double?getPrice()?{
return?price;
}
public?void?setPrice(double?price)?{
this.price?=?price;
}
}載人汽車類
package?com.imooc;
public?class?Salooncar?extends?Car?implements?ISeat?{
//載人數量(座位)
private?int?seat;
@Override
public?int?getSeat(){
//?TODO?Auto-generated?method?stub
return?seat;
}
public?void?setSeat(int?newSeat)?{
this.seat?=?newSeat;
}
//構造初始化
public?Salooncar(int?newNum,String?newName,double?newPrice?,int?newSeat)?{
this.setNum(newNum);
this.setName(newName);
this.setPrice(newPrice);
this.seat?=newSeat;
}
}皮卡車類
package?com.imooc;
public?class?Pickup?extends?Car?implements?ISeat,IBurden{
//載人數量(座位)
private?int?seat;
@Override
public?int?getSeat()?{
return?seat;
}
public?void?setSeat(int?newSeat)?{
this.seat?=?newSeat;
}
//載貨量
private?double?burden;
@Override
public?double?getBurden()?{
return?burden;
}
public?void?setBurden(double?newSurden)?{
this.burden?=?newSurden;
}
//構造初始化
public?Pickup(int?newNum,String?newName,double?newPrice,int?newSeat,double?newSurden){
this.setNum(newNum);
this.setName(newName);
this.setPrice(newPrice);
this.seat?=?newSeat;
this.burden?=?newSurden;
}
}貨車類
package?com.imooc;
public?class?Wagon?extends?Car?implements?IBurden?{
//載貨量
private?double?burden;
@Override
public?double?getBurden()?{
return?burden;
}
public?void?setBurden(double?newBurden)?{
this.burden?=?newBurden;
}
//構造初始化
public?Wagon(int?newNum,String?newName,double?newPrice,double?newBurden){
this.setNum(newNum);
this.setName(newName);
this.setPrice(newPrice);
this.burden?=?newBurden;
}
}座位(載人數人數)的接口
package?com.imooc;
public?interface?ISeat?{
public?int?getSeat();
}載貨量的接口
package?com.imooc;
public?interface?IBurden?{
public?double?getBurden();
}主函數
package?com.imooc;
import?java.util.Scanner;
import?org.omg.CORBA.PUBLIC_MEMBER;
public?class?Initail?{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
final?int?yes?=?1;
final?int?no?=?0;
//單價
int?sum?=?0;
//總金額
int?sums?=?0;
//單輛車可載人數
int?people?=?0;
//最多可載人數
int?peoples?=?0;
//單輛車可載貨量
int?burden1?=?0;
//最多可載載貨量
int?burden2?=?0;
Car[]?car?=?{new?Salooncar(1,?"奧迪A4",?500,?4),new?Salooncar(2,?"馬自達6",?400,?4),new?Pickup(3,?"皮卡雪6",?450,?4,?2),new?Salooncar(4,?"金龍",?400,?4),new?Wagon(5,?"松花江",?800,?4),new?Wagon(6,?"依維柯",?1000,?20)};
Car?prives?=?new?Car();
//詢問是否進入租車系統
System.out.println("歡迎使用答答租車系統");
System.out.println("請問您是否需要租車:"+"\n"+yes+".是"+"\t"+no+".否");
//創建Scanner對象,對象名為input
Scanner?input?=?new?Scanner(System.in);
int?one?=?input.nextInt();//登錄系統用的
//登錄系統
if(one?==?yes){
System.out.println("下列是出租車的類型及其價目表");
System.out.println("序號"+"\t"+"汽車類型"+"\t"+"\t"+"出租價格"+"\t"+"\t"+"容量");
//顯示出租車的清單列表
for(Car?car2:car){
if(car2?instanceof?Salooncar){
System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載人:"+((Salooncar)car2).getSeat());
}
if(car2?instanceof?Pickup){
System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載人:"+((Pickup)car2).getSeat()+"人"+"\t"+"載貨:"+((Pickup)car2).getBurden()+"噸");
}
if(car2?instanceof?Wagon){
System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載貨:"+((Wagon)car2).getBurden()+"噸");
}
}
//詢問需要汽車的輛數
System.out.println("請問需要組多少輛汽車:");
int?two?=?input.nextInt();//選擇汽車數量用的
for(int?i=1;i<=two;i++){
System.out.println("請輸入第"+i+"輛汽車的序號");
int?three?=?input.nextInt();
//不同類型車的租金,載人量,載貨量(現在只想到這種方法=-=)
if(three?>=1){
int[]?price?=?{500,400,450,400,800,1000};
int[]?seat?=?{4,20};
int[]?burden?=?{2,4,20};
switch(three){
case?1:{?
sum?=?price[0];
people?=?seat[0];
}
break;
case?2:{
sum?=?price[1];
people?=?seat[0];
}
break;
case?3:{?
sum?=?price[2];
people?=?seat[0];
burden1?=?burden[0];
}
break;
case?4:{
sum?=?price[3];
people?=?seat[1];
}
break;
case?5:{?
sum?=?price[4];
burden1?=?burden[1];
}
break;
case?6:{
sum?=?price[5];
burden1?=?burden[2];
}
break;
}
}
sums?=?sums?+?sum?;
peoples?=?peoples?+?people;
burden2?=?burden2?+?burden1;
}
//出租的天數
System.out.println("請輸入租借的天數");
int?four?=?input.nextInt();
int?days?=?four;
//計算總金額,總人數,總載貨量
sums?=?sums*days;
peoples?=?peoples*days;
burden2?=?burden2*days;
System.out.println("*****您的賬單");
System.out.println("載人類汽車:"+"\t"+"最多可載人:"+peoples+"人");
System.out.println("載貨類汽車:"+"\t"+"最多可載貨:"+burden2+"噸");
System.out.println("一共的價格:"+sums);
System.out.println("謝謝惠顧~!");
//退出系統
}else?if(one?==?no){
System.out.println("感謝您的再次使用!再見!");
}else{
System.out.println("輸入信息出錯");
}
}
}
2015-12-25
package?carProgram; import?java.util.ArrayList; import?java.util.Iterator; import?java.util.List; import?java.util.Scanner; ? import?org.omg.CORBA.PUBLIC_MEMBER; public?class?Initail?{ ? ????public?static?void?main(String[]?args)?{ ????????//?TODO?Auto-generated?method?stub ????????final?int?yes?=?1; ????????final?int?no?=?0; ????????//總金額 ????????double?sums?=?0; ????????//最多可載人數 ????????int?peoples?=?0; ????????//最多可載載貨量 ????????double?burden2?=?0; ????????? ????????? ????????? ????????Car[]?car?=?{new?Salooncar(1,?"奧迪A4",?500,?4),new?Salooncar(2,?"馬自達6",?400,?4),new?Pickup(3,?"皮卡雪6",?450,?4,?2),new?Salooncar(4,?"金龍",?400,?4),new?Wagon(5,?"松花江",?800,?4),new?Wagon(6,?"依維柯",?1000,?20)}; ????????List<Car>?list=new?ArrayList<Car>();//保存客戶下單的車輛對象 ????????List<String>?ls_Canmanned=new?ArrayList<String>();//用于放置載人的車輛類型 ????????List<String>?ls_CarryCargo=new?ArrayList<String>();//用于放置載貨車輛的類型 ????????//詢問是否進入租車系統 ????????System.out.println("歡迎使用答答租車系統"); ????????System.out.println("請問您是否需要租車:"+"\n"+yes+".是"+"\t"+no+".否"); ????????? ????????//創建Scanner對象,對象名為input ????????Scanner?input?=?new?Scanner(System.in); ????????int?one?=?input.nextInt();//登錄系統用的 ????????? ????????//登錄系統 ????????if(one?==?yes){ ????????????System.out.println("下列是出租車的類型及其價目表"); ????????????System.out.println("序號"+"\t"+"汽車類型"+"\t"+"\t"+"出租價格"+"\t"+"\t"+"容量"); ????????????? ????????????//顯示出租車的清單列表 ????????????for(Car?car2:car){ ????????????????if(car2?instanceof?Salooncar){ ????????????????????System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載人:"+((Salooncar)car2).getSeat()); ????????????????} ????????????????if(car2?instanceof?Pickup){ ????????????????????System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載人:"+((Pickup)car2).getSeat()+"人"+"\t"+"載貨:"+((Pickup)car2).getBurden()+"噸"); ????????????????} ????????????????if(car2?instanceof?Wagon){ ????????????????????System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載貨:"+((Wagon)car2).getBurden()+"噸"); ????????????????} ????????????} ????????????? ????????????//詢問需要汽車的輛數 ????????????System.out.println("請問需要組多少輛汽車:"); ????????????int?two?=?input.nextInt();//選擇汽車數量用的 ????????????for(int?i=1;i<=two;i++){ ????????????????System.out.println("請輸入第"+i+"輛汽車的序號"); ????????????????int?three?=?input.nextInt(); ????????????????switch(three){ ????????????????case?1:?list.add(car[0])?;?break; ????????????????case?2:?list.add(car[1])?;?break; ????????????????case?3:?list.add(car[2])?;?break; ????????????????case?4:?list.add(car[3])?;?break; ????????????????case?5:?list.add(car[4])?;?break; ????????????????case?6:?list.add(car[5])?;?break; ????????????????} ????????????} ?????????????? ????????????//出租的天數 ????????????System.out.println("請輸入租借的天數"); ????????????????int?four?=?input.nextInt(); ????????????????int?days?=?four; ????????????????? ????????????????//計算總金額,總人數,總載貨量 ?????????????? ???????????????for(int?i=0;i<list.size();i++){???//從保存有客戶下單的車輛list中挨個讀取,判斷屬于那種類型的車輛做相應的處理 ???????????????? Object?car2=?list.get(i); ????????????????????if(car2?instanceof?Salooncar){ ???????????????????? sums=((Car)?car2).getPrice()+sums; ???????????????????? peoples=((Salooncar)?car2).getSeat()+peoples; ???????????????????? ls_Canmanned.add(((Salooncar)?car2).getName()); ???????????????????? continue; ????????????????????} ????????????????????if(car2?instanceof?Pickup){ ???????????????????? sums=((Car)?car2).getPrice()+sums; ???????????????????? peoples=((Pickup)?car2).getSeat()+peoples; ???????????????????? burden2=((Pickup)?car2).getBurden(); ???????????????????? ls_Canmanned.add(((Pickup)?car2).getName()); ???????????????????? ls_CarryCargo.add(((Pickup)?car2).getName()); ???????????????????? continue; ????????????????????} ????????????????????if(car2?instanceof?Wagon){ ???????????????????? sums=((Car)?car2).getPrice()+sums; ???????????????????? burden2=((Wagon)?car2).getBurden()+burden2; ???????????????????? ls_CarryCargo.add(((Wagon)?car2).getName()); ???????????????????? continue; ????????????????????} ????????????????} ????????????????sums?=?sums*days; ????????????????peoples?=?peoples*days; ????????????????burden2?=?burden2*days; ????????????????? ????????????System.out.println("*****您的賬單"); ????????????System.out.print("載人類汽車:"); ????????????for(String?it:ls_Canmanned){???//輸出載客車輛;?未進行重名判斷 ????????????System.out.print(it+"\32"); ????????????} ????????????System.out.println("\t"+"最多可載人:"+peoples+"人"); ????????????System.out.print("載貨類汽車:"); ????????????for(String?it:ls_CarryCargo){?//輸出載貨車輛? ????????????????System.out.print(it+"\32"); ????????????????} ????????????System.out.println("\t"+"最多可載貨:"+burden2+"噸"); ????????????System.out.println("一共的價格:"+sums); ????????????System.out.println("謝謝惠顧~!"); ????????//退出系統 ????????}else?if(one?==?no){ ????????????System.out.println("感謝您的再次使用!再見!"); ????????}else{ ????????????System.out.println("輸入信息出錯"); ????????} ????????input.close(); ????} ? }2015-12-25
//把Initail?這塊換掉就行了
package com.imooc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
?
import org.omg.CORBA.PUBLIC_MEMBER;
public class Initail {
?
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? final int yes = 1;
? ? ? ? final int no = 0;
? ? ? ? //總金額
? ? ? ? double sums = 0;
? ? ? ? //最多可載人數
? ? ? ? int peoples = 0;
? ? ? ? //最多可載載貨量
? ? ? ? double burden2 = 0;
? ? ? ? ?
? ? ? ? ?
? ? ? ? ?
? ? ? ? Car[] car = {new Salooncar(1, "奧迪A4", 500, 4),new Salooncar(2, "馬自達6", 400, 4),new Pickup(3, "皮卡雪6", 450, 4, 2),new Salooncar(4, "金龍", 400, 4),new Wagon(5, "松花江", 800, 4),new Wagon(6, "依維柯", 1000, 20)};
? ? ? ? List<Car> list=new ArrayList<Car>();//保存客戶下單的車輛對象
? ? ? ? List<String> ls_Canmanned=new ArrayList<String>();//用于放置載人的車輛類型
? ? ? ? List<String> ls_CarryCargo=new ArrayList<String>();//用于放置載貨車輛的類型
? ? ? ? //詢問是否進入租車系統
? ? ? ? System.out.println("歡迎使用答答租車系統");
? ? ? ? System.out.println("請問您是否需要租車:"+"\n"+yes+".是"+"\t"+no+".否");
? ? ? ? ?
? ? ? ? //創建Scanner對象,對象名為input
? ? ? ? Scanner input = new Scanner(System.in);
? ? ? ? int one = input.nextInt();//登錄系統用的
? ? ? ? ?
? ? ? ? //登錄系統
? ? ? ? if(one == yes){
? ? ? ? ? ? System.out.println("下列是出租車的類型及其價目表");
? ? ? ? ? ? System.out.println("序號"+"\t"+"汽車類型"+"\t"+"\t"+"出租價格"+"\t"+"\t"+"容量");
? ? ? ? ? ? ?
? ? ? ? ? ? //顯示出租車的清單列表
? ? ? ? ? ? for(Car car2:car){
? ? ? ? ? ? ? ? if(car2 instanceof Salooncar){
? ? ? ? ? ? ? ? ? ? System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載人:"+((Salooncar)car2).getSeat());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(car2 instanceof Pickup){
? ? ? ? ? ? ? ? ? ? System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載人:"+((Pickup)car2).getSeat()+"人"+"\t"+"載貨:"+((Pickup)car2).getBurden()+"噸");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(car2 instanceof Wagon){
? ? ? ? ? ? ? ? ? ? System.out.println(car2.getNum()+"."+"\t"+car2.getName()+"\t"+"\t"+car2.getPrice()+"元/天"+"\t"+"載貨:"+((Wagon)car2).getBurden()+"噸");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? ?
? ? ? ? ? ? //詢問需要汽車的輛數
? ? ? ? ? ? System.out.println("請問需要組多少輛汽車:");
? ? ? ? ? ? int two = input.nextInt();//選擇汽車數量用的
? ? ? ? ? ? for(int i=1;i<=two;i++){
? ? ? ? ? ? ? ? System.out.println("請輸入第"+i+"輛汽車的序號");
? ? ? ? ? ? ? ? int three = input.nextInt();
? ? ? ? ? ? ? ? switch(three){
? ? ? ? ? ? ? ? case 1: list.add(car[0]) ; break;
? ? ? ? ? ? ? ? case 2: list.add(car[1]) ; break;
? ? ? ? ? ? ? ? case 3: list.add(car[2]) ; break;
? ? ? ? ? ? ? ? case 4: list.add(car[3]) ; break;
? ? ? ? ? ? ? ? case 5: list.add(car[4]) ; break;
? ? ? ? ? ? ? ? case 6: list.add(car[5]) ; break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? ??
? ? ? ? ? ? //出租的天數
? ? ? ? ? ? System.out.println("請輸入租借的天數");
? ? ? ? ? ? ? ? int four = input.nextInt();
? ? ? ? ? ? ? ? int days = four;
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? //計算總金額,總人數,總載貨量
? ? ? ? ? ? ??
? ? ? ? ? ? ? ?for(int i=0;i<list.size();i++){ ? //從保存有客戶下單的車輛list中挨個讀取,判斷屬于那種類型的車輛做相應的處理
? ? ? ? ? ? ? ? Object car2= list.get(i);
? ? ? ? ? ? ? ? ? ? if(car2 instanceof Salooncar){
? ? ? ? ? ? ? ? ? ? sums=((Car) car2).getPrice()+sums;
? ? ? ? ? ? ? ? ? ? peoples=((Salooncar) car2).getSeat()+peoples;
? ? ? ? ? ? ? ? ? ? ls_Canmanned.add(((Salooncar) car2).getName());
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(car2 instanceof Pickup){
? ? ? ? ? ? ? ? ? ? sums=((Car) car2).getPrice()+sums;
? ? ? ? ? ? ? ? ? ? peoples=((Pickup) car2).getSeat()+peoples;
? ? ? ? ? ? ? ? ? ? burden2=((Pickup) car2).getBurden();
? ? ? ? ? ? ? ? ? ? ls_Canmanned.add(((Pickup) car2).getName());
? ? ? ? ? ? ? ? ? ? ls_CarryCargo.add(((Pickup) car2).getName());
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(car2 instanceof Wagon){
? ? ? ? ? ? ? ? ? ? sums=((Car) car2).getPrice()+sums;
? ? ? ? ? ? ? ? ? ? burden2=((Wagon) car2).getBurden()+burden2;
? ? ? ? ? ? ? ? ? ? ls_CarryCargo.add(((Wagon) car2).getName());
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? sums = sums*days;
? ? ? ? ? ? ? ? peoples = peoples*days;
? ? ? ? ? ? ? ? burden2 = burden2*days;
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? System.out.println("*****您的賬單");
? ? ? ? ? ? System.out.print("載人類汽車:");
? ? ? ? ? ? for(String it:ls_Canmanned){ ? //輸出載客車輛; 未進行重名判斷
? ? ? ? ? ? System.out.print(it+"\32");
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("\t"+"最多可載人:"+peoples+"人");
? ? ? ? ? ? System.out.print("載貨類汽車:");
? ? ? ? ? ? for(String it:ls_CarryCargo){ //輸出載貨車輛?
? ? ? ? ? ? ? ? System.out.print(it+"\32");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("\t"+"最多可載貨:"+burden2+"噸");
? ? ? ? ? ? System.out.println("一共的價格:"+sums);
? ? ? ? ? ? System.out.println("謝謝惠顧~!");
? ? ? ? //退出系統
? ? ? ? }else if(one == no){
? ? ? ? ? ? System.out.println("感謝您的再次使用!再見!");
? ? ? ? }else{
? ? ? ? ? ? System.out.println("輸入信息出錯");
? ? ? ? }
? ? ? ? input.close();
? ? }
?
}