最近在跟著慕課網(wǎng)的java教程回顧和復(fù)習(xí)java纲爸,同時也在查漏補缺中對本科階段的知識做一個小型的總結(jié)踩验。
第二季的視頻用了三天看完玛痊,大作業(yè)用了一個上午寫完『潦ぃ看視頻過程中學(xué)習(xí)了eclipse強大的生成(generalize)和改錯功能书斜。
對java的面向?qū)ο筇匦院涂刂婆_IO做了復(fù)習(xí),也新學(xué)了ArrayList的功能酵使,收獲比較大荐吉。
先貼上大作業(yè)要求的輸出。
代碼借鑒了來自以下網(wǎng)頁的代碼
http://www.imooc.com/article/16652%20%20%20Java
http://blog.csdn.net/Joseph_Cherry/article/details/60530978
- 主要思想是:建立一個Car父類口渔,MotorCar是載人汽車子類样屠,Truck是載貨汽車子類,bothCar是皮卡(可載人也可載貨)子類缺脉。用ArrayList存儲各類車的名稱痪欲、租金、容量攻礼。重寫toString方法來解決輸出各類車的信息價格表业踢。
我的代碼:
Car.java
package com.DADACar;
public class Car {
public String name;
public int price;
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getHumanNum() {
// TODO Auto-generated method stub
return 0;
}
public int getGoodsNum() {
// TODO Auto-generated method stub
return 0;
}
}
motorCar.java
package com.DADACar;
public class motorCar extends Car {
private int humanNum;
public int getHumanNum() {
return this.humanNum;
}
public motorCar(String name, int price, int humanNum){
this.name=name;
this.price=price;
this.humanNum=humanNum;
}
public String toString(){
return(name+"\t"+price+"元\\天 "+"載客量:"+humanNum+"人");
}
}
trunckCar.java
package com.DADACar;
public class truckCar extends Car {
private int goodsNum;
public int getGoodsNum() {
return this.goodsNum;
}
public truckCar(String name, int price, int goodsNum) {
this.name=name;
this.price=price;
this.goodsNum=goodsNum;
}
public String toString(){
return(name+"\t"+price+"元\\天 "+"載貨量:"+goodsNum+"噸");
}
}
bothCar.java
package com.DADACar;
public class bothCar extends Car {
private int goodsNum;
private int humanNum;
public int getGoodsNum() {
return this.goodsNum;
}
public int getHumanNum() {
return this.humanNum;
}
public bothCar(String name, int price, int goodsNum, int humanNum){
this.name=name;
this.price=price;
this.goodsNum=goodsNum;
this.humanNum=humanNum;
}
public String toString(){
return(name+"\t"+price+"元\\天\t"+" 載貨量:"+goodsNum+"噸 載人量"+humanNum+"人");
}
}
rentCarSystem.java //主函數(shù)在這里!礁扮!
package com.DADACar;
import java.util.ArrayList;
import java.util.Scanner;
public class rentCarSystem {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用噠噠租車系統(tǒng)");
System.out.println("您是否需要租車知举? 1-是 0-否");
Scanner statusScanner = new Scanner(System.in);
String begin = statusScanner.next();
//statusScanner.close();
while(true){
switch(begin){
case "1":
systemInfoPrint();
break;
case "0":
System.out.println("謝謝使用本系統(tǒng),再見太伊!");
System.exit(0);
break;
default:
System.out.println("輸入的值有誤负蠕!");
}
}
}
public static void systemInfoPrint(){
ArrayList<Car> carlist = new ArrayList<Car>(6);
carlist.add(new motorCar("奧迪A4",500,4));
carlist.add(new motorCar("馬自達6",400,4));
carlist.add(new bothCar("皮卡雪6",450,2,4)); //貨量,人量
carlist.add(new motorCar("金龍",800,20));
carlist.add(new truckCar("松花江",400,4));
carlist.add(new truckCar("依維柯",1000,20));
System.out.println("可供租用的車型信息有:");
System.out.println("******************************************");
System.out.println("序號\t車型 單價 容量");
for(int i = 0;i<carlist.size();i++){
System.out.print((i+1)+"\t");//不換行
System.out.println(carlist.get(i));//換行倦畅,這里不知道行不行//行,get返回值是String绣的,重寫了Car的toString函數(shù)
}
int rentNum = 0;
System.out.println("請輸入租車數(shù)量:");
Scanner rentNumScanner = new Scanner(System.in);
rentNum = rentNumScanner.nextInt();
Scanner carNoSC=new Scanner(System.in);
int [] rentNumList = new int[rentNum];
for(int i=0;i<rentNum;i++){
System.out.println("請輸入第"+(i+1)+"輛車的序號:");
rentNumList[i]=carNoSC.nextInt();
}
System.out.println("請輸入租用天數(shù):");
Scanner rentDaySC = new Scanner(System.in);
int rentDayNum = rentDaySC.nextInt();
int totalMan=0;
int totalGoods=0;
int totalPrice=0;
StringBuilder humanOutput = new StringBuilder();
StringBuilder goodsOutput = new StringBuilder();
for(int i =0;i<rentNumList.length;i++){
if(carlist.get(rentNumList[i]-1) instanceof motorCar){
humanOutput.append(carlist.get(rentNumList[i]-1).getName());
humanOutput.append(" ");
totalMan = totalMan+carlist.get(rentNumList[i]-1).getHumanNum();
totalPrice = totalPrice+rentDayNum*carlist.get(rentNumList[i]-1).getPrice();
}
else if(carlist.get(rentNumList[i]-1) instanceof truckCar){
goodsOutput.append(carlist.get(rentNumList[i]-1).getName());
goodsOutput.append(" ");
totalGoods = totalGoods+carlist.get(rentNumList[i]-1).getGoodsNum();
totalPrice = totalPrice+rentDayNum*carlist.get(rentNumList[i]-1).getPrice();
}
else{
humanOutput.append(carlist.get(rentNumList[i]-1).getName());
humanOutput.append(" ");
goodsOutput.append(carlist.get(rentNumList[i]-1).getName());
goodsOutput.append(" ");
totalPrice = totalPrice+rentDayNum*carlist.get(rentNumList[i]-1).getPrice();
totalMan = totalMan+carlist.get(rentNumList[i]-1).getHumanNum();
totalGoods = totalGoods+carlist.get(rentNumList[i]-1).getGoodsNum();
}
}
System.out.println("您的賬單如下:");
//重頭戲
System.out.println("**可載人的車有:");
System.out.println(humanOutput+"\t共載人:"+totalMan+"人");
System.out.println("**可載貨的車有:");
System.out.println(goodsOutput+"\t共載貨:"+totalGoods+"噸");
System.out.println("租車的總價是:");
System.out.println(totalPrice+"元");
//rentNumScanner.close();
//carNoSC.close();
//rentDaySC.close();
}
}
-
另外值得注意的是
Scanner輸入流如果close了叠赐,就會出現(xiàn)如下所示的NoSuchElementException錯誤欲账。
>Exception in thread "main" [Java](http://lib.csdn.net/base/javase).util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)at java.util.Scanner.next(Scanner.java:1416)