Array Exercise


Classwork /Homework

Turn in the first part as classwork, then continue to do the rest for homework.  If you have more time in class and the group agrees, you can continue working past Part A together for as long as you are in class, and each continue from the point where you left off for homework. (No matter how much you get done during classwork, everyone must turn in for homework as well)

Remember that all group members can go into Blackboard to download the project turned in for classwork.

If you miss this class, you will have to start from scratch and do the classwork to complete the homework.


 

Classwork

Create a project and add a class with a main.

Create a class Book, which has instance variables for author, title, and price.  Add the standard methods.  Do not allow negative prices.

 Create a class Cart.

Give it an instance variable customername, which is a String, and an array of Books called contents.  Add all the standard methods except toString (discussed later), but don't include accessor or mutator for the array.  In the default constructor, set up contents to be length 10, but don't put any books in it.

Now add an int instance variable firstempty.  This will be used to keep track of the first empty position in the array.  In the constructor, explicitly set this to 0, indicating that the array is totally empty.  Don't include public accessor or mutator for this.

Add a method addBook, which takes a Book as parameter and returns a boolean.  First check firstempty to see if there is any room left in the array.  If so, put the new book in the first empty position in the array, and update firstempty accordingly, then return true.  If the array is full, just return false.

Add another method addBook, which takes as parameters Strings for author and title and a double for price, and returns a boolean.  First check firstempty to see if there is any room left in the array.  If so, create a new Book at  the first empty position in the array, with the given author, title and price, then update firstempty accordingly, then return true.  If the array is full, just return false.

Add a method clearCart which sets firstempty back to 0.

Add a method total, which returns the total of all the prices of all the books in the array.  Remember that the array may not be full!

Add a toString method that puts the customer name at the top, then lists all the books, then has the total price at the end.  Again, remember that the array may not be full.

In the class with main, create an array of three Carts.  Add one book to the first cart, two books to the second, and two to the third.  Make sure to use both versions of addBook

In a loop, print each Cart, then clear it, then print it again.

This is as much as you must do for classwork.  You may continue in class to do more.


Finish the assignment for

 homework


Create a class Store.  Give it instance variables

For this class, you need not add accessors or mutators -- we will not allow outside classes any access to our variables. 

In the default constructor,

Don't set current to anything in the constructor.  It will  be used to keep track of which Cart in the array is the current customer while the program runs:

To this class, add a method login, which asks the user for a name and searches through the array of Carts for one that has that customer name.  If you find one that matches, set current to that Cart.  If none matches, ask for another name, and keep trying until they give a name that matches.

Add a method display that prints out all the books in inventory, with numbers next to them, numbered from 1.

Add a method buy, which calls display, then asks the user to choose an book to buy by number.  Assume they are using the numbering from the printout, which starts at 1.  If the number is valid for an book in the inventory, add that book to the current cart.  If the number they give isn't valid, ask again, and keep trying until they give a number that is.  If adding a book to the cart returns false, print a message telling the user their cart is full. (Adding a book to a cart doesn't remove it from the inventory.)

Add  a method showCart which prints the current cart

Add a method checkout which prints a message telling the user, using the name from the current cart, that their credit card has been charged the total for the current cart, then clears out the current cart. (Cart has a method for this.)

Add a method logout, which sets current to null.

Add a method menu.  In this method, repeatedly ask the user if they want to login or quit until they choose quit.  If they say login, then call the login method and then (once they are logged in) repeatedly give the user the following options until they choose logout (in which case, we will be back to asking if they want to login or quit).

  1. buy
  2. show cart
  3. checkout
  4. logout

Based on the number they input, call the appropriate method.

 

 

Create a new class Interface, and add a  main method.  In this main, create a Store, and call its menu method.  Test your program.  Make sure that you can login, buy books, show the cart, checkout, and logout.  Also make sure that one customer can buy books, then logout without checking out, then another customer can log in, and after the second has logged out, the first can log in again and see the things they put in their cart earlier, as long as the program is still running. 

To help me test your work, include the names for your carts as a comment at the top of your main method.


EC+25

In Cart, add a method remove.  In this method, if there are no books in the array, print a message saying so.  Otherwise, set up a Scanner and then print all the books in the array as a numbered list (start at 1).  Ask the user for the number of a book to remove from the array and if this is a valid number of a book in the array, remove it using one of the following approaches:

In Store, add remove as another option in menu and if the user chooses it, call remove for the current cart.