Hi I could use some help with one of my java programing assigments here is what
ID: 3785460 • Letter: H
Question
Hi I could use some help with one of my java programing assigments here is what I have to do.
A retail store has a preferred customer plan where customers can earn discounts on all
their purchases. The amount of a customer’s discount is determined by the amount of the
customer’s cumulative purchases in the store, as follows:
When a preferred customer spends $500, he or she gets a 5% discount on all future
purchases.
When a preferred customer spends $1,000, he or she gets a 6% discount on all future
purchases.
When a preferred customer spends $1,500, he or she gets a 7% discount on all future
purchases.
When a preferred customer spends $2,000 or more, he or she gets a 10% discount on
all future purchases.
Design a class named PreferredCustomer , which inherits from the Customer class you created
in Programming Challenge 7. The PreferredCustomer class should have fields for
the amount of the customer’s purchases and the customer’s discount level. Write one or
more constructors and the appropriate mutator and accessor methods for the class’s fields.
Demonstrate the class in a simple program.
I have been working on it and so far I have this done and it's giving me some errors when I tried making a second extension to one of my classes. To be more specific the errors start when you get to the code that contains ---> public class PreferredCustomer extends Customer <--- Any help would be greatly appreciated.
public class Person
{
String name;
String address;
String telephoneNumber;
//creates a constructor method
public Person (String name, String address, String telephoneNumber)
{
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
}
//sets the name
public void setName(String name)
{
this.name = name;
}
//sets the address
public void setAddress(String address)
{
this.address = address;
}
//sets the telephone number
public void telephoneNumber(String telephoneNumber)
{
this.telephoneNumber = telephoneNumber;
}
//gets the name
public String getName()
{
return name;
}
//gets the address
public String getAddress()
{
return address;
}
//gets the phone number
public String getPhonenumber()
{
return telephoneNumber;
}
public String toString()
{
String person = "Name : "+ getName() + " Address : " +
getAddress() + " PhoneNumber : " + getPhonenumber();
return person;
}
public class Customer extends Person
{
int customerNumber;
boolean mailingList;
//creates a constuctor method
public Customer(String name, String address, String telephoneNumber,
int customerNumber, boolean mailingList)
{
super(name, address, telephoneNumber);
this.customerNumber = customerNumber;
this.mailingList = mailingList;
}
//sets the customer number
public void setCustomerNumber(int customerNumber)
{
this.customerNumber = customerNumber;
}
//gets the mail preference
public void setMailOnOff(boolean mailingList)
{
this.mailingList = mailingList;
}
//returns the mailing list
public boolean getMailOnOff()
{
return mailingList;
}
//gets the customer's number
public int getCustomerNumber()
{
return customerNumber;
}
public String toString()
{
String customer = super.toString() +
" Customer Number : " + getCustomerNumber() +
" Customer MailList : " + getMailOnOff();
return customer;
}
public class PreferredCustomer extends Customer
{
double purchase;
double discount;
}
public PreferredCustomer(String name, String address, String telephoneNumber,
int customerNumber, boolean mailingList, double purchase)
{
super(name,address,telephoneNumber, customerNumber,mailingList);
this.purchase = purchase;
if (purchase == 500)
setDiscount(5);
else if (pruchase == 1000)
{
setDiscount(6);
}
else if (purchase == 1500)
{
setDiscount(7);
}
else if (purchase >= 2000)
{
setDiscount(10);
}
public void setPurchase(double purchase)
{
this.purchase = purchase;
}
private void setDiscount(double discount)
{
this.discount = discount;
}
public double getPurchase()
{
return purchase;
}
public double getDiscount()
{
return discount;
}
public String toString()
{
String preferredCustomer = super.toString() +
" Purchase " + getPurchase() +
" Discount " + getDiscount();
return preferredCustomer;
}
}
}
}
Explanation / Answer
In Java One Program can have only one Public Class.
focus on brackets {}
Here is the answer:
class Person
{
String name;
String address;
String telephoneNumber;
//creates a constructor method
public Person (String name, String address, String telephoneNumber)
{
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
}
//sets the name
public void setName(String name)
{
this.name = name;
}
//sets the address
public void setAddress(String address)
{
this.address = address;
}
//sets the telephone number
public void telephoneNumber(String telephoneNumber)
{
this.telephoneNumber = telephoneNumber;
}
//gets the name
public String getName()
{
return name;
}
//gets the address
public String getAddress()
{
return address;
}
//gets the phone number
public String getPhonenumber()
{
return telephoneNumber;
}
public String toString()
{
String person = "Name : "+ getName() + " Address : " +
getAddress() + " PhoneNumber : " + getPhonenumber();
return person;
}
}
class Customer extends Person
{
int customerNumber;
boolean mailingList;
//creates a constuctor method
public Customer(String name, String address, String telephoneNumber,
int customerNumber, boolean mailingList)
{
super(name, address, telephoneNumber);
this.customerNumber = customerNumber;
this.mailingList = mailingList;
}
//sets the customer number
public void setCustomerNumber(int customerNumber)
{
this.customerNumber = customerNumber;
}
//gets the mail preference
public void setMailOnOff(boolean mailingList)
{
this.mailingList = mailingList;
}
//returns the mailing list
public boolean getMailOnOff()
{
return mailingList;
}
//gets the customer's number
public int getCustomerNumber()
{
return customerNumber;
}
public String toString()
{
String customer = super.toString() +
" Customer Number : " + getCustomerNumber() +
" Customer MailList : " + getMailOnOff();
return customer;
}
}
public class PreferredCustomer extends Customer
{
double purchase;
double discount;
public PreferredCustomer(String name, String address, String telephoneNumber,
int customerNumber, boolean mailingList, double purchase)
{
super(name,address,telephoneNumber, customerNumber,mailingList);
this.purchase = purchase;
if (purchase >= 500)
setDiscount(5);
else if (pruchase >= 1000)
{
setDiscount(6);
}
else if (purchase >= 1500)
{
setDiscount(7);
}
else if (purchase >= 2000)
{
setDiscount(10);
}
}
public PreferredCustomer(String name, String address, String telephoneNumber,
int customerNumber, boolean mailingList)
{
super(name,address,telephoneNumber, customerNumber,mailingList);
}
public void setPurchase(double purchase)
{
this.purchase = purchase;
if (purchase >= 500)
setDiscount(5);
else if (pruchase >= 1000)
{
setDiscount(6);
}
else if (purchase >= 1500)
{
setDiscount(7);
}
else if (purchase >= 2000)
{
setDiscount(10);
}
}
public double getPurchase()
{
return purchase;
}
public double getDiscount()
{
return discount;
}
public String toString()
{
String preferredCustomer = super.toString() +
" Purchase " + getPurchase() +
" Discount " + getDiscount();
return preferredCustomer;
}
}