Results 1 to 4 of 4
  1. #1
    mickey8945 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Post add() method then averaging it

    Hello I am new to this forum and computer programming is general.
    I hope I posted to the right area after reading the rules.
    I have a question about one area of the program I am working on.
    I have posted copies of 2 of the 3 classes I am working on.
    I am trying to update the variables (name, price) from the Testclass using the public void add(String name, double price) method.
    I have tried a couple of different ways but none work.
    Once I update them my average method should work.
    Could anyone help?
    Thank you

    package Cashier1;

    import java.lang.Math;

    public class Cashier
    {
    public static final double SILVER_VALUE = 0.5;
    public static final double QUARTER_VALUE = 0.25;
    public static final double DIME_VALUE = 0.1;
    public static final double NICKEL_VALUE = 0.05;
    public static final double PENNY_VALUE = 0.01;

    private String name;
    private double price;
    private String name1;
    private double price1;
    private String name2;
    private double price2;
    private String name3;
    private double price3;

    private double average;

    private double purchase;
    private double payment;

    public void add(String name, double price)
    {
    this.name = name;
    this.price = price;
    this.name1 = name;
    this.price1 = price;
    this.name2 = name;
    this.price2 = price;
    this.name3 = name;
    this.price3 = price;
    }
    public void average()
    {
    average = (price + price1 + price2 + price3)/4;

    }
    public double getAverage()
    {
    return average;
    }
    public Cashier()
    {
    purchase = 0;
    payment = 0;
    }
    public void enterPayment(int dollars, int silver, int quarters, int dimes, int nickles, int pennies)
    {
    payment = dollars + silver * SILVER_VALUE + quarters * QUARTER_VALUE
    + dimes * DIME_VALUE + nickles * NICKEL_VALUE + pennies * PENNY_VALUE;
    }
    public double giveChange()
    {
    double change = payment - purchase;
    purchase = 0;
    payment = 0;
    return change;
    }
    public Cashier(String name, double price)
    {
    this.name = name;
    this.price = price;
    }
    public void calculate()
    {

    }
    public void givechange()
    {

    }


    }

    package Cashier1;

    import java.util.Date;
    import java.text.DateFormat;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;


    class TestCashier
    {
    public static void main(String[] arg)
    {
    System.out.println("Joseph's Store to by stuff @");
    System.out.println("Thanks for stopping");
    Date d = new Date();
    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
    System.out.println(df.format(d));

    //String[] names;
    //names = new String[4];

    Cashier c = new Cashier();

    String name = GetData.getWord("Enter name of item");
    double price = GetData.getDouble("Enter price of item");
    c.add(name, price);

    String name1 = GetData.getWord("Enter name of item");
    double price1 = GetData.getDouble("Enter price of item");
    c.add(name1, price1);

    // Add a two more entries of your own
    String name2 = GetData.getWord("Enter name of item");
    double price2 = GetData.getDouble("Enter price of item");
    c.add(name2, price2);

    String name3 = GetData.getWord("Enter name of item");
    double price3 = GetData.getDouble("Enter price of item");
    c.add(name3, price3);

    // Now average the price of the items
    c.average();
    System.out.println(c.getAverage());

    // Make payment
    //double amount = GetData.getDouble("Enter amount of money for payment");

    //c.tendered(amount); // For example twenty dollars were tendered
    //c.makeChange();

    generateReceipt(c);
    }
    static void generateReceipt(Cashier c)
    {
    // Write the necessary code that will generate a customerís receipt.
    // The output must be displayed in a scrollable pane
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: add() method then averaging it

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    mickey8945 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: add() method then averaging it

    I used the code tags is that better?

    Java Code:
    package Cashier1;
    
    import java.lang.Math;
    
    public class Cashier
    {
        public static final double SILVER_VALUE = 0.5;
        public static final double QUARTER_VALUE = 0.25;
        public static final double DIME_VALUE = 0.1;
        public static final double NICKEL_VALUE = 0.05;
        public static final double PENNY_VALUE = 0.01;
        
        private String name;
        private double price;
        private String name1;
        private double price1;
        private String name2;
        private double price2;
        private String name3;
        private double price3;
        
        private double average;
        
        private double purchase;
        private double payment;    
              
        public void add(String name, double price)
        {
            this.name = name;
            this.price = price;
            this.name = name;
            this.price1 = price;
            this.name2 = name;
            this.price2 = price;
            this.name3 = name;
            this.price3 = price;
        }
        public void average()
        {
            average = (price + price1 + price2 + price3)/4;
                            
        }
        public double getAverage()
        {
            return average;
        }
        public Cashier()
        {
            purchase = 0;
            payment = 0;
        }
        public void enterPayment(int dollars, int silver, int quarters, int dimes, int nickles, int pennies)
        {
            payment = dollars + silver * SILVER_VALUE + quarters * QUARTER_VALUE
                    + dimes * DIME_VALUE + nickles * NICKEL_VALUE + pennies * PENNY_VALUE;
        }
        public double giveChange()
        {
            double change = payment - purchase;
            purchase = 0;
            payment = 0;
            return change;
        }
        public Cashier(String name, double price)
        {
            this.name = name;
            this.price = price;
        }
        public void calculate()
        {
            
        }
        public void givechange()
        {
            
        }
    
       
            
        
    }
    Java Code:
    package Cashier1;
    
    import java.util.Date;
    import java.text.DateFormat;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    
    
    class TestCashier
    {
        public static void main(String[] arg)
        {
            System.out.println("Joseph's Store to by stuff @");
            System.out.println("Thanks for stopping");
            Date d = new Date();
            DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
            System.out.println(df.format(d));
            
            //String[] names;
            //names = new String[4];
            
            Cashier c = new Cashier();
    
            String name = GetData.getWord("Enter name of item");
            double price = GetData.getDouble("Enter price of item");
    	c.add(name, price);
    
            String name1 = GetData.getWord("Enter name of item");
            double price1 = GetData.getDouble("Enter price of item");
    	c.add(name1, price1);
            
            // Add a two more entries of your own        
            String name2 = GetData.getWord("Enter name of item");
            double price2 = GetData.getDouble("Enter price of item");
    	c.add(name2, price2);
    
            String name3 = GetData.getWord("Enter name of item");
            double price3 = GetData.getDouble("Enter price of item");
    	c.add(name3, price3);
    
    	// Now average the price of the items
            c.average();
            System.out.println(c.getAverage());
    
            // Make payment
            //double amount = GetData.getDouble("Enter amount of money for payment");
    
            //c.tendered(amount); // For example twenty dollars were tendered
    	//c.makeChange();
    
            generateReceipt(c);	
        }
       static void generateReceipt(Cashier c)
       {
            // Write the necessary code that will generate a customerís receipt.
            // The output must be displayed in a scrollable pane
       }
       
    }

  4. #4
    mickey8945 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: add() method then averaging it

    I think I use a single add method to change 4 different variables.

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  2. Grade averaging program issue!
    By Twixmaster123 in forum New To Java
    Replies: 2
    Last Post: 04-07-2012, 05:44 PM
  3. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  4. Replies: 1
    Last Post: 10-27-2011, 07:16 AM
  5. Replies: 1
    Last Post: 10-17-2011, 01:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •