2015-02-23 62 views
1

我有一个学校项目,题为“游乐园规划项目”操纵ArrayList和访问方法从另一个类

它要求创建四个类:

  1. 票务 - 型号门票
  2. 商品 - 礼品店提供的模型商品
  3. AmusementPark - 曲目票和商品库存/销售
  4. WaldenAmusementPark - 测试仪程序

AmusementPark类是我需要帮助 - 我明白需要按照指示要发生的事情,但我对如何访问门票和商品的定义,使事情发生的getter/setter方法和的ArrayList不清楚 - 我正在阅读指导,正在观看视频但无法使其工作?:

我在下面粘贴了我的代码(如下所示) - 任何指导都不受欢迎不寻找要写入的实际代码 - 仅限于澄清我的困惑。提前致谢。

----------------票类---------------------------- ------

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

你需要实例化一个Ticket对象,你需要它。

Ticket yourTickecObject = new Ticket(constructor parameters); 

然后只需要调用的getter获取数据

int price = yourTicketObject.getPrice(); 

和setter来设置数据

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) 

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

希望这对我有帮助

+0

谢谢 - 非常感谢您的时间。 – manorris111 2015-02-23 19:14:54