• Post Reply Bookmark Topic Watch Topic
  • New Topic

input reader from another class.  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fellow newbie here. I got a following assignment I need to do:

InputReader Class
-This class will perform as it did in the labs. It will allow users to enter data from the command line. Reuse the class that is provided in the solution for the labs.

Customer Class
-Add a default constructor that will prompt the use to enter data to create a new Customer Object. The prompt might look like this:
-Create a new Customer.
(first name, last name, credit card #, address, phone number):
-example input:
darby,dog,9404949493923445,123 Here St.,123-123-1234
-Here you will use the InputReader class to obtain the user command line input. The input should then be split into separate arguments which are then used to construct a Customer Object. See the String class to find out how to split the arguments.
-The rest of the class will be unchanged from the previous assignment.


I get "Error: incompatible type".
The Input Reader is from another assignment that a teacher wrote. I felt I had to change it from Int to String because I'm dealing with characters. The first Imput Reader sample below is the original, then my modified (String)version.

Original Version


My Version


Here's the Rental Customer Class

The assignment say "The input should then be split into separate arguments which are then used to construct a Customer Object"
Can this be done in one method or I need to create a method for every category: name, phone, etc? Where can I read up on playing with String?



The printout result should look like:


Customer [firstName=Darby, lastName=Dog, creditCardNumber=123456789, address=123 Here St., phoneNumber=123-123-1234]
You are purchasing the following items:
kazzoo $2.00
kazzoo $2.00
kazzoo $2.00
tambourine $2.00
whistle $2.00
whistle $2.00
TOTAL: $12.00
You are renting the following items:
tambourine
Rental fees: $31.00
GRAND TOTAL: $43.00


Am I creating objects properly or input reader is not setup correctly? Note: where using blueJ.
Thanks in Advance!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcos William wrote: . . .
-Add a default constructor that will prompt the use to enter data to create a new Customer Object.
. . .
That is incorrect. You cannot write a default constructor. That obviously means a constructor without arguments.

If your input class has a method which returns an int, that returns an int and that cannot be changed. If you need a String, you find the method which returns a String.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an InputReader class; that deals with the keyboard input. Do all the input via that class. No Scanners or anything elsewhere. You will find I tell people to create utility classes. That is exactly what input reader is: a utility class. Give it all static members and a private constructor you never call, because you never need any instances. Call InputReader.nextInt() or whatever.
You have been told exactly what to do with the no‑arguments constructor, but you haven't done it. You use the input reader to initialise all the fields to sensible values. There are validation tests you can perform, but they are probably beyond the stage of learning you have reached. Each method has one return type, so you need one call per field to initialise.
There is never any need to catch an InputMismatchException; Rob Spoor told me ages ago that you can always avoid it. I seem to write about Scanner all the time: have a look at this old thread and the link to an older thread I posted.
Never close a Scanner pointing to System.in, somebody else had problems with that, as discussed here.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you never call the constructor for the input reader class, you will have to initialise the Scanner where it is declared
private static Scanner myScanner = ...;
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a super newb. Let me try to figure out you posted

That is incorrect. You cannot write a default constructor. That obviously means a constructor without arguments.

Can I write a default construtor to create a new Customer object without arguement? No arguement means, empty parameters...right?

If your input class has a method which returns an int, that returns an int and that cannot be changed. If you need a String, you find the method which returns a String.

This means changing the input class into String or creating a converter (int to String)?


 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You have an InputReader class; that deals with the keyboard input. Do all the input via that class. No Scanners or anything elsewhere. You will find I tell people to create utility classes. That is exactly what input reader is: a utility class. Give it all static members and a private constructor you never call, because you never need any instances. Call InputReader.nextInt() or whatever.
You have been told exactly what to do with the no‑arguments constructor, but you haven't done it. You use the input reader to initialise all the fields to sensible values. There are validation tests you can perform, but they are probably beyond the stage of learning you have reached. Each method has one return type, so you need one call per field to initialise.
There is never any need to catch an InputMismatchException; Rob Spoor told me ages ago that you can always avoid it. I seem to write about Scanner all the time: have a look at this old thread and the link to an older thread I posted.
Never close a Scanner pointing to System.in, somebody else had problems with that, as discussed here.


I'll read up on the links.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcos William wrote: . . .
Can I write a default construtor . . .
No. Only the complier ever writes a default constructor. You can read about it here, but it might be difficult to understand.

It is very easy to write a no‑arguments constructorIf you look at this link about Javadoc Comments, it says it is poor style to have default constructors. So always write a constructor for every class you write.
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got throught Customer class. I'm now required to create another Class called ShoppingCart. One of the method requires me to take 2 input (instrument name and cost) from another class, use an arraylist and printout. I created class object into an array, but for some reason, compiler can't find methods. Here's the intruction I need to follow:

Implement addToPurchaseCart(MusicalInstrument instrument, int quantity), which will first check to see that the number of Instruments purchased does not exceed the number currently in stock. If the number being purchased is OK, then add that number of Instruments to the purchased ArrayList. Also, don’t forget to delete the same number of Instruments from the inventory.


Here's what I got:

It needs to arrange like this:


kazzoo $2.00
kazzoo $2.00
kazzoo $2.00
tambourine $2.00


Compiler can't find: shopInstrument.getDescription and shopInstrument.getQuantitySold Also, what do I use to delete from the list?

Thanks in Advance!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instructions say to add it to the list. Not to write a new shopping list. That method does not appear to do what you were told at all.
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find the instruction a bit ambiguous . Here's the full instruction:

Your final assignment will allow users to build shopping carts for both rented and purchased items from the music store. We will continue to use the classes and associated functions from the previous assignment.
InputReader
• This class will perform as it did in the labs. It will allow users to enter data from the command line. Reuse the class that is provided in the solution for the labs.
Customer
• Add a default constructor that will prompt the use to enter data to create a new Customer Object. The prompt might look like this:
o Create a new Customer.
(first name, last name, credit card #, address, phone number):
o example input:
darby,dog,9404949493923445,123 Here St.,123-123-1234
• Here you will use the InputReader class to obtain the user command line input. The input should then be split into separate arguments which are then used to construct a Customer Object. See the String class to find out how to split the arguments.
• The rest of the class will be unchanged from the previous assignment.
ShoppingCart
• This is a new class which will be used to support two ArrayLists; one for purchased items and a second one for rented items.
• Provide accessor and mutator methods for each ArrayList, as in public ArrayList<RentalAgreement> getRentalAgreements().
Implement addToPurchaseCart(MusicalInstrument instrument, int quantity), which will first check to see that the number of Instruments purchased does not exceed the number currently in stock. If the number being purchased is OK, then add that number of Instruments to the purchased ArrayList. Also, don’t forget to delete the same number of Instruments from the inventory.
• Implement addToRentalCart(RentalAgreement agreement), which is used to add one RentalAgreement at a time to the rental ArrayList. NOTE you will have to create Rental Agreements before you can do this.
• Implement void viewShoppingCarts(), which will display the contents of the purchased ArrayList along with the price of each Instrument and a total price for them all. The method will also display the items being rented and the total rental fee for all those rented items.
• Output:

Customer [firstName=Darby, lastName=Dog, creditCardNumber=123456789,
address=123 Here St., phoneNumber=123-123-1234]
You are purchasing the following items:
kazzoo $2.00
kazzoo $2.00
kazzoo $2.00
tambourine $2.00
whistle $2.00
whistle $2.00
TOTAL: $12.00
You are renting the following items:
tambourine
Rental fees: $31.00
GRAND TOTAL: $43.00


I'm working on italic section. I was trying to access method from another class using array...which I think can't be done. So, I will call method the regular way and convert to arraylist. Any help/link of doing this using the following "addToPurchaseCart(MusicalInstrument instrument, int quantity)"?

Thanks

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see what is unclear about those instructions. The add method adds a new rental/purchase to the List. It does not write a new Shopping List.
There is also a method which cancels part of an order; I presume you will have to work out which order it is an remove it from the List.

It is much easier if you envisage those transactions in real life on pieces of paper rather than as software. Once you know how you do it on paper, it usually becomes easy to do convert it to software.
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it's pretty easy when putting it in real life. But when the direction ask for certain methods to be used using set class, it gets a bit confusing for a newbie. I think if I did it starting from fresh sheet with my own method, it might be a bit easier.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what did you get in real life? Write out the easy part. Then you can convert it to code.
 
Marcos William
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would, but not with this assignment. I won't get credit for not following instruction.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant, if you have a shopping list and want to add to it, how do you do it? And how do you remove something from a shopping list? Both those are parts of the question, and both are really simple once you work out what to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!