[英]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.