2015-02-23 37 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