I've a mother class livre.
Bd & album are two extended classes from Livre
I've a problem on the main(), I can't initiate & declare an object (myAlb) from the class album: here is what i did:
album[] myAlb;
myAlb= new album[nbr_of_albums];
myAlb[i] = album(1,5,"author","title"); // for an album i I call the constructor of album= error
here is the error: No enclosing instance of type livre is accessible. Must qualify the allocation with an enclosing instance of type livre (e.g. x.new A() where x is an instance of livre).
here is my full code source:
import java.util.*;
public class livre {
public abstract class book {
String titre;
String auteur;
float prix;
int nbr_pages;
book(String titre,String auteur, float prix,int nbr_pages){
this.titre = titre;
this.auteur = auteur;
this.prix = prix;
this.nbr_pages = nbr_pages;
}
abstract void affichage();
}
public class bd extends book {
String couleur;
bd(String titre,String auteur, float prix,int nbr_pages,String couleur){
super(titre,auteur,prix,nbr_pages);
this.couleur = couleur;
}
void affichage(){
System.out.println("\n\nbook:"+titre);
System.out.println("+ auteur"+auteur);
System.out.println("+ prix"+prix);
System.out.println("+ nbr_pages"+nbr_pages);
System.out.println("+ "+couleur);
}
}
public final class album extends book {
String [] couleur;
void changerCouleur(){
int nbr = 0;
System.out.print("Plz set the nbr of the page that you want to color: ");
Scanner sc = new Scanner(System.in);
while (!(nbr<= nbr_pages && nbr > 0 )){ nbr = sc.nextInt();}
System.out.print("Plz set what color u wanna colorate this page: ");
couleur[nbr] = sc.nextLine();
sc.close();
}
void affichage(){
System.out.println("\t\t book:"+titre);
System.out.println("+ auteur"+auteur);
System.out.println("+ prix"+prix);
System.out.println("+ nbr_pages"+nbr_pages);
System.out.println("+ couleurs des pages: ");
for(int i=0;i<nbr_pages;i++) System.out.println(" =>Page["+i+"]= "+couleur[i]);
}
album(String titre,String auteur, float prix,int nbr_pages){
super(titre,auteur,prix,nbr_pages);
couleur = new String[nbr_pages];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
album[] myAlb; bd[] myBd;
Scanner sc = new Scanner(System.in);
System.out.print("Set the nbr of Albums that you want to make: ");
int nbrAlbum = sc.nextInt();
myAlb= new album[nbrAlbum];
System.out.print("Set the nbr of BD that you want to make: ");
int nbrBd = sc.nextInt();
myBd= new bd[nbrBd];
for(int i=0;i<nbrAlbum;i++){
System.out.print("\tAlbum nbr "+i+": ");
System.out.print("=>titre = ");
String titre = sc.nextLine();
System.out.print("=>auteur = ");
String auteur = sc.nextLine();
System.out.print("=>prix = ");
float prix = sc.nextFloat();
System.out.print("=>nbr de Pages = ");
int nbr_pages = sc.nextInt();
myAlb[i] = new album(titre,auteur,prix,nbr_pages);
}
}
}