简体   繁体   中英

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. 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. 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. 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. 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. An integer counter must also be created to keep track of how many Bookings are stored in the array. (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". Each line either contains a single Booking or a GroupBooking object. Read each line from the file and instantiate the appropriate type of object (Booking or GroupBooking) and add it to the array. (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.)

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.

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

In your manager class you need a global variable array 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

// 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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