简体   繁体   English

一维数组的 class 中的两个实例变量

[英]Two instance variables in the class of a one dimensional array

I'm hoping someone can help me out with a question that I've been stuck on for quite a while.我希望有人可以帮助我解决一个我已经坚持了很长时间的问题。

Just to clarify the scenario.只是为了澄清场景。 There is a show being held in a theatre that can hold 180 people.在可容纳 180 人的剧院里举行了一场演出。 They allow online bookings that are written to a text file "bookings.txt" Individual bookings are single that only have a seat, contact and payment status record.它们允许写入文本文件“bookings.txt”的在线预订个人预订是单一的,只有座位、联系方式和付款状态记录。 Whereas group bookings have seats, a contact number, payment status, group name and group size records.而团体预订有座位、联系电话、付款状态、团体名称和团体人数记录。

So far I have created the Bookings and GroupBookings classes.到目前为止,我已经创建了 Bookings 和 GroupBookings 类。 I'll show them below:我将在下面展示它们:

/**
 *
 * @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

Now for the question that I am stuck on:现在对于我坚持的问题:

A new class has to be created called BookingManager.必须创建一个名为 BookingManager 的新 class。 From there I have to declare two instance variables in the class of one-dimensional array that can be used to store up to 180 Booking or GroupBooking objects.从那里我必须在一维数组的 class 中声明两个实例变量,可用于存储多达 180 个 Booking 或 GroupBooking 对象。 An integer counter must also be created to keep track of how many Bookings are stored in the array.还必须创建一个 integer 计数器来跟踪数组中存储了多少预订。 (These two instance variables should not be accessible from outside the class) (这两个实例变量不应从类外部访问)

I'm still a newbie in coding and I'm unsure of what to do here.我仍然是编码的新手,我不确定在这里做什么。 The follow-up question is also giving me difficulties:后续问题也给我带来了困难:

A contractor then has to be created to read the information from the text file "bookings.txt".然后必须创建一个承包商来读取文本文件“bookings.txt”中的信息。 Each line either contains a single Booking or a GroupBooking object.每行包含一个 Booking 或 GroupBooking object。 Read each line from the file and instantiate the appropriate type of object (Booking or GroupBooking) and add it to the array.从文件中读取每一行并实例化适当类型的 object(Booking 或 GroupBooking)并将其添加到数组中。 (Note in the case of GroupBooking you must create an object in the array for each member of the group. Exp for a group of six you have to have six separate GroupBooking objects in the array.) (注意在 GroupBooking 的情况下,您必须在数组中为组中的每个成员创建一个 object。对于一组六个的 Exp,您必须在数组中有六个单独的 GroupBooking 对象。)

I know a file scanner is needed from the second question but I have no idea whether to use a for loop or an if statement to differentiate between a single booking or a group booking.我知道第二个问题需要文件扫描仪,但我不知道是使用 for 循环还是 if 语句来区分单个预订或团体预订。

If anyone can help I would truly appreciate it.如果有人可以提供帮助,我将不胜感激。 This topic is still very new to me.这个话题对我来说还是很新的。

To prevent a variable being accessible outside a class declare the variable "private".为了防止在 class 之外访问变量,请声明变量“私有”。 eg例如

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. 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.

In your manager class you need a global variable array Booking class在您的经理 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);  

Different schemes of class systematics of coding 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM