Question

So I have code set up such that I have a subclass 'PBill' as an inherited extension of a class 'customer'. However, when I try to create a new PBill object in my main function, it says that no such object exists, and it can't figure out what to do. Here is my example:

public class customer {
private int reg;
private int prem;
private int raw;
private int total;
public customer(int re,int pr, int ra){
    this.reg=re;
    this.prem=pr;
    this.raw=ra;
    this.total=re+pr+ra;
}
public customer(int re){
    this(re,0,0);
}
public customer(int re,int pr){
    this(re,pr,0);
}       
public int totalBag(){
    return(reg);
}
public double calctot(){
    if(this.reg>10){
        reg+=1;
    }
    double totcost=reg*10+prem*15+raw*25;
    return(totcost);
}
public String printBill(){
    return("You bought "+reg+" bags of regular food, "+prem+" bags of premium food, and "+raw+" bags of raw food. If you bought more than 10 bags of regular food, you get one free bag! Your total cost is: $"+this.calctot()+".");
}
class PBill extends customer{
public PBill(int re, int pr, int ra){
    super(re, pr, ra);
}
public PBill(int re, int pr){
    super(re,pr);
}
public PBill (int re){
    super(re);
}
public double calcTot(){
    return(super.calctot()*.88);
}
public String printPBill(){
    return("You are one of our valued Premium Customers! We appreciate your continued business. With your 12% discount, your total price is: $"+this.calcTot()+".");
}
}

The error message occurs when I try to call it in another class with a main object to create a new object, like this:

public static void main(String[] args){
PBill c1=new PBill(10,2);

Which is where it gives me the error that PBill can not be resolved to a type.

So how would I go about creating a new PBill object so that I can access the methods inside of it, and is there an easier way to define object inheritance?

Était-ce utile?

La solution

PBill is the inner class of customer, if you want to instantiate a instance of PBill, you can move the definition of PBill out of customer, be sure not to add public.
If you don't prefer to that, you can still create a PBill instance by

customer c = new customer(1);
customer.PBill b = c.new PBill(1);

Autres conseils

Each Java class should be in a file of its own, with a name that's the same as the class name: customer on customer.java, PBill on PBill.java.

Adhere to conventions: class names should start with upper case letters ("Customer").

Using inheritance to link rather unrelated concepts is not best practice. A bill is one entity, and a customer is another one. There may, of course, various categories of customers where inheritance is applicable.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top