Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Xmas present program

    I've been assigned one problem to write on Java. I'm fairly new to Java and need some help on the program.

    I need to write an application that manages Christmas presents. The user will introduce names of recipients, gift name and gift cost. I will need to store the data in an array of objects. The program should also be able save the data in a file on disk, and also read the file.

    I've made an attempt on writing the problem. At first, i realized that I would need a two dimensional array which would have three columns for the name, gift and cost. The information about them would be recorded in rows. However, because Name and Gift are strings, but Cost is a number ... so I don't know if I can define an array that would accept both string elements and numerical elements; I've created two arrays instead.

    Because the user should be able to introduce as many recipients as he wishes, that would mean we would have a variable array, with variable amount of rows. I don't really know if that's possible. (I thought about setting the number of rows to the maximum number of an Int =])

    I have no idea how I can save and read the data on a file on disk. I've looked at BufferReader and also other stuff. But I don't get it how they would be able to recognize which column is which (in one row).

    Thanks in advance, any help would be appreciated.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Since this is an assignment we can't provide you with any code. We can provide you with some pointers, and if you're stuck on any specific code we can help shed some light.

    For you're arrays, you're right. You cannot combine data types. You're options are to use an array of standard or custom objects. To become scalable, the easiest way is to use the built in Vector class instead of a primitive array, otherwise you need to write functionality to create, increase and copy you're arrays. If the person, gift and price are always assigned (not randomly chosen), I would create a custom object containing a string, string, float/double and generic my vector: Vector<MyCustomObject> myVector = new Vector<MyCustomObject>().
    Reading and writing from persistant storage is a piece of cake. Follow the tutorials from the sun api for file and buffers, and simply implement serializable interface on you're object storing the information. This lets you read and write to filesystem with very little pain to yourself - Java takes care of the details. Google up some Java serialization tutorials, there are likely tons online to show you how to do this.

    On a final note, you can actually store this in a String array. You're float value can be stored as a string and reparsed using the String.valueOf (I think thats what its called) and Double.parseDouble methods. This is an overboard route - each character is stored as 8 bits, so only four characters (including the decimal point) are required before you exceed the memory used in a float. Minimal yes I know, way more memory to interpret the same number.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Since this is an assignment we can't provide you with any code. We can provide you with some pointers, and if you're stuck on any specific code we can help shed some light.

    For you're arrays, you're right. You cannot combine data types. You're options are to use an array of standard or custom objects. To become scalable, the easiest way is to use the built in Vector class instead of a primitive array, otherwise you need to write functionality to create, increase and copy you're arrays. If the person, gift and price are always assigned (not randomly chosen), I would create a custom object containing a string, string, float/double and generic my vector: Vector<MyCustomObject> myVector = new Vector<MyCustomObject>().
    Reading and writing from persistant storage is a piece of cake. Follow the tutorials from the sun api for file and buffers, and simply implement serializable interface on you're object storing the information. This lets you read and write to filesystem with very little pain to yourself - Java takes care of the details. Google up some Java serialization tutorials, there are likely tons online to show you how to do this.

    On a final note, you can actually store this in a String array. You're float value can be stored as a string and reparsed using the String.valueOf (I think thats what its called) and Double.parseDouble methods. This is an overboard route - each character is stored as 8 bits, so only four characters (including the decimal point) are required before you exceed the memory used in a float. Minimal yes I know, way more memory to interpret the same number.
    Thanks,

    I have a quick question then. Say I create a class like this:

    Code:
    public class XmasGift(String Name, String Gift, Double Cost)
    then I want to store all the XmasGift objects in an array. How can I do that?

  • #4
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I personally would create a class to store Gift information which you already have done: XmasGift. Then start reading information from the user, create an XmasGift object and store it in a Vector or ArrayList (similar to Vector).

    Code:
    ...
    //ArrayList to hold XmasGift objects using generics (better style)
    ArrayList<XmasGift> xmasgift= new ArrayList<XmasGift>();
    
    //ArrayList to hold XmasGift objects if not using generics
    //ArrayList xmasgifts = new ArrayList();
    
    //Prompt for Gift Name
    System.out.print("Enter Gift Name: ");
    String giftName =  //Read from console using Scanner or something similar
    
    //Prompt for cost
    System.out.print("Enter Gift Cost: ");
    double giftCost =  //Read from console using Scanner or something similar
    
    //Prompt for recipient
    System.out.print("Enter Recipient Name: ");
    String recipient =  //Read from console using Scanner or something similar
    
    //create an XmasGift object with the information
    XmasGift gift = new XmasGift(recipient, giftName, cost);
    
    //Store the XmasGift in the ArrayList
    xmasgifts.add(gift);
    
    //Print out the gifts in the ArrayList to test that they were added correctly
    //Assumes you're using generics
    for(int i = 0; i < xmasgifts.size(); i++){
         System.out.println("Gift " + (i+1) + ":");
    
         //Assumes you have a method getGiftName in XmasGift that returns the gift name
         System.out.println("Gift Name: " + xmasgifts.get(i).getGiftName());
    
         //Assumes you have a method getRecipient in XmasGift that returns the recipients name
         System.out.println("Recipient: " + xmasgifts.get(i).getRecipient());
    
        //Assumes you have a method getCost in XmasGift that returns the gift cost
         System.out.println("Cost: " + xmasgifts.get(i).getCost());
    }
    I may have provided more code then I should have, but I've left the harder stuff for you. However, there's the basic layout (according to my brain) which may help you. You'll need to add some sort of loop so that you can get multiple Gift information from the user. And of course do the file stuff.

    Here's some useful links for Vector and ArrayList:
    http://java.sun.com/j2se/1.3/docs/ap...ArrayList.html
    http://java.sun.com/j2se/1.3/docs/ap...il/Vector.html
    Last edited by Gox; 12-17-2008 at 08:37 PM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Oh oh, I'll make another suggestion (just to stay in christmas tune)!
    Create a custom collectioin called 'XMasBag' and extend Vector/Arraylist! Best part is, its an empty class, you're just using it to rename and develop the generic, heh heh heh. Maybe it will give you're instructor a little bit of extra cheer over the christmas holidays! isa XMasBag, awesome.

    Gox's code is much along the idea I have as well, a single person is assigned a single gift at a single price, letting you create a custom object for it. Then you just add it to you're collection.

    Start making some code for this and post back with problems.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New Coder
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Oh oh, I'll make another suggestion (just to stay in christmas tune)!
    Create a custom collectioin called 'XMasBag' and extend Vector/Arraylist! Best part is, its an empty class, you're just using it to rename and develop the generic, heh heh heh. Maybe it will give you're instructor a little bit of extra cheer over the christmas holidays! isa XMasBag, awesome.

    Gox's code is much along the idea I have as well, a single person is assigned a single gift at a single price, letting you create a custom object for it. Then you just add it to you're collection.

    Start making some code for this and post back with problems.
    I have some problems uisng the Scanner.

    Code:
    Scanner input = new Scanner(System.in);
    
    System.out.println("Please enter the name of the receiever: ");
    Name = input.next();
    System.out.println("Please enter the gift: ");
    Gift = input.next();
    System.out.println("Please enter the cost of the gift: ");
    Cost = input.nextDouble();
    This is similar to what I'm using to get the user to input their name and other details. However, the default delimiter is the "white space", so I can't enter something like "Paris Hilton" with white spaces, I've read the class library and other sources but I could still not change the delimiter so that it enables to read everything on one line. I believe the pattern should be something like end of input (\z) or end of line or something like that.

    Code:
    input.useDelimiter("\z")
    //and other patterns
    but it doesn't seem to work as it should.

    Should i switch to using BufferedReader instead?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep, scanner treats next in the same way as a tokenizer.
    For you're name, use nextline instead of next, that lets you retrieve the line. Alternatively, you could also use a while loop to concat you're next onto you're string (there's a method in Scanner that tells you if it has more entries, but I can't recall its name atm, its like hasNext or something like that). The proceed as normal.
    I'm trying to remember what it does with the linefeed when you use the nextline versus the next. I seem to recall that one of them left it on the buffer, which conflicted with you're next 'next' call. I don't think it applies when the following calls are for specific tokens like nextint and nextdouble though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New Coder
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yep, scanner treats next in the same way as a tokenizer.
    For you're name, use nextline instead of next, that lets you retrieve the line. Alternatively, you could also use a while loop to concat you're next onto you're string (there's a method in Scanner that tells you if it has more entries, but I can't recall its name atm, its like hasNext or something like that). The proceed as normal.
    I'm trying to remember what it does with the linefeed when you use the nextline versus the next. I seem to recall that one of them left it on the buffer, which conflicted with you're next 'next' call. I don't think it applies when the following calls are for specific tokens like nextint and nextdouble though.
    man, you're = your, you mean =]?

    Anyway, I tried using nextLine() but it keeps jumping down and ignore the first input line. I also tried using this:

    Code:
    while (input.hasNext() = true)
         Name = Name + " " + input.Next();
    It throws thi error: unexpected type. Required: variable. Found: value

  • #9
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I didn't feel like working (my apologies to my boss) so I quickly coded this up. I don't have a lot of experience with Scanner as well, but the following seemed to work for me.
    Code:
    System.out.print("Enter Gift Name: ");
    giftName = input.nextLine();
    
    System.out.print("Enter Recipient Name: ");
    recipient = input.nextLine();
    
    System.out.print("Enter Gift Cost: ");
    cost = Double.valueOf(input.nextLine())
    I had trouble getting input.nextDouble() to work the way I wanted, and rather than figure it out I decided to just read the Cost in as a String and then parse it into a double.


  •  

    Posting Permissions

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