I am currently doing a small project for practice, it is a simple console application for printing movie tickets. I am a student and I just started learning about interfaces and abstraction, so I'm not fully confident in my ability to use them, thus I have an issue with my interface method not printing out the code with in the for loop.
Any help or criticism would be greatly appreciated, thank you :)
Here is my Main class:
package movieticketapp;
import java.util.Scanner;
import java.util.ArrayList;
abstract class Tickets {
Scanner in = new Scanner(System.in);
public static ArrayList<TicketInfo> info = new ArrayList<TicketInfo>();
public void TicketData(){
System.out.println("Enter customer name: ");
String name = in.nextLine();
System.out.println("Enter name of movie: ");
String movie = in.nextLine();
System.out.println("Enter customer age: ");
int age = in.nextInt();
System.out.println("Enter ticket price: ");
Double price = in.nextDouble();
info.add(new TicketInfo(name, movie, age, price));
}
}
interface iTickets {
void PrintTickets();
}
class Movie extends Tickets {}
public class MovieTicketApp implements iTickets{
public static ArrayList<TicketInfo> info = new ArrayList<TicketInfo>();
public static void main(String[] args) {
Movie film = new Movie();
MovieTicketApp ticket = new MovieTicketApp();
film.TicketData();
ticket.PrintTickets();
}
@Override
public void PrintTickets() {
for (int i = 0; i < info.size(); i ) {
System.out.println("CUSTOMER INVOICE\n"
"*************************\n"
"CUSTOMER: " info.get(i).name
"\nMOVIE: " info.get(i).movie
"\nCOST: " info.get(i).price
"\nDISCOUNT: "
"\nTOTAL: ");
}
}
}
Here is my TicketInfo class:
package movieticketapp;
public class TicketInfo {
String name;
String movie;
int age;
double price;
public TicketInfo(String firstName, String movieName, int customerAge, double ticketPrice) {
this.name = firstName;
this.movie = movieName;
this.age = customerAge;
this.price = ticketPrice;
}
}
CodePudding user response:
You have 2 completely unrelated fields. One is called info
. The other is also called info
. That's not very useful, so lets talk in full name terms: One is called movieticketapp.Tickets.info
, the other is called movieticketapp.MovieTicketApp.info
.
Your TicketData()
method fills the Tickets.info
one. Your loop prints the MovieTicketApp.info
one.
For what its worth, this code is an unmitigated disaster.
- It does not follow convention. it's
printTickets()
, notPrintTickets()
. class Movie extends Tickets {}
doesn't seem to make semantic sense. subclassing is used for a 'is a more specific kind of' relation. The sentence 'An ArrayList is a more specific kind of AbstractList' makes sense and is correect. 'A Movie is a more specific kind of Tickets' makes no sense.- You have static variables, representing static state. This makes no sense. As a rule of thumb, if you're not sure if you want some field 'static' or not, you don't. In fact, don't use any
static
fields at all. Generally yourmain
is static (it has to be), and that's the one and only use ofstatic
in the entire code base. Once you've learned a little more java and you know what you're doing, you can start breaking this rule. - If you name interfaces or types
IFoo
, that's usually a bad sign. The idea behind an interface is that there can exist multiple implementations of it. If it is so hard to even imagine what multiple implementations would look like that you can't come up with individual names, that's a good sign interfaces are not appropriate (that there is really just the one feasible implementation). For example, interfaceIList
, concrete implementationList
? That'd be bad. But that's not how java's own lists work: There is interfaceList
and then concrete implementations such asArrayList
,LinkedList
,CopyOnWriteList
, etcetera.
Remove the info
field in your MovieTicketApp class. Turn your info
field from the Tickets
class into being non-static. Make a getter method that retrieves it. Have your for loop operate on what that method returns - either by fetching that list in your main
and passing the list as argument to your printTickets()
method, or by passing the Tickets
instance to your printTickets()
method so that it can print. Or if you really must, by having a non-static field of type Tickets
, which you set in your main and then use in your printTickets
method.