I'm currently trying to create a class in java, to this specification:
I'm not sure how to create "ratings" since I'm seeing its using some
map function using the input of a string and an integer.
Here's my code so far:
public class Movie {
String ID;
String Name;
String Description;
String Genre[];
String Directors[];
String Actors[];
String Language;
String CountryOfOrigin;
}
Please create different objects for genre, director, actor and rating. It will be a best practice when you try to add more information to each entity.
Use access modifier "private" to each attribute and implement get,set methods as required.
Use ArrayLists instead of arrays to avoid resizing efforts when required.
import java.util.ArrayList;
import java.util.List;
public class Genre {
private int id;
private String name;
public Genre(int id, String name) {
this.id = id;
this.name = name;
}
}
public class Director {
private int id;
private String name;
public Director(int id, String name) {
this.id = id;
this.name = name;
}
}
public class Actor {
private int id;
private String name;
public Actor(int id, String name) {
this.id = id;
this.name = name;
}
}
public class Rating {
private int value;
private String name;
public Actor(int value, String name) {
this.value = value;
this.name = name;
}
}
public class Movie {
private int id;
private String name;
private String description;
private List<Genre> generes;
private List<Director> directors;
private List<Actor> actors;
private String language;
private String countryOfOrigin;
pulic Movie(int id, String name, String description, String language, String countryOfOrigin){
this.id = id;
// set other variables
this.actors = new ArrayList<Actor>();
// create other lists
}
public void addGenere(Genre genere){
this.generes.add(genere);
}
// implement other add methods to lists
}
Related
I'm creating a basic crud application for members in a library. I've created a spring api to get the values and insert it into the DB. So when I check it using postman a error is shown. (500: Internal server error)Below diagram shows a request i tried to sent.
As shown above I'm sending another object inside the Member object and once I removed the latestBook, there is no error the value is posted.
I want to resolve this issue, please help me with this.
API
#PostMapping("/m")
public Member save(#RequestBody Member member){
repository.save(member);
return member;
}
Member.java
#Document(collection = "Members")
public class Member {
#Id
private String id;
private String name;
private Book latestBook;
private String gender;
private int contact;
public Member() {
}
public Member(String id, String name, Book latestBook, String gender, int contact) {
this.id= id;
this.name = name;
this.startMembershipDate = startMembershipDate;
this.gender = gender;
this.contactNum = contactNum;
}
//getters , setters and toString method
}
Book.java
public class Book{
private String name;
private String author;
private int year;
public Book(String name, String author, int year) {
this.name= name;
this.author= author;
this.year = year;
}
//getters , setters and toString method
}
I think the problem is with the Book class so please help me to solve this issue.
Not only for posting it does not work for delete either.
Solved the issue.
#Document(collection = "Members")
public class Member {
#Id
private String id;
private String name;
private Book latestBook = new Book();
private String gender;
private int contact;
public Member() {
}
public Member(String id, String name, Book latestBook, String gender, int contact) {
this.id= id;
this.name = name;
this.startMembershipDate = startMembershipDate;
this.gender = gender;
this.contactNum = contactNum;
}
//getters , setters and toString method
}
////////////////////////
public class Book{
private String name;
private String author;
private int year;
Book(){}
public Book(String name, String author, int year) {
this.name= name;
this.author= author;
this.year = year;
}
//getters , setters and toString method
}
I apologize, this question has already been answered once. However, the solution didn't help me.
I made a program working with an ArrayList containing name,ID and country. However, I cannot access the name, ID and country on their own, only their object as a whole. I tried using animal.name and I get the error name cannot be resolved or is not a field. I tried using methods like getName() to return the name of the object, but I got the error The method getName() is undefined for the type ArrayList<ANIMAL>. Would you please help me with that?
This is the class which, when called, creates a new object inside the ArrayList and fields of which I'm trying to access:
import java.io.*;
public class ANIMAL implements Serializable {
String nameA;
String IDA;
String countryA;
public ANIMAL(String name, String ID, String country){
nameA = name;
String IDA = ID;
String countryA = country;
}
public String getName(){
return nameA;
}
public String getCountry(){
return countryA;
}
public String getID(){
return IDA;
}
}
Your animal class:
public class Animal {
private String name;
private String id;
private String country;
public Animal (String name, String id, String country){
this.name = name;
this.id = id;
this.country = country;
}
public String getName(){
return name;
}
public String getCountry(){
return country;
}
public String getId(){
return id;
}
}
Pseudo application class (with main method for starting the application):
public class MyApp {
public static void main(String[] args) {
List<Animal> list = new ArrayList<>();
list.add(new Animal("British Bulldog", "12345", "UK"));
list.add(new Animal("Boston Terrier", "12346", "USA"));
list.add(new Animal("German Shepherd", "12347", "Germany"));
// this is a for-each loop but basically you just need
// to get an item from the list, list.get(i) would suffice
for (Animal a: list) {
System.out.println(a.getName());
}
}
}
Prints:
British Bulldog
Boston Terrier
German Shepherd
Also: I took the liberty of tidying up your code to match conventions, look at the differences and try to understand them.
Your constructor was flawed.
import java.io.*;
public class ANIMAL implements Serializable {
String name;
String ID;
String country;
public ANIMAL(String name, String ID, String country){
this.name = name;
this.ID = ID;
// String IDA = ID; doesn't assigns param ID to field ID of the class
this.country = country; //same
}
public String getName(){
return name;
}
public String getCountry(){
return country;
}
public String getID(){
return ID;
}
}
Hope this helps.
in C# you can create a instance of a class and set the values of variables at the same time:
public class Object
{
public virtual long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Version { get; set; }
public long ParentId { get; set; }
}
public class Start
{
Object object= new Object()
{
Id = 1,
Name = name,
ParentId = parentId,
Description = null,
Version= 2
};
}
Is this possible in Java aswell and how?
The standard way for setting values when creating an instance is to just have a constructor:
class ExampleObject {
long id;
String name;
String description;
int version;
long parentId;
public ExampleObject(final long id, final String name, final String description, final int version, final long parentId) {
this.id = id;
this.name = name;
this.description = description;
this.version = version;
this.parentId = parentId;
}
}
And then call it like:
ExampleObject exampleObject = new ExampleObject(1, name, null, 2, parentId);
It is possible to use a similar syntax to what you have shown, but it has quite a few downsides which you should research about before using it (and you also cannot use variables with this):
ExampleObject exampleObject = new ExampleObject() {{
id = 1;
name = "";
parentId = 2;
description = null;
version = 2;
}};
class ExampleObject {
long id;
String name;
String description;
int version;
long parentId;
}
What this does is creates an anonymous class with a static initialiser block. A static initialiser block looks like:
class ExampleObject {
long id;
String name;
String description;
int version;
long parentId;
{
id = 1;
name = "";
parentId = 2;
description = null;
version = 2;
}
}
You can create a constructor that accepts values for all the fields. This way, you can create a new instance of that object and set the values you want at the same time:
public class MyClass {
public long id;
public String name;
public String description;
public int version;
public long parentId;
/** Constructor **/
public MyClass(long id, String name, String description, int version, long parentId) {
this.id = id;
this.name = name;
this.description = description;
this.version = version;
this.parentId = parentId;
}
}
public static void main(String args[]) {
MyClass myClass = new MyClass(1, "name", "description", 1, 1);
}
By the way, it's not recommended (although you can) to name a class Object in Java, since Java also has a class with that very same name, and all Java classes extend from it (can lead to confussion).
public class Object
{
public long id;
public String name;
public String description;
public int version;
public long parentId;
public Object(long Id,string Name,string Description,int Version,long Parent_Id)
{
this.Id =Id ;
this.Name =Name ;
this.Description =Description ;
this.Version =Version ;
}
class Person {
private String name;
private String sex="male";
public Person(String name) {
this.name = name;
}
public String getSex(){return this.sex;}
}
In the above class, if I want to set the default value for sex. is it OK?
then, if I want to create new Person, is the following code good?
Person p = new Person("mike");
String sex = p.getSex();
You can chain constructors in Java to give certain values defaults.
For example, here's a constructor that takes a name and one that takes a name and a sex.
public class Person {
private String name;
private String sex;
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public Person(String name) {
this(name, "male");
}
// getters/setters here
}
If your object has a lot of properties that may be optional, you should consider the Builder pattern instead of standard constructors.
It always to good to make your class thread safe by defining the fields as final and assign the values only through constructor.
class Person {
private final String name;
private final String sex;
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() { return this.name; }
public String getSex(){return this.sex;}
}
I need to update my Country class so that it can store a list of languages, I also need a field for the list, a getter, and a method that allows me to add a language to the collection. I very green when it comes to programing. This is what I have so far.
public class Country {
private int id;
private String name;
private long population;
private double medianAge;
private List<String> languages;
List<String> list = new ArrayList<>();
/**
* Create a Country object with the given properties
*/
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
}
You can remove the member list - cannot see a reason for you to have it.
You constructor can be:
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
this.languages = new ArrayList<>();
}
Then you can have a method to add a language to the list:
public void addLanguage (String language) {
languages.add(language);
}
Finally a method to return the list of languages as given below:
public List<String> getLanguages() {
return languages;
}
More information on how ArrayList works
You need a getter method to access languages like below
getLanguages(){
return this.languages;
}
And a method which ads one language to existing list.
addLanguageToList(String language){
this.getLanguages().add(language);
}
Do you mean you want to add an initializer for languages? You can use the list.add() method.
public class Country{
private int id;
private String name;
private long population;
private double medianAge;
private ArrayList<String> languages = new ArrayList<String>();
public Country(int id, String name, long population, double medianAge String language) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
this.languages.add(laguage);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
}
Its very simple, Create a getter and two add method, one for adding one language and another add method for adding list of language to existing language list.
import java.util.ArrayList;
import java.util.List;
public class Country {
private int id;
private String name;
private long population;
private double medianAge;
private List<String> languages;
List<String> list = new ArrayList<>();
/**
* Create a Country object with the given properties
*/
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
public List<String> getLanguages() {
return languages;
}
public void addLanguage(String language) {
this.languages.add(language);
}
public void addLanguages(List<String> languages) {
this.languages.addAll(languages);
}
}