Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with used car program  RSS feed

 
Umor Collins
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create two classes, one called car (this is the actual car object) and one called carlist (this is an array of cars which are for sale). I of course also need to create an additional driver class for the program (this will contain the main method which will create objects and call their methods, and I need to use the keyboard class to get input from the user).
The program should be capable of providing the following user experience:
ACME Used Car Dealers
1. Purchase car
2. Display cars for sale
3. Sell car
Enter option: 1
PURCHASE CAR
MAKE: Ford
Model: Escort
Year: 2001
Miles: 3400
Color: White
Purchase price: 1500
Sale price: 2495
1. Purchase car
2. Display cars for sale
3. Sell car
Enter option: 2
CARS FOR SALE
No Make Model Year Miles Color Price
1 Ford Escort 2001 3400 White 2495
2 Saab 93 2000 56000 Black 4999
1. Purchase car
2. Display cars for sale
3. Sell car
Enter option: 3
SELL A CAR
Which car: 1
What price: 2395
Sold a White Ford Escort 2001
You just made us $895 profit

BTW, each car object should store (at a minimum) the following information:
-make
-model
-year
-miles
-color
-purchase price
-for sale price
-sold flag
The carlist object will be an array of cars and a pointer to the next free location in the array for the next car to go in
I would appreciate any sort of help
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Umor, Welcome to JavaRanch!
Man, I hope this isn't your first java assignment! Nothing like throwing you into the deep end first.
It sounds like your instructions have already laid out a good starting point for you. It tells you what each class "knows" and is "supposed to do".
You know you have a Car class.
You know all the instance variables of the Car class.
You know you have a Driver class and it has a method called "main".
You know you have a CarList class that creates an array of car objects.
You know you have at least three methods to implement somewhere: Purchase Car, Display Cars for Sale, and Sell a Car.
Have you at least got the basic shell of each of these classes started? If not, get this done and post back the code you have started and we'll take it from there.
 
Adam Brown
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how far along you are with your study of Java, but if you're trying to create a "dynamic" array of objects, you might want to look into Array Lists. Array Lists allow you to add and remove objects from an object array, however, once objects are added to the array list they will no longer be type specific in regards to what objects you are "pushing" (and I use the term loosely...) in. Simply put, if you were to add Car objects to an ArrayList, they would no longer be objects of type Car. They instead would be objects of type Object (notice I used a capital 'O', meaning that 'Object' is a class, a top-level class of the java.lang package). In order to extract any information on a specific object within the ArrayList you have to find the index you want and cast it back as a Car object. This explanation may or may not be of any help to you but regardless, a look at the ArrayList class located in the java.util package (java.util.ArrayList; = fully qualified name) would be to your benefit.
For the official scoop on what an ArrayList is, go here
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my lash at it (it's rough).
It's not exactly what you're after, but there might be a few bits in it to suit you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!