Reputation: 79
import java.util.ArrayList;
import java.util.Collections;
public class AmusementPark {
private ArrayList<Ticket> tickets;
private ArrayList<Merchandise> merchandise;
private String name;
AmusementPark(String name){
this.name=name;
this.tickets = new ArrayList<Ticket>();
this.merchandise= new ArrayList<Merchandise>();
}
void addTicket(long num, String cat, String h, String dt, double pr, boolean pch){
Ticket test = new Ticket(num, cat, h, dt, pr, pch);
tickets.add(test);
}
String getName(){
return name;
}
public ArrayList<String> getTicketDates(){
ArrayList<String> theDateArray= new ArrayList<>();
int i;
for (i=0; i <tickets.size(); i++){
if(tickets .get(i).getPurchased()== false){
theDateArray.add(tickets.get(i).getDate());
}
}
for(int f=0; f<theDateArray.size();f++){
System.out.println(theDateArray.get(f)+ " ");
}
return theDateArray;
}
public int getTickets(String date){
int tix= getTicketDates().indexOf(date);
int occurances= Collections.frequency(getTicketDates(), tix);
if (tix>=0){
System.out.println(occurances);
}
return occurances;
}
}
Now, here is the main class....
public class AmusementParkTester {
public static void main(String[] args) {
AmusementPark park1= new AmusementPark("Walden University Park");
park1.addTicket(777, "Child", "Noah Johnson","2017-06-09" , 27.99, false);
System.out.println("The dates in which tickets are available are as follows: ");
park1.getTicketDates();
park1.getTickets("2017-06-09");
}
}
If I add and item to the tickets ArrayList, and then I print out the method getDate, it prints out the date 3 times. I tried everything including taking the print statement out and putting it outside the next }. When I put in a break, it gives me a dead code error.
What this loop is basically doing is taking another list called tickets, and when the boolean of the getDate method is false, it adds the date in the form of a String to the next ArrayList. The dates are printing 3 times for each ticket though.
UPDATE
I added the entire class as well as the main class to it.
The getTickets method is where a date is searched, and the amount of tickets in the ArrayList that meet that date is returned as an integer.
Once I call that method on the main class, it repeats the date 3 times, and it still doesn't return the right integer in the next method.
Upvotes: 0
Views: 133
Reputation: 140504
As is commonly the case, good indentation makes the problem clearer.
Because the loop with the System.out.println
is inside the other loop, you print out the contents of theDateArray
multiple times.
Assuming all of the tickets in tickets
aren't purchased, you add a new element to theDateArray
each time; so:
Move this loop outside the other loop:
for (i=0; i <tickets.size(); i++){
if(tickets .get(i).getPurchased()== false){
theDateArray.add(tickets.get(i).getDate());
}
}
for(int f=0; f<theDateArray.size();f++){
System.out.println(theDateArray.get(f)+ " ");
}
Also, note that enhanced for loops make this much cleaner:
for (Ticket t : tickets) {
if (!t.getPurchased()) {
theDateArray.add(t.getDate());
}
}
for (String date : theDateArray) {
System.out.println(date + " ");
}
Upvotes: 2
Reputation: 3844
You are using wrong int inside second loop, instead of int i you should use int f. Like someone else mentioned in the comments you have to many brackets, remove one after if and another before return and it should be ok. Try this code:
public ArrayList<String> getTicketDates(){
ArrayList<String> theDateArray= new ArrayList<>();
int i;
for (i=0; i<tickets.size(); i++){
if(tickets.get(i).getPurchased()== false)
theDateArray.add(tickets.get(i).getDate());
}
for(int f=0; f<theDateArray.size();f++){
System.out.println(theDateArray.get(f)+ " ");
}
return theDateArray;
}
Upvotes: 1
Reputation: 2922
for (i=0; i<tickets.size(); i++){
if(tickets.get(i).getPurchased()== false);{
theDateArray.add(tickets.get(i).getDate());
}
for(int f=0; f<theDateArray.size();f++){
System.out.println(theDateArray.get(i)+ " ");
}
}return theDateArray;
There is a semicolon after if(tickets.get(i).getPurchased()== false);
inside the for loop, this makes it a body-less if, and the block of code following will always be executed . May be your input data data is such that the loop runs 3 times, thus the output
Upvotes: 0