Get Object from other class file - java

Having issue of getting object from other class file...
This function let us take in the title (from movie class) and name (from theatre class).
2 input from user asking what is the title and name, from there we need to take in an movie object and theatre object to check is the title can be found in the movie class. same to name in theatre class.
public MovieScreening(Movie movieObject,Theatre theatreObject){
this.movieObject = movieObject;
this.theatreObject = theatreObject;
}
public Movie getMovieObject() {
return movieObject;
}
public Theatre getTheatreObject() {
return theatreObject;
}

Your code is wrong and incomplete, so I can barely have an idea of what do you want here.
But maybe, this code below can help you somehow. Please state better questions.
import java.util.ArrayList;
public class Screener {
private static ArrayList <MovieScreening> screenings = new ArrayList <MovieScreening>();
/**
* #param args
*/
public static void main(String[] args) {
String movie = "Titanic";
String theatre = "Cineplex";
screenings.add(new MovieScreening(new Movie("Titanic"),new Theatre("Odeon")));
screenings.add(new MovieScreening(new Movie("run lola run"),new Theatre("Cineplex")));
screenings.add(new MovieScreening(new Movie("Titanic"),new Theatre("Cineplex")));
for(MovieScreening s:screenings){
if(s.contains(movie,theatre)){
System.out.println("Added successfully:"+s);
} else {
System.out.println("Your movie or/and theatre cannot be found:"+s);
}
}
}
}
and
public class MovieScreening {
private Movie movie;
private Theatre theatre;
public MovieScreening(Movie movie, Theatre theatre) {
this.movie = movie;
this.theatre = theatre;
}
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
public Theatre getTheatre() {
return theatre;
}
public void setTheatre(Theatre theatre) {
this.theatre = theatre;
}
public boolean contains(String movie, String theatre) {
return this.movie.getTitle().equals(movie) && this.theatre.getName().equals(theatre);
}
#Override
public String toString() {
return "MovieScreening [movie=" + movie + ", theatre=" + theatre + "]";
}
}
and
public class Theatre {
private String name;
public Theatre(String name) {
this.name = name;
}
public String getName() {
return name;
}
#Override
public String toString() {
return "Theatre [name=" + name + "]";
}
}
and
public class Movie {
private String title;
public Movie(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
#Override
public String toString() {
return "Movie [title=" + title + "]";
}
}
will probably give you an output like this
Your movie or/and theatre cannot be found:MovieScreening [movie=Movie [title=Titanic], theatre=Theatre [name=Odeon]]
Your movie or/and theatre cannot be found:MovieScreening [movie=Movie [title=run lola run], theatre=Theatre [name=Cineplex]]
Added successfully:MovieScreening [movie=Movie [title=Titanic], theatre=Theatre [name=Cineplex]]

Related

Returning an object corresponding to a class

So i have 2 classes, and in the class race i have a method ( public Athlete getAthlete(int codAthlete) ) that
should return the object corresponding to the Athlete with the code passed by parameter, but i am not sure how to
implement it. Can someone give me a hand?
public class Athlete {
private int codAthlete;
private String name;
public Athlete(int codAthlete){
this.codAthlete = codAthlete;
}
public int getCodAthlete() {
return this.codAthlete;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String getInformation() {
return "Code: " + this.codAthlete +
" Name " + this.name;
}
}
.
public class Race {
private String idRace;
private Set<Athlete> athletes;
public Race(String idRace) {
athletes = new HashSet<>();
this.idRace = idRace;
}
public String getIdRace () {
return this.idRace;
}
public Athlete getAthlete(int codAthlete){
for(Athlete a: Athlete){
if(a.getCodAthlete() == codAthlete)
a.getInformation();
}
return (????);
// Returns the object corresponding to the Athlete with the code passed by parameter.
}
}

Class Return type when creating a method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
[UML Diagram][1]
I'm studying for the midterm exam next week and I'm practicing some given examples from my professor; however, I am having some trouble with class return type methods.
I attached UML diagram just in case.
What i'm trying to understand is getPerson method in Job class. I don't think i need a array list in Job class to store all the employee. Because I have an array list already in Company class. Also return type is Employee class that I'm not sure how to get person's info using this class return type.
My problems
public Employee getPerson() {} in Job class
public boolean isVacant() {} in Job class
Also would you mind checking getVacantJobs, getFilledJobs, and getAllJobs methods if those are correctly built?
I used iterator to display all the stored jobs.
---------------------------Employee Class -----------------------------
public class Employee {
private String name;
private int id;
public Employee(int id, String name) {
this.name = name;
this.id =id;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
#Override
public String toString() {
return "Employee [name=" + name + ", id=" + id + "]";
}
}
----------------------------Job Class--------------------------------------
public class Job {
private String description;
private int id;
private double maxSalary;
public Job(int id, double maxSalary, String description) {
this.description = description;
this.id = id;
this.maxSalary = maxSalary;
}
public Job(int id, double maxSalary, String description, Employee e1) {
this.description = description;
this.id = id;
this.maxSalary = maxSalary;
}
#Override
public String toString() {
return "Job [description=" + description + ", id=" + id
+ ", maxSalary=" + maxSalary + "]";
}
public final String getDescription() {
return description;
}
public final void setDescription(String description) {
this.description = description;
}
public final double getMaxSalary() {
return maxSalary;
}
public final void setMaxSalary(double maxSalary) {
this.maxSalary = maxSalary;
}
public final int getId() {
return id;
}
public Employee getPerson() {
retrun
}
public final void setPerson(Employee person) {
this.id = person.getId();
}
}
--------------------------Company Class ---------------------------
import java.util.ArrayList;
import java.util.Iterator;
public class Company {
static ArrayList list = new ArrayList();
Iterator itr = list.iterator();
private String name;
public Company(String name) {
this.name = name;
}
public Company() {
// TODO Auto-generated constructor stub
}
public static void addJob(Job j1) {
list.add(j1);
}
public void removeJob(int id) {
list.remove(id);
}
public ArrayList<Job> getVacantJobs() {
while (itr.hasNext()) {
if ((itr == null)) {
System.out.println(itr);
}
}
return null;
}
public ArrayList<Job> getFilledJobs() {
while (itr.hasNext()) {
if (!(itr == null)) {
System.out.println(itr);
}
}
return null;
}
public ArrayList<Job> getAllJobs() {
while (itr.hasNext()) {
System.out.println(itr.next());
}
return null;
}
}
Add field person to Job class.
public class Job {
// .....
private Employee person;
public Employee getPerson() {
return person;
}
public final void setPerson(Employee person) {
this.person = person;
}
public boolean isVacant() {
return person == null;
}
}
And add jobs field to Company class.
public class Company {
// static ArrayList list = new ArrayList(); // You don't need this
// Iterator itr = list.iterator(); // You don't need this.
// .....
private ArrayList<Job> jobs = new ArrayList<>();
public ArrayList<Job> getVacantJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
if (job.isVacant())
result.add(job);
return result;
}
public ArrayList<Job> getFilledJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
if (!job.isVacant())
result.add(job);
return result;
}
public ArrayList<Job> getAllJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
result.add(job);
return result;
}
}

Creating an object and calling it

this is my current code to store rooms(it compiles fine) but in the UML there is a variable called addEquipment and there is also another class called Equipment to be defined. I'm having trouble wrapping my head around what I'm supposed to do with this. Am I supposed to create and call an object called Equipment? what goes in addEquipment?
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private String equipmentList;
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public String getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(String anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
}
//Create room object
public Room(int capacity, String equipmentList) {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
}
}
You can create a new class Equipment and modify your attribute equipmentList to be a List:
public class Equipment {
private String name;
public Equipment(String name) {
this.name = name;
}
}
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private List<Equipment> equipmentList = new ArrayList<Equipment>();
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public List<Equipment> getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(List<Equipment> anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
Equipment oneEquipment = new Equipment(newEquipment);
equipmentList.add(oneEquipment);
}
//Create room object
public Room() {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
String capacity=String.valueOf(getCapacity());
String room = "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
return room;
}
}
In the method addEquipment, you can create a new Equipment and add it to equipmentList, like code above.
An Equipment class could be anything. Lets assume the "Equipment"-class has a String called "name" as it's attribute
public class Equipment {
String name;
public Equipment( String name) {
this.name = name;
}
public String getName() {
return this.name
}
}
When you extend your Room class by the requested "addEquipment" method, you can do something like this.
public class Room {
... // Your code
private int equipmentIndex = 0;
private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects
public void addEquipment( Equipment eq ) {
if ( equipmentIndex < 10 ) {
equipment[ equipmentIndex ] = eq;
equipmentIndex++;
System.out.println("Added new equipment: " + eq.getName());
} else {
System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
}
}
}
Now when you call
room.addEquipment( new Equipment("Chair") );
on your previously initialized object of the Room-class, you will get
"Added new equipment: Chair"
Hope this helps a bit.
PS: The code is untestet (maybe there hides a syntax error somewhere)

trying to print a method from arraylist

I am trying to print title from my member class using array in my library class
public class Book gives me this error :
http://screencast.com/t/tqpJp2BF8sH
{
// instance variables - replace the example below with your own
private int x;
private Integer bookid;
private String author;
private String title;
private String ficornonfic;
/**
* Constructor for objects of class Book
*/
public Book(Integer bookID, String Author, String Title, String FictionORnonfiction )
{
bookid = bookID;
author = Author;
title = Title ;
ficornonfic = FictionORnonfiction;
x = 0;
}
public String PrintListOfBooks()
{
return title;
}
public String toString() {
return "Title:" + title + " BookId: " + bookid + " Author: " + author + ".";
}
public int getTitle() {
return this.title;
}
}
---------------------------------------------------------
this is my library class
import java.util.ArrayList;
public class Library
{
private ArrayList<Member>listOfMembers;
public Library()
{
listOfMembers = new ArrayList<Member>();
listOfBooks = new ArrayList<Book>();
}
public void storeMember(Member Member)
{
listOfMembers.add(Member);
}
public int numberOfMembers()
{
return listOfMembers.size();
}
public void listMembers()
{
for (int item=0; item<listOfMembers.size(); item++ ) {
Member m = listOfMembers.get (item);
System.out.println(m.GetWholeName());
}
}
public Member findMember(int id) {
for(Member member : listOfMembers) {
if (member.getId() == id) {
return member;
}
}
return null;
}
private ArrayList<Book>listOfBooks;
public void storeBook(Book Book)
{
listOfBooks.add(Book);
}
public int numberOfBooks()
{
return listOfBooks.size();
}
public void listBooks()
{
for (int item=0; item<listOfBooks.size(); item++ ) {
Book b = listOfBooks.get (item);
System.out.println(b.PrintListOfBooks());
}
}
public Book findBook(string title) {
for(Book book : listOfBooks) {
if (book.getId() == id) {
return book;
}
}
return null;
}
}
I am trying to print title from my member class using array in my library class
public class Book gives me this error :
http://screencast.com/t/tqpJp2BF8sH
The title is a String, not an int. You can change the method signature to match the field type:
public String getTitle() {
return this.title;
}
First, as others have noted your title is a String not an int. So change the return type of getTitle() (or remove it altogether). Because it appears like you've correctly overridden toString() in Book to get all the information, and you could iterate the contents of your List with a for-each loop. Something like,
for (Book b : listOfBooks) {
System.out.println(b);
}
which is equivalent to
for (int i = 0; i < listOfBooks.size(); i++) {
Book b = listOfBooks.get(i);
System.out.println(b.toString());
}
Finally in Java, by convention, method names start with a lower case letter.
You are essentially telling the program to return an Integer when the value you wish to return is a String.
If you change int to String, the error will go away.
public String getTitle() {
return this.title;
}

Parse complex JSON object with GSON with changing content due to OOP

I get such a JSON from the server and want to parse it to my objects:
"product":{
"product_type":"assignment",
"id":717,
"product_profile":{
"title":"new Order from java",
"info":"Some special info",
"dtl_expl":true,
"special_info":""
}
}
Depend on "product_type" value I get different value of "product_profile" from server. The "product_profile" can be one of three types. I created class presentation for each of them. But question is that how organize correct parsing of JSON object to my Product class due to OOP principles? Should I create interface and implement it in each of my three classes, or I should create one parent class and extend it in my three classes to make it work right?
My classes structure. First of all Product class , object of which I should get as a result from json:
public class Product {
ProductAssignment prodAss;
ProductWriting prodWr;
ProductType returnState;
#SerializedName("id")
int id;
#SerializedName("product_type")
String product_type;
#SerializedName("product_profile")
ProductType product_profile;
public Product()
{}
public Product(int id, String product_type, ProductType product_profile)
{
this.id = id;
this.product_type = product_type;
this.product_profile = product_profile.returnObject(product_type);
}
public int getProductId()
{
return this.id;
}
public String getProductType()
{
return this.product_type;
}
public ProductType getProduct()
{
return product_profile.returnObject(product_type);
}
public void setProductId(int id)
{
this.id = id;
}
public void setProductTitle(String product_type)
{
this.product_type = product_type;
}
public void setProduct(ProductType product_profile)
{
this.product_profile = product_profile;
}
#Override
public String toString() {
return "id=" + id + " " + "title=" + product_type
+ " " + "profile=" + product_profile + "}";
}
}
Now parent class ProductType for two subclasses:
public class ProductType extends ProductType{
String product;
static ProductType productType;
static ProductAssignment productAssignment;
static ProductWriting productWriting;
IProductType component;
private ProductType returnState;
ProductAssignment prodAss;
ProductWriting prodWr;
public ProductType()
{
}
public ProductType(IProductType c)
{
component = c;
}
// implemented method of interface
#Override
public ProductType returnObject(String product_type)
{
System.out.println("ProductType");
if (product_type.equals("assignment"))
returnState = prodAss.returnObject(product_type);
else if (product_type.equals("writing"))
returnState = prodWr.returnObject(product_type);
System.out.println(returnState.getClass().getName());
return returnState;
}
}
One of the subclasses:
public class ProductWriting extends ProductType{
#SerializedName("id")
int id;
#SerializedName("title")
String title;
#SerializedName("pages_number")
int pages_number;
#SerializedName("number_of_references")
String number_of_references;
#SerializedName("dtl_expl")
boolean dtl_expl;
#SerializedName("info")
String info;
public ProductWriting()
{}
public ProductWriting(String title, String info, boolean dtl_expl,
int pages_number ,
int id,String number_of_references)
{
this.title = title;
this.info = info ;
this.dtl_expl = dtl_expl;
this.id = id;
this.pages_number = pages_number;
this.number_of_references = number_of_references;
}
public ProductWriting(IProductType c){
super(c);
}
// getters and setters
#Override
public ProductType returnObject(String res) {
System.out.println("Writing");
super.returnObject(res);
return new ProductWriting();
}
}
Another one :
public class ProductAssignment extends ProductType{
ProductAssignment thisObj;
#SerializedName("title")
String title;
#SerializedName("info")
String info;
#SerializedName("dtl_expl")
boolean dtl_expl;
#SerializedName("special_info")
String special_info;
#SerializedName("shoot_exclusive_video")
boolean shoot_exclusive_video;
#SerializedName("shoot_common_video")
boolean shoot_common_video;
public ProductAssignment()
{}
public ProductAssignment(String title, String info, boolean dtl_expl, String special_info,
boolean shoot_common_video, boolean shoot_exclusive_video)
{
this.title = title;
this.info = info ;
this.dtl_expl = dtl_expl;
this.special_info = special_info;
this.shoot_common_video = shoot_common_video;
this.shoot_exclusive_video =shoot_exclusive_video;
}
// getters and setters
#Override
public ProductType returnObject(String res) {
System.out.println("Assignment");
super.returnObject(res);
return new ProductAssignment();
}
#Override
public String toString()
{
return "title=" + title + "info " + "=" + info
+ " " + "profile=" + dtl_expl + "}";
}
}
Interface for binding my classes:
public interface IProductType
{
ProductType returnObject(String parse);
}
I tried to implement in this way, but it doesn't work for now with it.

Categories

Resources