2 Attachment(s)
Need help with Enums and setting costs
Hello everyone, first i want to thank anyone who will help and guide me through this problem I am having.
- My first issue is with the "enum CustomerType", in my Customer.java class I am required to write this
Code :
public enum CustomerType {SILVER, GOLD, PLATINUM};
private CustomerType cType = CustomerType.SILVER;
I understand the above part, but I am confused on the first requirement. The first task states
"Method createInvoice generates a string that is printed in a JOptionPane dialog box. If the customer is type SILVER, the base cost is $25. If the customer is type GOLD, the base cost is $30. If the customer is type PLATINUM, the base cost is $35. createInvoice calls calculateCharge to add the video and test cost to the base cost."
I do not know as of where or how to even add the base cost to the enum types. createInvoice is in my Invoice.java class and invoice is implemented by my Lesson.java class as you'll see in my Lesson.java class. (Ill provide the UML for better understanding)
My second issue is after applying the base cost to the enum customerType, the requirement states "calculateCharge charges $1 for each video if customer type is SILVER and $.50 for each test.
If the customer is type GOLD, each video is $.50 and each test is $.25. There is no extra charge
if the customer is type PLATINUM.
ArrayList lessonList keeps a list of all vides watched and tests taken by the customer. "
Then my 3rd requirement is to write a test case called CustomerTest this is the requirement for CustomerTest.java class
"Write test case, CustomerTest, that creates two customers with the data given. The customers are
kept in ArrayList, customerList. Then, using an enhanced for loop, polymorphically walk through
the customerList and create the invoice for each customer. Print all customer’s charges in a
dialog box as shown."
below are my java classes in UML order:
Address class
Code :
public class Address
{
private String street;
private String city;
private String state;
private int zip;
public Address()
{
setStreet ("");
setCity ("");
setState ("");
setZip (0);
}
public Address ( String s, String c, String st, int z)
{
setStreet ( s );
setCity ( c );
setState ( st );
setZip ( z );
}
public void setStreet (String s)
{
street = s;
}
public String getStreet ()
{
return street;
}
public void setCity (String c)
{
city = c;
}
public String getCity ()
{
return city;
}
public void setState (String st)
{
state = st;
}
public String getState ()
{
return state;
}
public void setZip ( int z )
{
zip = z;
}
public int getZip ()
{
return zip;
}
public String toString ()
{
return ( "City " + city + " State " + state + " ZIP " + zip);
}
}
Invoice class
Code :
public interface Invoice
{
public String createInvoice();
}
Customer class
Code :
import java.util.ArrayList;
public class Customer
{
private String name;
private Address address;
private int accountNumber;
public enum CustomerType {SILVER, GOLD, PLATINUM};
private CustomerType cType = CustomerType.SILVER;
private ArrayList <Lesson> lessonList = new ArrayList <Lesson>();
public Customer()
{
setName("");
setAddress (new Address());
setAccountNumber(0);
}
public Customer (String n, Address a, int num)
{
setName(n);
setAddress(a);
setAccountNumber(num);
}
public void setName (String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setAddress (Address a)
{
address = a;
}
public Address getAddress()
{
return address;
}
public void setAccountNumber (int num)
{
accountNumber = num;
}
public int getAccountNumber()
{
return accountNumber;
}
public void setCType (CustomerType c)
{
cType = c;
}
public CustomerType getCType()
{
return cType;
}
public void addLesson (Lesson l)
{
lessonList.add(l);
}
Lesson Class
Code :
import java.util.ArrayList;
public abstract class Lesson implements Invoice
{
private int chapter;
private String topic;
public Lesson()
{
setChapter(0);
setTopic("");
}
public Lesson(int c, String t)
{
setChapter( c );
setTopic (t);
}
public void setChapter (int c)
{
chapter = c;
}
public int getChapter ()
{
return chapter;
}
public void setTopic (String t)
{
topic = t;
}
public String getTopic()
{
return topic;
}
public abstract Double calculateCharge(Customer.CustomerType c);
public String toString()
{
return ( "Chapter " + chapter + " Topic " + topic);
}
}
Test class
Code :
import java.util.ArrayList;
public class Test extends Lesson
{
private int grade;
public Test ()
{
super();
setGrade(0);
}
public Test (int c, String t, int g)
{
super(c, t);
setGrade(g);
}
public int getGrade ()
{
return grade;
}
public void setGrade (int g)
{
grade=g;
}
public String toString()
{
return (super.toString() + "Grade " + grade);
}
public String calculateCharge()
}
Video class
Code :
import java.util.ArrayList;
public class Video extends Lesson
{
private int number;
public Video()
{
super();
setNumber(0);
}
public Video (int c, String t, int n)
{
super(c, t);
setNumber(n);
}
public void setNumber(int n)
{
number = n;
}
public int getNumber()
{
return number;
}
public String toString ()
{
return (super.toString() + " Number " + number);
}
public String calculateCharge()
}
CustomerTest class
Code :
import java.util.ArrayList;
import javax.swing.*;
public class CustomerTest
{
public static void main (String args[])
{
String message="";
Customer c1 = new Customer ("Jones", new Address("Cooper","Arlington", "Texas", 76019), 12345);
Customer c2 = new Customer ("Smith", new Address("Bowen","Arlington", "Texas", 76006), 65489);
c1.setCType(Customer.customerType.GOLD);
c2.setCType(Customer.customerType.SILVER);
Video v1 = new Video ( 1, "Introduction", 10);
Video v2 = new Video ( 2, "Programming", 20);
Test t1 = new Test ( 1, "Introduction", 85);
Test t2 = new Test ( 2, "Programming", 96);
v1.addCustomer(c1);
v2.addCustomer(c2);
ArrayList <Lesson> lessonList = new ArrayList <Lesson>();
LessonList.add(c1);
LessonList.add(c2);
for (Customer c: customerList)
{
message += c.createInvoice() +"\n" + c.calculateCharge() + "\n";
JOptionPane.showMessageDialog(null, message);
}
}
}
I will attach a picture of the UML
AND THIS IS HOW THE OUTPUT SHOULD LOOK LIKE AT THE END The Customer information and lists will print out to the prompt. All customers' charges will print to JOptionPane dialog box.
Attachment 1408Attachment 1409
Re: Need help with Enums and setting costs
You can put some attributes into your enums ... for example.
Code Java:
public enum CustomerType {
SILVER(25d, 1d), GOLD(30d, 0.50d), PLATINUM(35d, 0.25d);
private double baseCost;
private double charge;
private CustomerType(double baseCost, double charge) {
this.baseCost = baseCost;
this.charge = charge;
}
public double getBaseCost() {
return baseCost;
}
public double getCharge() {
return charge;
}
}
I think this will solve your first 2 problems. You just need to code something like this
customer.getCustomerType().getBaseCost() or customer.getCustomerType().getCharge()
Hope it helps