Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with Enums and setting costs

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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
    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
    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

     public interface Invoice
    {
    	public String createInvoice();
    }

    Customer class
     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
     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
     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

     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
    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.


    Screen Shot 2012-09-05 at 2.25.55 PM.jpgScreen Shot 2012-09-05 at 2.28.07 PM.jpg
    Last edited by Charlie.beat; September 5th, 2012 at 02:33 PM.


  2. #2
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Need help with Enums and setting costs

    You can put some attributes into your enums ... for example.

     
     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
    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseņo Web, Desarrollo de Software, SEO,
    LinkedIn

  3. The Following User Says Thank You to psabbate For This Useful Post:

    Charlie.beat (September 5th, 2012)

Similar Threads

  1. enums..
    By imsuperman05 in forum Object Oriented Programming
    Replies: 3
    Last Post: December 29th, 2011, 10:37 PM
  2. CONVERT ENUMS TO GET METHOD??
    By Me360 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2011, 07:56 PM
  3. How To Use Enums
    By Json in forum Java Programming Tutorials
    Replies: 6
    Last Post: October 16th, 2009, 04:17 PM
  4. How To Use Enums
    By Json in forum Java Code Snippets and Tutorials
    Replies: 6
    Last Post: October 16th, 2009, 04:17 PM
  5. switch with Enums
    By chronoz13 in forum Loops & Control Statements
    Replies: 17
    Last Post: October 8th, 2009, 08:08 PM