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!