操纵ArrayList并从另一个类访问方法

时间:2015-02-23 18:34:20

标签: java arraylist setter getter

我有一个名为"游乐园编程项目"

的学校项目

它需要创建四个类:

  1. 门票 - 模特门票
  2. 商品 - 礼品店提供的模特商品
  3. AmusementPark - 跟踪门票和商品库存/销售
  4. WaldenAmusementPark - 测试程序
  5. AmusementPark类是我需要帮助的地方 - 我了解需要按照说明进行操作但我不清楚如何访问在Ticket和Merchandise中定义的getter / setter方法和ArrayLists以使事情发生 - 我正在阅读指导,观看视频但无法使其发挥作用?:

    我已经粘贴了我的代码(例如它) - 任何指导都表示赞赏不寻找要编写的实际代码 - 只是为了清除我的困惑。提前谢谢。

    ---------------- TICKET CLASS ----------------------------- -----

    import java.util.*;
    
    public class Ticket {
    
        //Ticket Class models admission tickets
    
    
        //Instance Variables called for in the instructions
        private long number;
        private String category;
        private String holder;
        private Date date;
        private double price;
        private boolean purchased;
    
    
        // Constructor Ticket -- The instructions did not call for instance field "number" as a parameter and left "purchased" out of the UML???
        public Ticket (long number, String category, String holder, Date date, double price, boolean purchased){      
            this.number = number;               //Stores unique ticket ID Number
            this.category = category;           //Stores adult, child, or senior
            this.holder = holder;               //Stores name of purchaser
            this.date = date;                   //Stores admission date for the ticket
            this.price = price;                 //Stores price of the ticket
            this.purchased = purchased;         //Stores true if purchased, false if reserved 
        }  // End Brace Constructor Ticket
    
    
        // MUTATOR METHODS..............................................
    
        // setPrice Mutator Method
        public void setPrice(double price){
            this.price = price;
        }  // End Brace setPrice
    
        //changePurchaseStatus Mutator Method
        public void setchangePurchaseStatus(boolean newStatus){
            this.purchased = newStatus;
        } // End Brace changePurchasedStatus
    
    
    
        // ACCESSOR METHODS.............................................
    
        //getnumber Accessor Method
        public long getnumber(){
            return number;
        }
    
        //getcategory Accessor Method
        public String getcategory(){
            return category;
        }
    
        //getholder Accessor Method
        public String getholder(){
            return holder;
        }
    
        //getdate Accessor Method
        public Date getdate(){
            return date;
        }
    
        //getprice Accessor Method
        public double getprice(){
            return price;
        }
    
        //getpurchased Accessor Method
        public boolean getpurchased(){
            return purchased;
        }
    
    
    }  // End Brace Ticket Class
    
    --------------------------MERCHANDISE CLASS------------------------------
    
    public class Merchandise {
    
    
        //Instance Variables called for in the instructions
        private long ID;                            //ID of specific merchandise item 
        private String category;                    //Stores type of item (T-Shirt, Sweatshirt, Stuffed Animal) - if invalid display "Unknown"
        private String description;                 //Stores description of item
        private double price;                       //Stores price of item
        private boolean instock;                    //True = in stock  False = on order
    
    
        // Constructor Merchandise
        public Merchandise(long ID, String category, String description, double price, boolean instock){
             this.ID = ID;
             this.category = category;
             this.description = description;
             this.price = price;
             this.instock = instock;
        } // End Brace Constructor Merchandise
    
    
    
        // MUTATOR METHODS..............................................
    
        public void setPrice(double price){
            this.price = price;
        }
    
        public void setInstock(boolean newStatus){   
            this.instock = newStatus;
        }
    
    
    
        // ACCESSOR METHODS.............................................
    
        public long getID(){
            return ID;
        }
    
        public String getcategory(){  
            return category;
        }
    
        public String getdescription(){
            return description;
        }
    
        public double getprice(){
            return price;
        }
    
        public boolean getinstock(){
            return instock;
        }
    
    
        // toString Method.............................................
        @Override
        public String toString(){
            return("Merchandise ID:" + "\t" + this.ID + "\n" + "Merchandise Category:" + "\t" + this.category + "\n" 
            + "\t" + "Merchandise Description:" + "\t" + this.description + "$" + this.price + "\t" 
            + "In-Stock Status" + "\t" + this.instock);
        }
    
    
    
    } //End Brace Merchandise Class
    
    
    ------------------------AMUSEMENT PARK CLASS---------Where I Need Help!!--------
    import java.util.ArrayList;
    
    import java.util.Date;
    
    public class AmusementPark {
    
        //Instance Variables called for in the instructions
        private String name = "Walden Gift Shop";   // Assigned a value that I think is needed to support "getName" Accessor Method 
        private Date date;                          // Not called for in the instruction but I believe it is needed
    
    
        //ArrayLists
        public static ArrayList<Ticket> tickets = new ArrayList<Ticket>();                  //Stores ticket objects
        public static ArrayList<Merchandise> merchandise = new ArrayList<Merchandise>();    //Stores Merchandise objects
    
        //  Stores ArrayList of type Date called "ticketDate" that has all dates for which tickets are still available 
        //  This ArrayList was not clearly called for in the instructions but is required to return dates for which tickets are still available - 
        //  if no tickets are available return empty list
    /** public ArrayList<Date> ticketDate(){  
        ArrayList<Date> ticketDate = new ArrayList<Date>();
        Date.(2014, 03, 01);
        }  */
    
        // Constructor AmusementPark
        public AmusementPark(String name){  //How should I be referencing / using the ArrayLists tickets & merchandise in the constructor?????? 
            this.name = name;
        }
    
    
        // ACCESSOR METHODS.............................................
    
        public String getName(){        // Returns the name of the amusement park shop
            return name;
        }
    
    
    
    //---------------------  Have 3 getters to return various Ticket data - Need to fix the stubs for them shown below----------------------
    
        // Need to fix this getTicketDates getter
    /** public getTicketDates(){        //Returns an ArrayList<Date> of all dates tickets are still available
            return                  
        } */
    
        // Need to fix this getTickets getter
    /** public Date getTickets(){       // Returns an integer indicating number of tickets available for specified date
            return date;
        } */
    
    
        // Need to fix this getTicket getter
    /** public long getTicket (long id){        //Returns Ticket number / ID that matches the specified ticket
            Ticket myTicketID = new Ticket();
                id.getnumber();
        }*/
    
    //---------------------------END the 3 "getMerchandise" getters----------------------------------------------------------------------
    
    
    
    //---------------------  Have 3 "getMerchandise" getters to define - Need to fix the stubs for them shown below----------------------
    
        // Need to fix this getMerchandise getter
    /** public getMerchandise(){                    //Returns an Arraylist<Merchandise> of all inventory - if no match return empty list
          return              ;
        } */
    
        //Need to fix this getMerchandise getter
    /** public getMerchandise (String category){    //Returns a list of Merchandise objects whose category matches the specified (e.g., T-Shirts), no match return empty list
            return   
        }*/
    
        //Need to fix this getMerchandise getter
        /** public getMerchandise (long id){        //Returns the merchandise item that matches the specified id number, if not match return null
            return
        }*/
    
    //---------------------------END the 3 "getMerchandise" getters----------------------------------------------------------------------
    
    
    
        //Need to fix this addTicket Mutator method
    /** public addTicket (Ticket) { // Adds a new Ticket to the inventory of AmusementPark Class    
        } */
    
        //Need to fix this buyMerchandise Mutator method
    /** public buyMerchandise (String id){ //Removes a Merchandise object from teh list of merchandise of the AmusementPark Class, if no match throw exception  
        } */
    
    
        //Need to fix this buyTicket Mutator method
    /** public buyMerchandise (String id){ //Removes a Ticket object from the list of ticket items of the AmusementPark Class - If not match throw exception    
        } */
    
    
    
    
    
    
    }  // End Brace AmusementPark Class
    

1 个答案:

答案 0 :(得分:1)

您需要在需要的地方实例化Ticket对象。

Ticket yourTickecObject = new Ticket(constructor parameters);

然后只需调用getter获取数据

int price = yourTicketObject.getPrice(); 

并设置设置数据

yourTicketObject.setPrice(someValue);

对于ArrayList,票证声明:

ArrayList<someTipe> yourArrayListName = new ArrayList<someTipe>();

吸气剂:

完整数组列表:

public ArrayList getYourArrayListName(){
  return this.yourArrayListName;
}

具体项目:

public yourArrayListType getItem(index){
  return this.yourArrayListName.get(index)
}

设定器:

如果您想添加一个项目:

public void addObjectToYourArrayListName(SomeValidType argument){
   this.yourArrayListName.add(argument);
}

如果要设置ArrayList:

public void setYourArrayListName(ArrayList<someValidType> argument){
   this.yourArrayListName = argument;
}

要访问数组列表:

设定器:

添加项目:

yourTicketObject.addObjectToYourArrayListName(SomeValidType argument)

添加完整的ArrayList:

yourTicketObject.setYourArrayListName(ArrayList<someValidType>)

吸气剂:

获取完整数组列表:

Arraylist<someType> someName = yourTicketObject.getYourArrayListName()

获取具体索引:

YourArrayListType variableName = yourTicketObject.getItem(index)

所有这些代码都是抽象的,您可以在每个适应的地方使用它,注意变量的类型。

希望这有帮助