Jump to content

Xmas present program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
19 replies to this topic

#1
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
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
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Stop right there.

Java is an object orientated programming language. This means that representing real world objects within java is very easy. Let me give you an example of how a car could be represented in java:


public class Car{


private int numberOfSeats;

private int engineSize;

private String manufacturer;

private String modelName;


public Car(String modelName, String manufacturer, int noSeats, int engSize){

  this.numberOfSeats = noSeats;

  this.engineSize = engSize;

  this.manufacturer = manufacturer;

  this.modelName= modelName;

}


public int getNoOfSeats(){

  return this.noOfSeats;

}


  //etc, etc, etc


}


You can create a new car like so:


Car rustBucket = new Car("F40", "Ferrari", 51, 1400); 


Now, how might you be able to represent a gift?

Oh and if you want a variable size array, have a look at arraylists.

HTH

#3
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts

Stu_328 said:

Stop right there.

Java is an object orientated programming language. This means that representing real world objects within java is very easy. Let me give you an example of how a car could be represented in java:


public class Car{


private int numberOfSeats;

private int engineSize;

private String manufacturer;

private String modelName;


public Car(String modelName, String manufacturer, int noSeats, int engSize){

  this.numberOfSeats = noSeats;

  this.engineSize = engSize;

  this.manufacturer = manufacturer;

  this.modelName= modelName;

}


public int getNoOfSeats(){

  return this.noOfSeats;

}


  //etc, etc, etc


}


You can create a new car like so:


Car rustBucket = new Car("F40", "Ferrari", 51, 1400); 


Now, how might you be able to represent a gift?

Oh and if you want a variable size array, have a look at arraylists.

HTH

Thanks,

That's exactly what I've been doing all the time. I'm trying out the ArrayList right now. My main problem now remains if I could store all the string and numerical data in one array and how I can save and load it from a file =]

#4
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
By the way, if I want to allow the user to enter a string, do I use input.next, input.nextLine or input.nextString ? I tried using input.nextString but it's not in the Scanner class. (input being an object defined as the Scanner class)

#5
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Dont store the data in an 2 dimensional array.

Have an array of Gift objects.

An array can contain any single object type you want.

Getting your data out of your object and writing it to a text file is lame. Look up serialization instead.

#6
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts

Stu_328 said:

Dont store the data in an 2 dimensional array.

Have an array of Gift objects.

An array can contain any single object type you want.

Getting your data out of your object and writing it to a text file is lame. Look up serialization instead.

Yeah, that's my problem. I don't get how can I have an array of Gift objects. Can you show me how?

#7
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
You do it like you create an array of any object. How do you create a String array?

a String is just an object, you know. And so is your gift class.

#8
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts

Stu_328 said:

You do it like you create an array of any object. How do you create a String array?

a String is just an object, you know. And so is your gift class.

Alright, I'll give it a go. I'll post my code later to see if I get the right idea. Thanks dude =]

#9
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
By the way, you got msn?

#10
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Not at work.

#11
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts

Stu_328 said:

Not at work.

My program is on its way to completion now. Thanks to you. I have a small question though: when I want the user to input a string, i use this

Scanner input = new Scanner(System.in);

String Name;
String Gift

Name = input.next();
Gift = input.next();

//etc

it works, only for strings which do not have spaces in them. I mean if I put just "David" for Name, and then "Lamborgini" for Gift, it works fine. But if I put say "David Morgan", it kinda jumps and ignores the Gift input line. How can I fix that?

Thanks

#12
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
If you aren't already, you should have your head in this:

Java 2 Platform SE 5.0

It documents all the core Java classes (also known as Javadoc).

Heres what they say about the Scanner object:

"Sun" said:


A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.


If I were you I wouldn't be using a Scanner at all, but if you want to why don't you change the delimiter?