java关于泛型的实验代码_[ Java学习 ] 泛型类的应用实验
成文原因:
這篇文章是我這周所做的 Java 實驗題的一個小總結(jié)。
這次實驗讓我深刻贊同了我們 Java 老師在這節(jié)實驗課前告訴我們的話:
最重要的是把問題想明白,它要怎么拆分成幾個類,每個類里究竟需要哪些方法,具體細(xì)節(jié)怎么實現(xiàn)。當(dāng)你真正把這些問題想清楚了以后,剩下的編程,其實就是很簡單的事情了。難度往往不是難在編程,而是分解問題為一個個小問題后,逐一解決它們的能力。
做完實驗以后再來看老師這句話,覺得不能同意更多。
我貼上來的代碼有兩份。
前一份是未完成的代碼,之所以未完成,是因為:寫到一半時,覺得這樣的結(jié)構(gòu)設(shè)計太不合理了,如果照著這么寫下去,會有許多重復(fù)的代碼片段不斷地出現(xiàn),于是就想著這么去改進(jìn)…怎樣才能盡可能減少方法,但是提高代碼的復(fù)用性呢?
于是,果斷放棄了前一種沒寫完的,不太優(yōu)的結(jié)構(gòu),第二份代碼才是寫完時真正提交的最終版本。
除了代碼,我把實驗報告的心得部分也一并貼了上來用以自警
-------------------------------下面是實驗題目------------------------------
-------------------------------下面是代碼------------------------------
/*
前一份是未完成的代碼,之所以未完成,是因為:寫到一半時,覺得這樣的結(jié)構(gòu)設(shè)計太不合理了,如果照著這么寫下去,會有許多重復(fù)的代碼片段不斷地出現(xiàn),于是就想著這么去改進(jìn)…怎樣才能盡可能減少方法,但是提高代碼的復(fù)用性呢?
于是,果斷放棄了前一種沒寫完的,不太優(yōu)的結(jié)構(gòu),第二份代碼才是寫完時真正提交的最終版本。
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.*;
class Customer
{
private String name;
private String fNum; // flight number,表示他所定的航線的航線號
private int sum; //買了多少票
Customer()
{
}
Customer(String n, String f, int s)
{
name = n;
fNum = f;
sum = s;
}
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setSum(int s)
{
sum = s;
}
public int getSum()
{
return sum;
}
public void setfNum(String f)
{
fNum = f;
}
String getfNum()
{
return fNum;
}
}
class Flight
{
private static int sum = 0; //航線計數(shù)
private int order; //航線排列號
private String destination;//終點(diǎn)站名
private String FNum; //航班號
private int limit; //乘客限額
private int free; //余票數(shù)量
private LinkedList booked; //已預(yù)定客戶列表
private LinkedList waiting; //等候替補(bǔ)客戶名單
Flight()
{
}
Flight(int o, String d, String F, int l)
{
order = o;
destination = d;
FNum = F;
limit = l;
}
public int getOrder()
{
return order;
}
public String getDestination()
{
return destination;
}
public String getFNum()
{
return FNum;
}
public int getLimit()
{
return limit;
}
public int getFree()
{
return free;
}
public void addFree(int f)
{
free += f;
}
public void subFree(int f)
{
free -= f;
}
public LinkedList getBooked()
{
return booked;
}
public LinkedList getWaiting()
{
return waiting;
}
public void showInfo() //show information
{
System.out.println("航線排列號為:" + order + " 終點(diǎn)站為:" + destination);
System.out.println("航班號為:" + FNum + " 成員定額為: " + limit + " 剩余票數(shù)為: " + free);
System.out.println("已訂票客戶有:");
Iterator it = booked.iterator();
for (; it.hasNext(); )
{
System.out.println(it.next().getName() + " ");
}
System.out.println();
System.out.println("等候替補(bǔ)客戶有:");
for ( it = waiting.iterator(); it.hasNext(); )
{
System.out.println(it.next().getName() + " ");
}
System.out.println();
}
}
class Menu
{
private ArrayList flights;
Flight temp;
public void init()
{
Flight f = new Flight(1, "北京","K2", 20);
flights.add(f);
f = new Flight(2, "北京","K3", 23);
flights.add(f);
f = new Flight(3, "成都","K4", 25);
flights.add(f);
}
public void addFlight(Flight flight) //加入一條航線
{
flights.add(flight);
}
public void query() //查詢
{
Scanner in = new Scanner(System.in);
int order = in.nextInt(); //次序
Flight temp;
Iterator it = flights.iterator();
for ( ; it.hasNext(); )
{
temp = it.next();
if (temp.getOrder() == order)
{
temp.showInfo();
return;
}
}
System.out.println("沒有檢索到相關(guān)信息");
}
public void book() //訂票
{
System.out.println("請輸入航班號,您的姓名,以及您想要訂購的票數(shù)");
Scanner in = new Scanner(System.in);
Flight temp;
String tp, tp1; //tp 為航班號,tp1為姓名
int tem; // temporary
tp = in.next();
tp1 = in.nextLine();
tem = in.nextInt();
Customer c = new Customer(tp1, tp, tem); //構(gòu)造函數(shù)參數(shù)依次為:姓名、 航線號、 所購票數(shù)
Iterator it = flights.iterator();
Flight temp;
for ( ; it.hasNext(); )
{
temp = it.next();
if (temp.getFNum() == tp)
{
if (temp.getFree() >= tem)
{
temp.subFree(tem);
temp.getBooked().add(c);
System.out.println("購票成功");
}
else
{
temp.getWaiting().add(c);
System.out.println("抱歉,票數(shù)暫時不夠!");
}
return;
}
}
System.out.println("輸入的航線不存在!");
}
public void cancel() //退票
{
System.out.println("請輸入航班號,您的姓名,以及您想要退票的票數(shù)");
Scanner in = new Scanner(System.in);
Flight temp;
String tp, tp1; //tp 為航班號,tp1為姓名
int tem; // temporary
tp = in.next();
tp1 = in.nextLine();
tem = in.nextInt();
Iterator it = flights.iterator();
Flight temp;
for ( ; it.hasNext(); )
{
temp = it.next();
if (temp.getFNum() == tp) //確認(rèn)航班號確實存在
{
for ( Iterator it2= temp.getBooked().iterator(); it2.hasNext(); )
{
Customer now = it2.next();
if (now.getName() == tp1 && now.getfNum() == tp) //確認(rèn)該顧客確實定了該航班的機(jī)票
{
temp.addFree(tem);
System.out.println("退票成功");
if (now.getSum() == tem) //如果顧客將自己買的票全部退票,則從買票列表刪除該顧客
temp.getBooked().remove(now);
}
}
for ( Iterator it2= temp.getWaiting().iterator(); it2.hasNext(); ) //遍歷該航班的waiting列表,看是否有滿足條件的購票請求可以滿足
{
Customer now = it2.next();
if (now.getSum() <= temp.getFree())
}
}
}
System.out.println("輸入信息有誤");
}
public void menu() //顯示菜單
{
System.out.println("----------");
System.out.println("1.航線查詢");
System.out.println("2.辦理退票");
System.out.println("3.辦理退票");
System.out.println("4.退出系統(tǒng)");
System.out.println("----------");
Scanner in = new Scanner(System.in);
int order = in.nextInt(); //指令
switch(order)
{
case 1: query(); break;
case 2: book(); break;
case 3: cancel(); break;
case 4: System.exit(0); in.close();break;
default: System.out.println("輸入錯誤,請 重新運(yùn)行訂票系統(tǒng)!");
}
menu();
}
}
public class test
{
public static void main(String args[] )
{
Menu m = new Menu();
m.menu();
}
}
//這份才是真正完整的,也是我提交的最終版本
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.*;
class Customer
{
private String name;
private String fNum; // flight number,表示他所定的航線的航線號
private int sum; //買了多少票
Customer()
{
}
Customer(String n, String f, int s)
{
name = n;
fNum = f;
sum = s;
}
public String getName()
{
return name;
}
public int getSum()
{
return sum;
}
public String getfNum()
{
return fNum;
}
public void setSum(int s)
{
sum = s;
}
}
class Flight
{
private static int sum = 0; //航線計數(shù)
private int order; //航線排列號
private String destination;//終點(diǎn)站名
private String FNum; //航班號
private int limit; //乘客限額
private int free; //余票數(shù)量
private LinkedList booked; //已預(yù)定客戶列表
private LinkedList waiting; //等候替補(bǔ)客戶名單
Flight()
{
}
Flight(int o, String d, String F, int l)
{
order = o;
destination = d;
FNum = F;
limit = free = l;
booked = new LinkedList();
waiting = new LinkedList();
}
public String getDestination()
{
return destination;
}
public String getFNum()
{
return FNum;
}
public void showInfo() //show information
{
System.out.println("航線排列號為:" + order + " 終點(diǎn)站為:" + destination);
System.out.println("航班號為:" + FNum + " 成員定額為: " + limit + " 剩余票數(shù)為: " + free);
System.out.println();
System.out.println("已訂票客戶有:");
Iterator it = booked.iterator();
for (; it.hasNext(); )
{
System.out.println(it.next().getName() + " ");
}
System.out.println();
System.out.println("等候替補(bǔ)客戶有:");
for ( it = waiting.iterator(); it.hasNext(); )
{
System.out.println(it.next().getName() + " ");
}
System.out.println();
}
public void isbook(Customer c)
{
if (c.getSum() > free) //檢驗是否有足夠余票可供該顧客訂票
{
System.out.println("抱歉,票數(shù)暫時不足!");
waiting.add(c);
return;
}
book(c); //先判斷可不可以訂票,確認(rèn)可以再訂票
}
public void book(Customer c) //這個函數(shù)可重用,在退票以后,遍歷 waiting 鏈表之前
{
for (Iterator it = booked.iterator(); it.hasNext(); ) //判斷該顧客之前是否有訂過該航班的票
{
Customer now = it.next();
if (now.getName().equals(c.getName()))
{
now.setSum(now.getSum() + c.getSum());
free -= c.getSum();
System.out.println("顧客" + c.getName() + "訂票成功");
System.out.println("一共成功訂票" + now.getSum() + "張,航班號為" + FNum );
return;
}
}
booked.add(c);
free -= c.getSum();
System.out.println("顧客" + c.getName() + "訂票成功");
System.out.println("一共成功訂票" + c.getSum() + "張,航班號為" + FNum );
}
public void cancel(Customer c)
{
Iterator it = booked.iterator();
boolean found = false;
for ( ; it.hasNext(); )
{
Customer now = it.next();
if (now.getName().equals(c.getName()))
{
found = true;
free += c.getSum();
System.out.println("退票成功");
if (now.getSum() == c.getSum())//如果顧客將自己買的票全部退票,則從買票列表刪除該顧客
{
booked.remove(now);
break;
}
}
}
if (!found) System.out.println("退票失敗,您沒有定該航班的機(jī)票!");
System.out.println();
it = waiting.iterator();
for ( ; it.hasNext(); )
{
Customer now = it.next();
if (now.getfNum().equals(FNum) && now.getSum() <= free)
book(now);
}
}
}
class Menu
{
private ArrayList flights;
Flight temp;
public void init()
{
flights = new ArrayList();
Flight f = new Flight(1, "北京","k2", 20);
addFlight(f);
f = new Flight(2, "北京","k3", 23);
addFlight(f);
f = new Flight(3, "成都","k4", 25);
addFlight(f);
}
public void addFlight(Flight flight) //加入一條航線
{
flights.add(flight);
}
public void query() //查詢
{
System.out.println("請輸入要查詢的目的地城市:");
Scanner in = new Scanner(System.in);
String d = in.next();
Flight temp;
Iterator it = flights.iterator();
boolean found = false;
for ( ; it.hasNext(); )
{
temp = it.next();
if (temp.getDestination().equals(d))
{
found = true;
temp.showInfo();
}
}
if (!found)System.out.println("沒有檢索到相關(guān)信息");
}
public void book() //訂票
{
Scanner in = new Scanner(System.in);
System.out.println("請輸入訂票人數(shù):");
int t = in.nextInt();
Flight temp; // 航班類臨時變量
String tp, tp1; //tp 為航班號,tp1為姓名
int tem; // temporary
for (int i = 0; i < t; i++)
{
System.out.println("請輸入航班號,您的姓名,以及您想要訂購的票數(shù)");
tp = in.next();
tp1 = in.next();
tem = in.nextInt();
Customer c = new Customer(tp1, tp, tem); //構(gòu)造函數(shù)參數(shù)依次為:姓名、 航線號、 所購票數(shù)
Iterator it = flights.iterator();
boolean found = false;
for ( ; it.hasNext(); )
{
temp = it.next();
if (temp.getFNum().equals(tp))
{
temp.isbook(c);
found = true;
break;
}
}
if (!found) System.out.println("輸入的航線不存在!");
System.out.println();
}
}
public void cancel() //退票
{
Scanner in = new Scanner(System.in);
System.out.println("請輸入退票人數(shù):");
int t = in.nextInt();
Flight temp; // 航班類臨時變量
String tp, tp1; //tp 為航班號,tp1為姓名
int tem; // temporary
for (int i = 0; i < t; i++)
{
System.out.println("請輸入航班號,您的姓名,以及您想要退票的票數(shù)");
tp = in.next();
tp1 = in.next();
tem = in.nextInt();
Customer c = new Customer(tp1, tp, tem); //構(gòu)造函數(shù)參數(shù)依次為:姓名、 航線號、 所購票數(shù)
Iterator it = flights.iterator();
boolean found = false;
for ( ; it.hasNext(); )
{
temp = it.next();
if (temp.getFNum().equals(tp)) //確認(rèn)航班號確實存在
{
temp.cancel(c);
found = true;
break;
}
}
if (!found) System.out.println("輸入信息有誤");
System.out.println();
}
}
public void menu() //顯示菜單
{
System.out.println("----------");
System.out.println("1.航線查詢");
System.out.println("2.辦理訂票");
System.out.println("3.辦理退票");
System.out.println("4.退出系統(tǒng)");
System.out.println("----------");
Scanner in = new Scanner(System.in);
int order = in.nextInt(); //指令
switch(order)
{
case 1: query(); break;
case 2: book(); break;
case 3: cancel(); break;
case 4: System.exit(0); in.close();break;
default: System.out.println("輸入錯誤,請重新運(yùn)行訂票系統(tǒng)!");
}
menu();
}
}
public class test
{
public static void main(String args[] )
{
Menu m = new Menu();
m.init();
m.menu();
}
}
-------------------------------這些是實驗心得體會------------------------------
五、心得體會(要詳細(xì),編程中碰到的問題及解決方案)
搜過的資料(都是超鏈接,可直接點(diǎn)擊)
改進(jìn)和思考:
1.????????在寫 Menu類 的 book() 和 cancel() 函數(shù)時,發(fā)現(xiàn)其實有大量重疊部分,因為 cancel() 函數(shù)在刪除完以后,也是要遍歷該航線的 Waiting列列表,看有沒有 Customer 對象能夠完成之前沒能完成的訂票。也就是說,book()函數(shù)里已經(jīng)實現(xiàn)過的功能,還需要在 cancel()函數(shù)里在實現(xiàn)一次。
這樣導(dǎo)致的結(jié)果,就是代碼看上去又拖沓又冗長,于是想了個辦法,在航班類里加入 book() 和 cancel() 函數(shù),并且將 Customer類對象作為參數(shù)傳到航班類里,這樣就能直接調(diào)用 Flight 類的函數(shù)來完成訂票退票,可以提高代碼的復(fù)用性。
而且,如果傳入 Customer類對象,那么,其實航班類的很多get函數(shù)都可以直接去掉了,一下子使代碼簡潔多了,看起來終于舒服多了,之前的一堆get函數(shù)很是拖沓…
從這個優(yōu)化的過程感受到的就是:
不要急著敲代碼,而是先想想能不能有更好的思路,比如這題,剛開始時就沒有想到特別透徹清楚明白的程度,導(dǎo)致后來,實現(xiàn)函數(shù)的時候才發(fā)現(xiàn),完全不用那么繁瑣,明明有更簡單的方法來實現(xiàn),我為什么要弄得那么復(fù)雜?
所以,先別急著敲代碼,而是應(yīng)該先把每一個細(xì)節(jié)琢磨清楚,多問問自己:還能不能再簡化一點(diǎn)?能不能再把代碼寫優(yōu)美一點(diǎn)?一拿到題目就做,往往是最浪費(fèi)時間的方法,因為后面用來修改和優(yōu)化的時間會更多。我應(yīng)該做的是,每次把所有優(yōu)化的可能考慮周全,盡量把類里的方法減少到最少,同時提高這些方法的利用率,我覺得這才是實驗里最為關(guān)鍵的部分。
2.??????著手于簡化、簡化再簡化
將不必要的get 和 set 函數(shù)都去掉,例如 Customer類,該題不考慮顧客改名的情況,故而不需要? name 的 set方法;此外,該題沒有一個顧客同時買多個航線的飛機(jī)票的情況,就算真的要處理這種情況,按照我的代碼設(shè)計的邏輯,也是要新建一個 Customer 對象的,因為我覺得這樣做更方便處理。
再說詳細(xì)一些就是,如果aaa同時買了 K2 和 K3 航班的票,那么,我打算采取的,不是將這這個航班的購票數(shù)都存在一個 Customer的對象里,而是打算再建立一個 Customer對象,保存 K3 的航班線名臣和K3的訂票數(shù)。因為在辦理訂票和退票時,我們都是根據(jù)航班號來查航線的。如果把不同的航班號,存在同一個 Customer對象里,其實無形中加大了查找特定某航線的難度。
所以,照著這么說,航線名也是不會改的,如果這個顧客買的某個航線的票,全都被他自己后來退光了,那也只需要在那個航線的 booked 鏈表里刪掉這個顧客的信息即可
這么想來,其實 fNum 的 set 方法也可以去掉,因為就本題的考慮,根本用不到這個方法。
唯一需要保留的 set 方法,是sum的set方法,因為可能存在退票但沒有退完的情況,這時就需要改變 Customer對象的訂票數(shù)了。
同理,對于 Flight 類,也應(yīng)進(jìn)行一次這樣的考慮和排查。因為方法是為了解決問題而設(shè)計的,不是為了寫一個方法而去寫一個方法。所以,如果能更簡單地解決問題,就不要把問題弄復(fù)雜了!~
3.????????情況考慮要比題目設(shè)置更加周全一些:
題目好像沒提到一點(diǎn),其實在進(jìn)行訂票時,也應(yīng)該進(jìn)行一輪該航班的 booked 列表的遍歷。
因為,考慮真實情況時,同一個顧客是可以多次訂票的,所以,我們需要先知道顧客之前有沒有訂過票,訂過和沒訂過的處理方式是不同的。
4.??????語法點(diǎn)和知識不太熟練,導(dǎo)致的失誤
4.1. 在進(jìn)行字符串比較時,又忘了兩個字符串之間是不能直接用 == 來判斷相等的,而是應(yīng)該用 equals 函數(shù),又一次把 C++的語法規(guī)則套在Java上用了…說明我學(xué)的還是不到位啊!
4.2. 此外,用于讀取 String對象的 next 和 nextLine,必須弄清楚兩者的區(qū)別,正確選擇用哪個,否則用了 equals 來比較以后,得到的結(jié)果仍然是不相等
4.3. 使用泛型前,要先對其進(jìn)行初始化,否則會出現(xiàn)空指針錯誤
-------------------------------其他相關(guān)文章------------------------------
[ Java學(xué)習(xí) ] 類的其他文章匯總(都是超鏈接,可直接點(diǎn)擊):
總結(jié)
以上是生活随笔為你收集整理的java关于泛型的实验代码_[ Java学习 ] 泛型类的应用实验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 口 + A 口:爱国者三位 20W
- 下一篇: java并发排序_Java并发(三):重