Reputation: 397
I need a bit of a hand, I'm writing a program that's fundamentally a little shop. I have different items here under the class Content
, which looks like this:
public abstract class Content {
// Content Attributes
private String contentID;
private String contentName;
private double applicationPrice;
private int downloadCount;
//private Comments[] comments = new Comments[100];
//private int commentCount= 0;
// Constructor
public Content(String ID, String Name, double price) {
this.contentID = ID;
this.contentName = Name;
this.applicationPrice= price;
}
public Content(String ID, String Name) {
this.contentID = ID;
this.contentName = Name;
this.applicationPrice= 0;
}
// Accessor Methods
public String getID() {
return contentID;
}
public double getApplicationPrice() {
return applicationPrice;
}
public String getContentName() {
return contentName;
}
public void download() {
//code to make a download happen
//++ download
return;
}
public void addComment() {
//scanner input comment
//this.comments[commentCount]= my new comment;
}
}
Under content I have other classes that extend it such as Application
and Publication
(Publication
is further extended by Book
and Magazine
). As an example Application
looks like this:
import java.util.Scanner;
public class Application extends Content {
// attributes
private String osType;
//constructor
public Application(String ID, String name, double price, String os) {
super(ID, name, price);
this.osType = os;
}
public Application(String ID, String name, String os) {
super(ID, name);
this.osType = os;
}
//accessor methods
public String getOsType() {
return this.osType;
}
}
My issue is that I think there's a problem with my fundamental understanding of inheritance in practice. I want to understand how best I can allow my user to create these objects. So far I have a MyShop
class where they can create an application:
import java.util.Scanner;
public class MyShop {
// instance variables that you need (marked as private)
// declare a private array to store content here
private Content[] contentList = new Content[100];
private int contentCount = 0;
// declare a private array to store users here
public MyShop() {
}
public void addApplication(){
Scanner console = new Scanner(System.in);
String appID;
String appName;
double appPrice;
String appOS;
int control = 0;
while(control== 0) {
try {
System.out.println("What is the ID of the Application?");
appID = console.next();
System.out.println("What is the name of the Application?");
appName = console.next();
System.out.println("What is the price of the Application?");
appPrice = console.nextDouble();
System.out.println("What is the minimum OS of the Application?");
appOS = console.next();
Application newApplication= new Application(appID, appName, appPrice, appOS);
System.out.println(newApplication.getApplicationPrice());
contentList[contentCount] = newApplication;
contentCount++;
console.close();
control = 1;
}catch(Exception e) {
System.out.println("invalid input try again!");
}
}
}
public void showContent() {
System.out.println(contentList);
}
// public void addUser(....) {
// // code here
// }
// space for other possible methods here
}
Is there a way I can use inheritance to avoid having to have a addBook()
and addMagazine()
in addition to addApplication()
and instead have something generic like addContent()
?
Please let me know if my question is unclear or if more information is needed!
Upvotes: 0
Views: 54
Reputation: 114767
Rethink your model, it should match how it works in the real world. Your shop sells items, it's not a manufactorer. So the shop class will never create an application. It will have a pool of sellable things. Products. A stock. And some products are applications, some are books, some are magazines, cars, shampoos, whatever.
Rename Content
to StockItem
. contentId
would be itemId
, contentName
itemName
or just name
. A model is easier (far easier!) to understand if it matches the real world.
Then, add a Stock
class, which has all the items (a List<StockItem> items
for a very simple first approach). The Stock will have some methods, like
public StockItem findById(String stockItemId);
public List<StockItem> findAllByName(String name);
public void addItem(StockItem newItem);
public void itemsSold(String stockItemID, int quantity);
//...
Now if you want to add a book to the stock, simply call
addItem(new Book("Clean Code", "Robert Martin"));
while this book is a stockitem:
public class Publication extends StockItem { ... }
public class Book extends Publication { ... }
Later, you'd learn that this simplified model is not production ready, there are better solutions for real shops, but the message would be: start with a good model. Look at the real world, describe what you see and use exactly that to build your classes and add the methods.
Upvotes: 1
Reputation: 2166
Factory pattern solves exactly this problem.
You can create a method:
<T extends Content> void addContent(Class<T> contentClass) {
if (Application.class.equals(contentClass)) {
...
} else if ...
} else {
throw new RuntimeException("Unknown Content type");
}
}
Or
void addContent(ContentType contentType) {
switch (contentType) {
case APPLICATION:
...
case ...
default:
throw new RuntimeException("Unknown Content type")
}
}
public enum ContentType {
APPLICATION, BOOK, MAGAZINE;
}
Upvotes: 0
Reputation: 924
I think you can make interface with method addContent(int mode)
with 3 integers inside with names APPLICATION, BOOK, MAGAZINE
and then implement it to your myShop
class. According to int mode
you make realization of method addContent.
Upvotes: 0