Results 1 to 6 of 6
  1. #1
    Xinc is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Array with multiple variables - Problem with input

    Hello.

    I am creating a program to have an array holding variables of type DiscountSale and then outputs the data in each index of the array. However, for some reason the last input into the array overwrites every other input of the array. I'm not sure what to do. Please help if you can. Here is the code:

    import java.util.Scanner;

    public class MultiItemSale {

    Scanner keyboard = new Scanner(System.in);
    private int column;
    private int index;
    private double totalBill;
    DiscountSale sale = new DiscountSale();
    DiscountSale[] a;

    public MultiItemSale() {
    column = 0;
    a = new DiscountSale[0];
    }

    public void welcomeBanner() {
    System.out.println("Welcome to the Sale program.");
    System.out.println("This program will let you input data of items"
    + " you are buying, and then outputs the list of items "
    + "along with the sum total.\n");
    }

    public void fillArray() {

    int number = 1;

    System.out.println("How many items would you like to buy?\n");
    System.out.print("Number of items: ");

    this.column = keyboard.nextInt();
    System.out.println();

    this.a = new DiscountSale[column];

    for (index = 0; index < a.length; index++) {
    System.out.println("Item " + number + ":");
    sale.setName();
    sale.setPrice();
    sale.setDiscount();
    a[index] = sale;
    number++;
    }
    }

    public void process() {

    for (index = 0; index < a.length; index++) {

    this.totalBill += a[index].bill();
    }
    }

    public void output() {

    int num = 1;

    for (index = 0; index < a.length; index++) {

    System.out.println("Item " + num + ": " + a[index]);
    num++;
    }

    System.out.println("Total item(s): " + a.length);
    System.out.print("Total cost: ");
    System.out.printf("%3.2f", totalBill);
    System.out.println("\n");

    }

    }
    --------------------------------------------------------------------------
    package sale9;

    import java.util.Scanner;

    public class Sale {

    Scanner keyboard = new Scanner(System.in);
    private String name;
    private double price;

    public Sale() {
    name = "No name yet";
    price = 0;
    }

    public void setName() {

    System.out.print("Enter the name of the object: ");
    this.name = keyboard.next();
    System.out.println();
    }

    public String getName() {
    return name;
    }

    public void setPrice() {

    System.out.print("Enter the price of the object: ");
    this.price = keyboard.nextDouble();
    System.out.println();
    }

    public double getPrice() {
    return price;
    }

    @Override
    public String toString() {
    return (name + " Price and total cost = $" + price);
    }

    public double bill() {
    return price;
    }
    }
    --------------------------------------------------------------------------
    package sale9;

    public class DiscountSale extends Sale {

    private double discount;

    public DiscountSale() {
    super();
    discount = 0;
    }

    public void setDiscount() {

    System.out.print("Enter a percentage for discount: ");
    this.discount = keyboard.nextDouble();
    System.out.println();
    }

    public double getDiscount() {
    return discount;
    }

    @Override
    public double bill() {

    double fraction = discount/100;
    return (1 - fraction)*getPrice();
    }

    @Override
    public String toString() {

    return (getName() + " Price = $" + getPrice()
    + " Discount = " + discount + "%"
    + " Cost = $" + bill());
    }

    }
    --------------------------------------------------------------------------

    Also, I seen some other posts where the source code is shown in a post in a more friendlier format using the word "code." Could someone please tell me how I should post the source code as well?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Code tags, use [code ] your code here [/code]. I will keep reading your post and tell you what i notice. Try creating the object in the loop.

  3. #3
    Xinc is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you for telling me how to post source codes, and thank you for your time as well!

    I'll try that. I check the code with debug mode. When I input data, the process seems to either copy the last index or the program inputs the new data to the all the previous indexes.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Java Code:
    this.a = new DiscountSale[column];
    for (index = 0; index < a.length; index++) {
        System.out.println("Item " + number + ":");
        sale.setName();
        sale.setPrice();
        sale.setDiscount();
        a[index] = sale;
        number++;
    }
    This is creating a single isnatnce of DiscountSale and then adding it into every index in the array. So when you change the values later on you are changing the single instance, and when you are printing out the array you are simply printing out the single instance several times.

  5. #5
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    Quote Originally Posted by Xinc View Post
    Hello.


    public class MultiItemSale {

    Scanner keyboard = new Scanner(System.in);
    private int column;
    private int index;
    private double totalBill;
    DiscountSale sale = new DiscountSale();
    DiscountSale[] a;

    public MultiItemSale() {
    column = 0;
    a = new DiscountSale[0];
    }

    public void fillArray() {

    int number = 1;

    System.out.println("How many items would you like to buy?\n");
    System.out.print("Number of items: ");

    this.column = keyboard.nextInt();
    System.out.println();

    this.a = new DiscountSale[column];

    for (index = 0; index < a.length; index++) {
    System.out.println("Item " + number + ":");
    sale.setName();
    sale.setPrice();
    sale.setDiscount();
    a[index] = sale;
    number++;
    }
    }
    As with what said in the previous post.

    a[index] = sale; where sale is a DiscountSale Object that gets updated each iteration, but nevertheless each element is still pointing to the same sale object.

    So what I mean by this
    when you calculate the first sale and add it to the array
    a[0] = sale
    you then modify sale and add it a[1],
    i.e.
    a[1] = sale

    but as this is the same sale object, it will update it in both a[0] and a[1] and so forth so that when the loop has finished they should all be as per the last sale.
    To fix this, recreate the sale object in each loop, so that its a different sale object each time.

    i.e.

    for (index = 0; index < a.length; index++) {
    System.out.println("Item " + number + ":");
    sale = new DiscountSale();
    sale.setName();
    sale.setPrice();
    sale.setDiscount();
    a[index] = sale;
    number++;
    }

    I'm 99% confident that is what the issue is, keep me posted on whether that was the problem.

    Cheers,

    David

  6. #6
    Xinc is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you sunde887, Tolls, and DavidG24! The problem has been fixed, and the program is working excellent now! I spent probably more than 8 hours trying to fix this one problem haha.

    I'm just so happy :D Thank you all once again! This is the first I tried a forum and I'm glad this is a great community :)

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  2. Handling input - multiple keys pressed
    By Shadowstrike in forum Advanced Java
    Replies: 3
    Last Post: 12-30-2010, 01:47 PM
  3. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 04:58 AM
  4. Multiple line user input
    By jointhelabel in forum New To Java
    Replies: 3
    Last Post: 10-26-2008, 02:41 AM
  5. Multiple Line Input Dialog Box
    By johnt in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 09:30 PM

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
  •