0

I'm currently working on a project that was assigned to me and I'm having a bit of trouble trying to find out how I should start. This assignment is to rewrite a program using classes instead of just putting everything into the main program. Here is my code that I created that has to rewritten:

public class Lab14 {
public static void main(String[] args)  {
int Numbook1=0;
int Numbook2=0;
int Numbook3=0;
double priceOfDesignPatterns=32.46;
double priceOfEffectiveJava=35.48;
double priceOfJavaPuzzlers=27.86;
String book1="Design Patterns";
String book2="Effective Java";
String book3="Java Puzzlers";
final double SALES_TAX=.065;
String choice;
int finished=0;


Scanner input=new Scanner(System.in);
System.out.println("Welcome to ECommerce!");
System.out.println("The books we have in stock are: ");
System.out.println(book1+", "+book2+", "+book3);
System.out.println("Please enter the name of the book you wish to purchase.  Press 'q' to quit.");

while (finished==0) 
{
choice=input.nextLine();
    if (choice.equals(book1))   
    {
        System.out.println("How many "+book1+"(s) do you wish to buy?");
        Numbook1=(input.nextInt())+Numbook1;
        System.out.println(Numbook1+" copies of "+book1+" are in your cart.");
        System.out.println("Enter the name of the next book you would like to purchase, or press 'q' to quit.");
    }
    else if (choice.equals(book2))
    {
        System.out.println("How many "+book2+"(s) do you wish to buy?");
        Numbook2=(input.nextInt())+Numbook2;
        System.out.println(Numbook2+" copies of "+book2+" are in your cart.");
        System.out.println("Enter the name of the next book you would like to purchase, or press 'q' to quit.");
    }
    if (choice.equals(book3))
    {
        System.out.println("How many "+book3+"(s) do you wish to buy?");
        Numbook3=(input.nextInt())+Numbook3;
        System.out.println(Numbook3+" copies of "+book3+" are in your cart.");
        System.out.println("Enter the name of the next book you would like to purchase, or press 'q' to quit.");
    }
    if (choice.equals("q"))
    {
        double total=0;
        double totalTax=0;

        total=(Numbook1*priceOfDesignPatterns)+(Numbook2*priceOfEffectiveJava)+(Numbook3*priceOfJavaPuzzlers);
        totalTax=total*SALES_TAX;
        System.out.println("Your cart contains: ");
        System.out.println(Numbook1+" "+book1+"(s), "+Numbook2+" "+book2+"(s), "+Numbook3+" "+book3+"(s).");
        System.out.println("Sales Tax: $"+totalTax);
        System.out.println("Your total cost is: $"+(totalTax+total));
        finished=1;
    }
}
}
}

Now I know that I have to write a couple of classes to construct book1,2,3 and also to set the price and access them later, but I'm just really lost on how to start the process and aggregate everything together. If someone could guide me in the right direction it would be greatly appreciated! Thank you so much.

Razib
  • 10,965
  • 11
  • 53
  • 80
Mark Wilcoxen
  • 69
  • 1
  • 10
  • 4
    Study your books, your notes, this has been reviewed in class most assuredly, then try something, anything, rather than posting "I'm really lost" and then dumping all here. – Hovercraft Full Of Eels May 01 '15 at 03:54
  • 1
    You have books (class Book) with price and title. A cart (class Cart ) for adding books. ...more at : http://agilemodeling.com/artifacts/crcModel.htm (or any good books on the topic) – Jayan May 01 '15 at 03:55
  • At least post your best first guess as to what you think a Book class might look like, don't worry how good or bad the attempt is. Please give us **something** to work with. How else can we have any idea of how you might be approaching this wrong?? – Hovercraft Full Of Eels May 01 '15 at 04:01
  • This is in essence re-factoring on a large scale. You need to slowly separate things into new classes and methods. Testing the program often to be sure that you haven't broken anything. It seems overwhelming but slowly and carefully it is doable. A great reference book is "Refactoring" by Martin Fowler. I don't know if you have time to read it for the current assignment but it's a great read in general anyway. Good Luck! – yitzih May 01 '15 at 04:33

1 Answers1

2

I will try to guide you towards the solution. The construct of the code may be as follows:

class Book {

    String name;
    double price;

    Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public double getPrice() {
        return this.price;
    }

    public String getName() {
        return this.name;
    }

}

You can use this Book class to define all your books. Write the set methods for this class yourself. You can use set-get methods over class variables for easy access and modifications. Next, you can have something like the following Cart class to keep track of all books user has chosen.

class Cart {

    ArrayList<Book> books;

    Cart() {
        books = new ArrayList<>();
    }

    public void addToCart(Book book) {
        books.add(book);
    }

    public ArrayList<Book> getCart() {
        return books;
    }

}

I will ask you to write a Cost class yourself in which you can compute the cost of all books added in the cart and add the sales tax to get the final cost.

You may then have a handler class like the one you have mentioned in your code to define books and generate cart for the user and compute the cost of the cart.

The objective of this assignment is to help you understand the basics of object oriented programming and modeling real world problems. I hope you get a hint of how it works.

Bhoot
  • 2,614
  • 1
  • 19
  • 36
  • 1
    This was very, very helpful. I think my problem was i couldn't figure out where to put what. The only thing i have a question too is when i go to call upon these methods in the main program, how do i stop the "cannot make a static reference to a non-static field" error? – Mark Wilcoxen May 01 '15 at 04:26
  • By giving a spoon-fed answer, you are doing the original poster a disservice by robbing him of the opportunity of discovering the code for himself, and you encourage them to come here for whole solutions rather then to think for themselves first. – Hovercraft Full Of Eels May 01 '15 at 04:26
  • @MarkWilcoxen: surely you at least know how to search for things first, right? [StackOverflow search](http://stackoverflow.com/search?q=%5Bjava%5D+cannot+make+a+static+reference+to+a+non-static+field). Yeah, I'm jaded, but you've asked another question without putting in any evidence for effort when the solution is so easy to find. – Hovercraft Full Of Eels May 01 '15 at 04:28
  • @HovercraftFullOfEels To stand up for myself and Bhoot as well, he defiantly didn't give me the entire answer, i still need to create methods within the class he showed me, as well as, create another method entirely. He simply gave me the basic rundown. – Mark Wilcoxen May 01 '15 at 04:29
  • But you didn't even show us an attempt first, and that should never occur. You're cheating yourself Mark by doing this, and you know it. At least try, and if you do try, at least show us the result. Am I asking too much of you? I don't think so. – Hovercraft Full Of Eels May 01 '15 at 04:30
  • 1
    @HovercraftFullOfEels: I agree that it is a jumpstart, but it is not a complete answer. I have tried to guide the OP towards a solution, but the one presented by me is far from accurate and complete. It is upto the OP to learn further and come up with an accurate solution. – Bhoot May 01 '15 at 04:34
  • @MarkWilcoxen: Please take a look at this question http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context. – Bhoot May 01 '15 at 04:36
  • @ Bhoot: and that's why I retracted my down-vote. Still I would have preferred that we held off answering pending the original poster improving his question and stretch his brain cells a little. My fear is that he may be turning into a "help vampire" where he doesn't learn to think for himself, something that I've seen us encourage by answering these type of questions. I sincerely hope that I'm wrong and that his questions improve with time. We'll see. – Hovercraft Full Of Eels May 01 '15 at 04:38
  • Also, please see my link in the third comment to this answer. I think it does your link above one better because it shows the OP how to search this site (although that question could have been answered easily also with a simple Google search). – Hovercraft Full Of Eels May 01 '15 at 04:41