gpt4 book ai didi

java - 操作 ArrayList 并访问另一个类的方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:18 25 4
gpt4 key购买 nike

我有一个名为“游乐园编程项目”的学校项目

需要创建四个类:

  1. 门票 - 模特入场券
  2. 商品 - 礼品店出售的商品模型
  3. 游乐园 - 跟踪门票和商品库存/销售
  4. WaldenAmusementPark - 测试程序

AmusementPark 类是我需要帮助的地方 - 我了解按照说明需要发生什么,但我不清楚如何访问 Ticket 和 Merchandise 中定义的 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

最佳答案

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

Ticket yourTickecObject = new Ticket(constructor parameters);

然后调用getter即可获取数据

int price = yourTicketObject.getPrice(); 

以及设置数据的 setter

yourTicketObject.setPrice(someValue);

ArrayList也是一样,ticket中声明:

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

setter/getter :

完整数组列表:

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

setter/getter :

获取完整数组列表:

Arraylist<someType> someName = yourTicketObject.getYourArrayListName()

获取具体索引:

YourArrayListType variableName = yourTicketObject.getItem(index)

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

希望这有帮助

关于java - 操作 ArrayList 并访问另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com