Hi all,
I'm working on a school project and I am running into a problem, possibly more...
This is my first post, so I am sorry if it ends up ugly.
Here is a description of the project:
This project focuses on demonstrating your understanding of classes and objects. Before attempting this project, be sure you have completed all of the reading assignments listed in the syllabus to date, participated in the weekly conferences, and thoroughly understand the examples throughout the chapters. The project requirements include:
Write a class representing a line item in a shopping cart: the characteristics of this class are the name of the product, the cost per unit, and the number of items. This class is named LineItem.Provide a constructor that takes 3 arguments, and initializes the corresponding instance variables. Provide the accessors to the 3 instance variables.
Write a class representing a shopping cart: a shopping cart contains a list of line item objects. This class is named ShoppingCart: provide a constructor for this class, a method to add a line item object to it, and a method printing out its content, with the total cost adding up the cost of all line items.
Write a test program that creates 2 shopping carts, and asks the user to input the content of each shopping cart. Once the user is done inputing the content of the shopping carts, the program displays their contents with their cost.
Your programs should compile and run without errors.
Be sure to test your program carefully. Provide a list of comprehensive test cases used to validate your application and include these test cases in your word document containing your UML diagrams and descriptions. Similar to Project 1, your test data can be shown in a table that includes input data, expected output, actual output and pass/fail results from the test.
Submission requirements:
Your deliverables include 3 Java files (LineItem.java, Shoppingcart.java, and TestShoppingCart.java), and a Word document. Do not forget to have a comment indicating your name in each java file. Your word document should include your test table, and UML diagrams and descriptions and be named TestShoppingCart.doc. Your completed assignment should be submitted to your WebTycho Project 3 assignment area no later than the due date listed in the syllabus.
Yay.. that was a fun read!!!
Here is my first file :
import.java.util.*;
public class TestShoppingCart {
public static void main(String [] args){
//Create two shopping carts
ShoppingCart newCartOne = new ShoppingCart(totalcost);
ShoppingCart newCartTwo = new ShoppingCart(totalcost);
//I think I need this to pull the product names when asking for input
LineItem shoppingCartTest = new LineItem(productNames[], itemCost[], shoppingCartQuantity[]);
//ask the user to select a cart (Not sure that I will need this
Scanner userInput = new Scanner(System.in);
System.out.println("Please select your shopping cart: \nFor cart 1 enter integer 1.\nFor cart 2 enter integer 2.");
int userCart = userInput.nextInt();
//Ask the user to input the contents of each cart
for (int x = 0; x < shoppingCartQuantity.length; x++){
System.out.println("How many " + productNames[x] + "Would you like? Enter an Integer: ");
shoppingCartQuantity[x] = userInput.nextInt();
}
//Once they input the content, Display the contents of the cart, Quantity of each item and the total cost.
newCartOne.getCartDisplay();
newCartOne.getTotalCost();
newCartTwo.getCartDisplay();
newCartTwo.getTotalCost();
}
}
Here is the ShoppingCart file
import java.util.*;
/*this class needs to :
*contain a list of lineItem objects
*Provide a constructor for this class
*method to add a line item to this class
*/
class ShoppingCart{
private int totalCost = 0;
public ShoppingCart(int totalCost){
this.totalCost = totalCost;
}
//Method to display the contents and quantity of the cart
public void getCartDisplay(int[] shoppingCartQuantity, String[] productNames){
for (int x = 0; x < shoppingCartQuanitity.length; x++){
System.out.println("Your cart has " + shoppingCartQuantity[x] + productNames[x]);
}
}
//Method to display the total Cost
public int getTotalCost(int[] shoppingCartQuantity, int[] itemcost, int totalCost){
for(int i = 0; i < shoppingCartQuantity.length; i++){
int lineItemCost = itemCost[i] * shoppingCartQuantity[i];
totalCost = totalCost + lineItemCost;
}
System.out.println("The total Shopping Cart cost is: $" + totalCost);
return totalCost;
}
}
And lastly the LineItem.java file
/*
*Characteristics are: name of the product, cost per unit, number of items
*Provide a constructor that takes 3 arguments and intializes the corresponding instance variables
*provide the accessors to the 3 instance variables
*/
import java.util.*;
class LineItem{
private int[] itemCost = {50, 25, 75, 100, 10};
private String[] productNames = {"Bananas","Apples","Oranges","Grapefruits","Cherries"};
private int[] shoppingCartQuantity = new int[5];
public lineItem(string[] productNames, int[] itemCost, int[] shoppingCartQuantity){
this.itemCost = itemCost;
this.productNames = productNames;
this.shoppingCartQuantity = shoppingCartQuantity;
}
public int[] getShoppingCartQuantity(){
return shoppingCartQuantity;
}
public int[] getItemCost(){
return itemCost;
}
public String[] getProductNames(){
return productNames;
}
}
in the first file I get a '.class' expected error on the following line
LineItem shoppingCartTest = new LineItem(productNames[], itemCost[], shoppingCartQuantity[]);
Thoughts? Am I on the right track as far as the program goes, or am I completely off my rocker?