Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Interface and abstract class problem

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Interface and abstract class problem

    Hey guys, so I am trying to learn how to use interfaces abstract classes , and I've ran into a problem. So this is my code

     
    interface Article {
       public String getName();
       public double getPrice();
       public String getDescription();
     
    }
     
    abstract class AbstractArticle implements Article {
    	final private String name;
    	final private double price;
    	final private String description;
     
    	AbstractArticle(String name,double price,String description) {
    		this.name = name;
    		this.price = price;
    		this.description = description;		
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public double getPrice() {
    		return price;
    	}
     
    	public String getDescription() {
    		return description;
    	}
    }
     
    class Accessory extends AbstractArticle {
    	final String instructionsForUse;
     
    	Accessory(String name, double price, String description, String instructionsForUse) {
    		super(name,price,description);
    		this.instructionsForUse = instructionsForUse;
    	}
     
    	public String getinstructionsForUse() {
    		return instructionsForUse;
    	}
    }
     
    class Merchandise implements Article {
    	final private int quantity;
    	Article article;
     
    	Merchandise(Article article) {
    		this.article = article;
    		quantity = 0;	
    	}
     
    	Merchandise(Article article, int quantity) {
    		this.article = article;
    		this.quantity = quantity;
    	}
     
    	public int getQuantity() {
    		return quantity;
    	}
     
    	public Article getArticle() {
    		return article;
    	}
     
     
    }
    So as you can see I have this interface Called article and i implement it in an abstract class,inherit the methods from the interface, use the abstract class to write another concrete class (Accessory) in which i use the super key word to call the methods from the abstract class(name,description,price). Now In this Merchandise class I'm having an issue. So according to the graph it extends from the Interface Article as well, and in the class its self i need to implement a final field (that is quantity) and also two constructors (Article article) to set the starting value of quantity to 0 (hopefully I did that right) and a second one to initialize the parameters. And i need to use getters to get the values of quantity and article. Now I've done that but when i try to compile the class I get this error that says i need to override the method getDescription or make the class Merchandise abstract. How do i solve this. I can't make the class abstract beacuse that is against the rules of the assignment, but rewrting those methods bring me nothing.Is there any way to avoid this? Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Interface and abstract class problem

    error that says i need to override the method getDescription or make the class Merchandise abstract.
    That message seems clear enough. Did you put that method in that class?

    Note: The code should annotate where it is doing an override by using the @Override statement just before the method declaration that is being implemented.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Interface and abstract class problem

    Is there any way to avoid putting all of those methods in that class,because if i put the getDescription() method I need to put the other 2 as well?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Interface and abstract class problem

    way to avoid putting all of those methods
    No.
    Why did that class implement the interface if it is not going to implement the needed methods?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Interface and abstract class problem

    Well the diagram says to do so, okay thanks I'll see what i can figure out

  6. #6
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Interface and abstract class problem

    Hey folks! So after a few days of coding I have done finished this program partially. What I mean by that is the following:

     
    public static Warehouse fromCsv(String file) {
    		In.open(file);
    		String material = In.readFile();
    		In.close();
     
    		String lines[] = material.split("\n");
            Merchandise[] merchandise = new Merchandise[lines.length];
    		for(int i=0; i<merchandise.length; i++) {
    			String[] parts = lines[i].split(",");
     
     
    		Article article = null;
    		if(parts[0].equals("instrument")) {
    			article = new Instrument(parts[1],Double.parseDouble(parts[2]),parts[3],Integer.parseInt(parts[4]));
    		}else{
    			article = new Accessory(parts[1],Double.parseDouble(parts[2]),parts[3],parts[4]);
    		}
    		merchandise[i] = new Merchandise(article,Integer.parseInt(parts[5]));
    		}
    		return new Warehouse(merchandise);
    	}
     
    	public Merchandise[] getMerchandise() {
    		return merchandise.clone();
    	}
     
    	public Merchandise findMerchandise(String name) {
    		Merchandise found = null;
    		for(Merchandise the : merchandise) {
    			if(the.getArticle().getName().equals(name)) {
    				found = the;
    				break;
    			}
    		}
     
    class Shop {
    	private final Warehouse warehouse;
     
    	Shop(Warehouse warehouse){
    		this.warehouse = warehouse;
    	}
     
    	public void printCatalog() {
    		Out.println();
    		Out.formatln("%-30s%20s%10s", "Name" , "Price","Quantity");
    		String format = "%-30s%20.2f%10d"; 
    		for(Merchandise the : warehouse.getMerchandise()) {
    		 Out.formatln(format,the.getArticle().getName(),the.getArticle().getPrice(),the.getQuantity());
    		 }  
    	} 
     
    	public void printProductDescription(String name){
    		Merchandise merchandise = warehouse.findMerchandise(name);
     
    		 Out.println();
    		 if (merchandise != null) { 
    		 Out.println(merchandise); 
    		 } else { 
    		 Out.println("Article does not exist"); 
    		 } 
    	 }
     
    class Main {
    	public static void main(String[] args) {
    		Warehouse warehouse = Warehouse.fromCsv("warehouse.csv");
    		Shop shop = new Shop(warehouse);
     
    		while(true) {
    			Out.println("Pianos & more");
    			Out.println("0. Print Catalogue");
    			Out.println("1. Search Article");
    			Out.println();
    			Out.print("Selection:");
    			int selection = In.readInt();
    			In.read();
     
     
    			switch(selection) {
    				case 0:
    				shop.printCatalog();
    				break;
    				case 1:
    				Out.print("Article name:");
    				String name = In.readLine();
    				shop.printProductDescription(name);
    				break;
    				default:
    				Out.println("Invalid selection");
    			}
    		}
    	}
    }
    Now the the part of the Program where I need to print out the catalog is working just fine, but the part of the code where I need to search an article isn't working, after I input the article name it gives me the "article doesnt exist message" and after another try it just print out the catalog a few times. What could be causing this?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Interface and abstract class problem

    How are you trying to debug the code? Add some print statements in the method(s) that are not working correctly that print out the values of the variables that they are using so you can see what the computer sees when the program executes.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Interface and abstract class problem

    Well, to be honest I dont even know what debuggin is, we haven't covered that part yet.And about the print statements I will try thanks for the idea.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Interface and abstract class problem

    From Google:
    debugging
    /ˌdēˈbəɡiNG/

    the process of identifying and removing errors from computer hardware or software.
    "software debugging"
    Adding print statements is one way to do it. Another is to use the interactive debugger that many IDEs have.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Interface and abstract class problem

    Hey just to update on the thread,I have resolved the input issue by using In.readString() and putting the article name under " " in the CMD.

Similar Threads

  1. Difference between interface and abstract class?
    By sreekanthgude in forum Java Theory & Questions
    Replies: 7
    Last Post: December 7th, 2018, 12:52 AM
  2. Replies: 0
    Last Post: July 21st, 2018, 01:25 AM
  3. Java Abstract class and Interface issues in a code
    By John234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 09:51 PM
  4. abstract VS interface
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2012, 01:07 PM
  5. [SOLVED] Sahaja here. Need guidance. Interface and Abstract class
    By sahaja in forum Java Theory & Questions
    Replies: 2
    Last Post: July 4th, 2012, 04:43 PM