[英]Two instance variables in the class of a one dimensional array
我希望有人可以幫助我解決一個我已經堅持了很長時間的問題。
只是為了澄清場景。 在可容納 180 人的劇院里舉行了一場演出。 它們允許寫入文本文件“bookings.txt”的在線預訂個人預訂是單一的,只有座位、聯系方式和付款狀態記錄。 而團體預訂有座位、聯系電話、付款狀態、團體名稱和團體人數記錄。
到目前為止,我已經創建了 Bookings 和 GroupBookings 類。 我將在下面展示它們:
/**
*
* @author OP
*/
public class Booking
{
public String seat;
public String contact;
public double cost;
public boolean paid;
//Constructor
public Booking(String st, String ct, int cost, boolean pd)
{
seat = st;
contact = ct;
this.cost = cost;
paid = pd;
}//end of Booking
//Getters
public String getSeat()
{
return seat;
}//end of getSeat
public String getContact()
{
return contact;
}//end of getContact
public boolean isPaid()
{
return paid;
}//end of isPaid
public double getCost()
{
//Determining what discount should be applied to their seat location
if (seat.contains("A") || seat.contains("B") ||
seat.contains("C") || seat.contains("D"))
{
cost = 200;
}
else
{
if (seat.contains("E") || seat.contains("F") ||
seat.contains("G") || seat.contains("H"))
{
cost = 160;
}
else
{
if (seat.contains("I") || seat.contains("J") ||
seat.contains("K") || seat.contains("L"))
{
cost = 120;
}
}
}//end of nested if statement
return cost;
}//end of getCost
@Override
public String toString()
{
return seat + "\t" + "R" + cost + "\t" + paid;
}//end of toString
}//end of class booking
/**
*
* @author OP
*/
public class GroupBooking extends Booking
{
private String groupName;
private int groupSize;
public GroupBooking(String st, String ct, boolean pd, String gn, int gs)
{
//Variables from previous class (using inheritance)
super.seat = st;
super.contact = ct;
super.paid = pd;
//New variables for this class
groupName = gn;
groupSize = gs;
}//end of GroupBooking
@Override
public double getCost()
{
cost = super.getCost();
for (int i = 0; groupSize % 4 > i; i++)
{
cost = cost - 60;
i++;
}//end of for loop
return cost;
}//end of getCost
public int getGroupSize()
{
return groupSize;
}//end of getGroupSize
public String getGroupName()
{
return groupName;
}//end of getGroupName
@Override
public String toString()
{
return seat + "\t" + "R" + cost + "\t" + groupName;
}//end of toString
}//end of class GroupBooking
現在對於我堅持的問題:
必須創建一個名為 BookingManager 的新 class。 從那里我必須在一維數組的 class 中聲明兩個實例變量,可用於存儲多達 180 個 Booking 或 GroupBooking 對象。 還必須創建一個 integer 計數器來跟蹤數組中存儲了多少預訂。 (這兩個實例變量不應從類外部訪問)
我仍然是編碼的新手,我不確定在這里做什么。 后續問題也給我帶來了困難:
然后必須創建一個承包商來讀取文本文件“bookings.txt”中的信息。 每行包含一個 Booking 或 GroupBooking object。 從文件中讀取每一行並實例化適當類型的 object(Booking 或 GroupBooking)並將其添加到數組中。 (注意在 GroupBooking 的情況下,您必須在數組中為組中的每個成員創建一個 object。對於一組六個的 Exp,您必須在數組中有六個單獨的 GroupBooking 對象。)
我知道第二個問題需要文件掃描儀,但我不知道是使用 for 循環還是 if 語句來區分單個預訂或團體預訂。
如果有人可以提供幫助,我將不勝感激。 這個話題對我來說還是很新的。
為了防止在 class 之外訪問變量,請聲明變量“私有”。 例如
private String costtotal="";
An instance variable "is not" static ("is not" a class member variable), and are a global variable only declared at the top of the class code below the import statements, so exist until the class exits.
在您的經理 class 中,您需要一個全局變量數組 Booking class
Booking[] bookings;
private String costtotal=""; // e.g.
// in the constructor read the bookings file and find the number of bookings made
//int totalbooked=...whatever reading the file data counts to of bookings made;
bookings=new Booking[totalbooked];
// create and fill each Booking object and assign it to its index on the array in a loop
bookings[loopcount]=new Booking(st,ct,cost,pd);
class系統編碼的不同方案
// bean syntax in a java bean framework class type
public void setCosttotal(String costtotal){
this.costtotal=costtotal;
}
//bean syntax
public String getCosttotal(){
return costtotal;
}
// normal non bean syntax 1
public String costTotal(String csttot){
return (String)csttot;
}
// somewhere else in code in scope to global variable
costtotal=costTotal(valuein);
// normal non bean syntax 2
public String costTotal(String csttot){
costtotal=csttot;
return costtotal;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.