مثال2 على البرمجة الغرضية التوجه في جافا java object oriented | library books system

  • 2020-12-07

توصيف

مثال بسيط على البرمجة الكائنية في جافا

object oriented example in java

 

class Book {
 private String name;
Author authors[];
 private double price;
 private int qty ;

 public Book(String name,Author authors[],double price,int qty){
this.name=name;
this.authors=authors;
this.price=price;
this.qty=qty;
}
public String getName(){
	return name;
	}
public Author[] getAuthors(){
	return authors;
	}
 public double getPrice(){
	return price;
}
public int getQty(){
	return qty;
}
public void setName(String name){
this.name=name;
	}
  public void setPrice(double price){
this.price=price;
	}
  public void setQty(int qty){
this.qty=qty;
	}

public void setAuthors(Author authors[]){
	this.authors=authors;
}

  public String toString()
  {
    String authors_list="";
    for(int i =0;i<authors.length;i++)
    {
      authors_list=authors_list+"\n"+authors[i].toString();
    }
    return "name: "+name+", price: "+price+", quantity: "+qty+"authors : "+authors_list;
  }
}


 class Author {
 private String name;
 private String email;
 private char gender;
public Author(String name,String email,char gender){
this.name=name;this.email=email;this.gender=gender;
}
public String getName(){
	return name;
	}
public String getEmail(){
	return email;
}
public char getGender(){
	return gender;
}
public void setName(String name){
  this.name=name;
	}
  public void  setEmail(String email){
this.email=email;
}
public void  setGender(char gender){
this.gender=gender;
}
    public String toString(){
  return name+" "+email+" "+gender;
}
 }


class Main {
  public static void main(String[] args) {
    Author author1=new Author(" hind ","hianada@gmail.com",'f');
Author author2=new Author("majeed "," majeed11@htmail.com",'m');
   System.out.println(author1.getName()+"    "+author1.getEmail()+"   "+author1.getGender());
   System.out.println(author2.getName()+"    "+author2.getEmail()+"   "+author2.getGender());
System.out.println(author1.toString());
System.out.println(author2.toString());
 Author a[]=new Author[2];
  a[0]=author1;
  a[1]=author2;

  Book b=new Book("intro to programming",a, 120, 3);
  System.out.println(b.toString());
    
  
  }
}

هل كان المحتوى مفيد؟

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...