[Homework] Getting data from a file to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • miguelguerin
    New Member
    • Mar 2008
    • 7

    [Homework] Getting data from a file to another

    Im having trouble getting the input from one file to another.

    Main file:
    [CODE=java]package assigment3;

    /**
    *
    * @author mguer017
    */
    public class Main {

    /** Creates a new instance of Main */
    public Main() {
    }

    public static void main(String[] args) {
    Cashier c = new Cashier();
    c.add ("Bread", 2.99); //adds string and double
    c.add ("Chicken", 6.79); // adds string and double
    c.add ("Egg", 3.07); // adds string and double
    c.average(); // calculate the average price
    c.tendered(20); // Twenty dollars were tendered

    System.out.prin tln(c); // print Cashier()

    }

    }
    [/CODE]

    Cashier file: (Calculations file)
    [CODE=java]package assigment3;

    /**
    *
    * @author mguer017
    */
    public class Cashier {
    /* Working on it
    private static final double DOLLAR = 1.00;
    private static final double QUARTER = 0.25;
    private static final double DIME = 0.10;
    private static final double NICKEL = 0.05;
    private static final double PENNY = 0.01;
    */
    private String Item;
    private int Total_Items, Tendered;
    private double Price, average, Total;

    /** Creates a new instance of Cashier */


    public Cashier() {
    System.out.prin tln(getItem() + " ......... $" + getPrice() + "\n" + "----------" + "\n" + getTotal());
    System.out.prin tln("Amount tendered: $" + getTendered());
    System.out.prin tln("The change is: $" + getChange());
    System.out.prin tln("There were " + getTotal_Items( ) + " items");
    System.out.prin tln("Average price is: $" + average);
    System.out.prin tln("Your change: $" + getChange());


    }
    public void add(String It, double Prc) {
    this.Item = It;
    this.Price = Prc;

    }

    public void tendered(int Amount) {
    Tendered = Amount;

    }

    public double average() {
    return average = Total_Items / Total;

    }

    public String getItem() {
    return Item;
    }

    public double getPrice() {
    return Price;
    }

    public double getTotal() {
    return Total = Total + Price;
    }

    public double getTendered() {
    return Tendered;
    }

    public int getTotal_Items( ) {
    return Total_Items;
    }

    public double getChange() {
    return Tendered - Total;
    }

    }
    [/CODE]
    The output comes with 0's and null on the strings.
    Code:
    null ......... $0.0
    ----------
    0.0
    Amount tendered: $0.0
    The change is: $0.0
    There were 0 items
    Average price is: $0.0
    Your change: $0.0
    Any idea what I am doing wrong?
    The assignment gave me the code of the main file, so I only need to mess with the Cashier file.
    Thanks,
    Miguel Guerin
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Consider two lines of your code:

    [CODE=Java]Cashier c = new Cashier();
    c.add ("Bread", 2.99); //adds string and double[/CODE]

    Before you can get to the second line, you must execute the first, which calls:

    [CODE=Java]public Cashier() {
    System.out.prin tln(getItem() + " ......... $" + getPrice() + "\n" + "----------" + "\n" + getTotal());
    System.out.prin tln("Amount tendered: $" + getTendered());
    System.out.prin tln("The change is: $" + getChange());
    System.out.prin tln("There were " + getTotal_Items( ) + " items");
    System.out.prin tln("Average price is: $" + average);
    System.out.prin tln("Your change: $" + getChange());
    }[/CODE]

    Clearly, the constructor is the last place to do that!

    Comment

    • miguelguerin
      New Member
      • Mar 2008
      • 7

      #3
      Yes, I was thinking on calling each one on the main file, but the assignment gave me the code for the main file.
      [CODE=java]... Here is a typical test class.

      class TestCashier
      {
      public static void main(String[] arg)
      {
      Cashier c = new Cashier();
      c.add("Bread", 2.99);
      c.add ("Chicken", 6.79);
      c.add("Egg", 3.07);
      c.average();
      c.tendered(20); // Tenty dollars were tendered
      System.out.prin tln(c);
      }
      } ...[/CODE]
      Is there a way to do it this way, or the professor is messing with us =p
      This is my first semester in java so I don't have a lot of experience with the language.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        What do you think the last line should accomplish:

        [CODE=Java]System.out.prin tln(c); [/CODE]

        Comment

        • miguelguerin
          New Member
          • Mar 2008
          • 7

          #5
          Sorry I don't get what you mean with that. I believe that is calling the Cashier() from the Cashier.java file because I create a new Cashier() with the variable "c".
          As I stated in my last post I do not have a lot of experience.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by miguelguerin
            Sorry I don't get what you mean with that. I believe that is calling the Cashier() from the Cashier.java file because I create a new Cashier() with the variable "c".
            As I stated in my last post I do not have a lot of experience.
            I appreciate you don't have much experience, but if you don't know what a line of code does, and you are just typing it in because your instructor told you to do so, you've crossed the line that separates logic from voodoo.

            That statement causes Cashier's toString method to be called. Have you discussed toString methods?

            Comment

            • miguelguerin
              New Member
              • Mar 2008
              • 7

              #7
              When I add this line:
              [CODE=java]public String toString() {
              return Item + "........ $" + Price;
              }[/CODE]
              It only shows the last input. Egg ..... $3.07
              and on "toString() " it warns me about a "Add@overri de annotation" What does this means?

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by miguelguerin
                When I add this line:
                [CODE=java]public String toString() {
                return Item + "........ $" + Price;
                }[/CODE]
                It only shows the last input. Egg ..... $3.07
                and on "toString() " it warns me about a "Add@overri de annotation" What does this means?
                You compiler is suggesting you do this:

                [CODE=Java] @Override
                public String toString() {
                ...
                }[/CODE]
                That @Override is an example of an annotation. It indicates that you are attempting to override a method. A compiler warning is just that, a warning and not an error, but the compiler has your best interests at heart. When you learn about annotations you'll see this is a safety function.

                More on annotations: http://java.sun.com/j2se/1.5.0/docs/...notations.html

                Comment

                • miguelguerin
                  New Member
                  • Mar 2008
                  • 7

                  #9
                  Any idea on why is only showing the last input? I been reading about the toString() but I still have problems getting the concept.

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by miguelguerin
                    Any idea on why is only showing the last input? I been reading about the toString() but I still have problems getting the concept.
                    If your code hasn't changed much from the initial post, fields item and price can only record what was passed by the last call to method add. If you want Cashier to store multiple items and their prices, you are going to write more code: to define a class that represents an order item and store instances of it in an array or collection within Cashier.

                    Comment

                    • miguelguerin
                      New Member
                      • Mar 2008
                      • 7

                      #11
                      Ok, we haven't study arrays yet, and I have no clue what do you mean with collection. This is an update of the code:
                      [CODE=java]package assigment3;

                      /**
                      *
                      * @author mguer017
                      */
                      public class Cashier {
                      /* Working on it
                      private static final double DOLLAR = 1.00;
                      private static final double QUARTER = 0.25;
                      private static final double DIME = 0.10;
                      private static final double NICKEL = 0.05;
                      private static final double PENNY = 0.01;
                      */
                      private String Item;
                      private int Total_Items, Tendered;
                      private double Price, average, Total;

                      /** Creates a new instance of Cashier */


                      public Cashier() {

                      }

                      public void add(String It, double Prc) {
                      this.Item = It;
                      this.Price = Prc;

                      }
                      @Override
                      public String toString() {
                      return Item + "........ $" + Price + "\n" + "-------------" + "\n" + getTotal() + "\n Amount tendered: $" + getTendered() + "\n The change is: $"+ getChange() + "\n There were " + getTotal_Items( ) + " items \n Average price is: $" + average + "\n Your change: $" + getChange();
                      }

                      public void tendered(int Amount) {
                      Tendered = Amount;

                      }

                      public double average() {
                      return average = Total / Total_Items;

                      }

                      public String getItem() {
                      return Item;
                      }

                      public double getPrice() {
                      return Price;
                      }

                      public double getTotal() {
                      return Total = Total + Price;
                      }

                      public double getTendered() {
                      return Tendered;
                      }

                      public int getTotal_Items( ) {
                      return Total_Items;
                      }

                      public double getChange() {
                      return Tendered - Total;
                      }

                      }[/CODE]
                      The output now look like this:
                      Code:
                      Egg........ $3.07
                      -------------
                      3.07
                       Amount tendered: $20.0
                       The change is: $16.93
                       There were 0 items 
                       Average price is: $NaN
                       Your change: $16.93
                      Any hint on what I should look into for the modification?

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        My hint is try to understand your code! If you never assign anything to a numeric field, it will be zero. What did you expect?

                        Comment

                        • miguelguerin
                          New Member
                          • Mar 2008
                          • 7

                          #13
                          hmm ok thanks for the help.

                          Comment

                          Working...