I've a problem in displaying the attribute of object from list...

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

52 Replies - 1107 Views - Last Post: 01 December 2013 - 10:56 AM Rate Topic: -----

#1 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 05:24 AM

Hello Everyone,
As this my first post in the dreamincode.net, I've a project to do for my Java programming course.. It is pretty straight forward.

User enters the shopping market, goes to different department and selects the items and adds to his/her cart. So far, the coding i did is shown below. At first, user selects the department he/she want to go in, and then list of items is shown once he/she selects the department. I just need the help when user enters the name of the product and its quantity, it should add to his cart by automatically knowing the price which is saved in the list.

import java.util.ArrayList;
public class ShoppingCart {


ArrayList <Item> myList = new ArrayList <Item> ();
	private double totalPrice;
	
	public ShoppingCart()
	{
		totalPrice = 0.0;
	}
	
	public void chckdept(String name, Integer qty)//problem here
	{
		if(name.equalsIgnoreCase(b.getName()))
	}
	
	
	
	public void addToCart(String itemName, int quantity) //Selecting items and adding to cart
	{		
		myList.add(new Item(itemName, quantity));
	}
	
	public String toString()
	{
		String output = "\nShopping Cart\n \nItem\tPrice\tQty\tTotal\n";
		for (int i = 0; i < myList.size(); i++)
			return myList.get(i).toString() + "\n";
			return output+= "\nTotal Price:" + totalPrice +"\n";
	}
	
	public void getInput(Integer i)
	{
		Department d = new Department();
		if(i==1)
		{
			System.out.println("Item\tPrice");
			for ( int x = 0 ; x < d.fav().size(); x++)
			{				
				System.out.println(d.FAV.get(x).list());
			}		
		}
	}
	
	public double getTotalPrice() {
		return totalPrice;
	}
}
 


import java.util.ArrayList;

public class Item
{
	private String name;
	private double price;
	private int quantity;
	
	public Item (String itemName, double itemPrice) //For department items
	{
		name = itemName;
		price = itemPrice;
	}
	public Item (String itemName, int numPurchased) //User Input Name & Quantity
	{
		name = itemName;
		quantity = numPurchased;
		double total = price*quantity;
	}
	public String toString ()
	{
		 return (name + "\t" + price+" AED\t" + quantity);
	}
	
	public String list()
	{
		return ("-> "+name + "\t" + price+" AED");
	}
	
	public double getPrice()
	{
		return price;
	}
	public String getName()
	{
		return name;
	}
	public int getQuantity()
	{
		return quantity;
	}
} 

 



//Class for different departments and the products they contain...
import java.util.ArrayList;


public class Department {
	ArrayList <Item> FAV = new ArrayList <Item> ();
	ArrayList <Item> Electronics = new ArrayList <Item> ();
	ArrayList <Item> Clothing = new ArrayList <Item> ();

Department()
{
	Item b = new Item("BANANA", 0.75);
	FAV.add(B)/>/>/>;
	Item o = new Item("ORANGES", 0.75);
	FAV.add(o);
	
}
}



import java.util.Scanner;

public class ShoppingTest {

	public static void main(String[] args) {
		
		Scanner read = new Scanner (System.in); 
		String name;
		String str;
		Integer qty;
		ShoppingCart myCart = new ShoppingCart();
		do {
			System.out.println("Choose the department from the following: \n1)Fruits & Vegetables \n2)Electronics \n3)" +
					"Clothing \n4)Cosmetics");
			String sc=read.nextLine();
			int scc=Integer.parseInt(sc);
			myCart.getInput(scc);
			
			System.out.println("Enter the names of the Items and quantity:");
			name=read.nextLine();
			qty = read.nextInt();
			myCart.addToCart(name, qty);
			System.out.println(myCart.toString());
			System.out.println("\n Do you wish to continue (y/n)?");
			str = read.nextLine();
			
		} while(str.equalsIgnoreCase("y"));
		
		System.out.println("Your total bill=" + myCart.getTotalPrice());
		}
	}




I've been struggling for this from long time, I hope here i'll surely be helped.
Thankyou in advance..

This post has been edited by Atli: 23 November 2013 - 06:29 AM
Reason for edit:: Removed the excessive bold formatting. Bold text is for emphasis, not to be used on the entire post.


Is This A Good Question/Topic? 0
  • +

Replies To: I've a problem in displaying the attribute of object from list...

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 07:41 AM

You have a missing abstraction. That's why you're running into problems. That abstraction is Order. An Order (which would hold the total cost) would consist of 1 to n Item instances.
Was This Post Helpful? 1
  • +
  • -

#3 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 08:09 AM

OOOH..You mean like by making an abstract class?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 08:55 AM

No - it need not be abstract
Was This Post Helpful? 0
  • +
  • -

#5 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 09:00 AM

View Postg00se, on 23 November 2013 - 08:55 AM, said:

No - it need not be abstract

then? Please do explain me as I'm new to it.. Thanks for the help mate..
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 11:41 AM

Well i thought i just did explain.
Was This Post Helpful? 0
  • +
  • -

#7 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 11:53 AM

View Postg00se, on 23 November 2013 - 11:41 AM, said:

Well i thought i just did explain.


Lol No I mean like detailed explanation if you can.. That'll be helpful.. :-/
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:05 PM

Start by writing the class i mentioned. It should be something like

public class Order {
    private List<Item> items;
    private String id;

...
}

Was This Post Helpful? 0
  • +
  • -

#9 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:09 PM

View Postg00se, on 23 November 2013 - 12:05 PM, said:

Start by writing the class i mentioned. It should be something like

public class Order {
    private List<Item> items;
    private String id;

...
}


Ahan, i'm already on it, and by 'Order' do you mean the Cart in which the customer adds the items he chooses from each department and has the total bill ? Thanks mate.. :D
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:12 PM

Quote

and by 'Order' do you mean the Cart
No i don't:

Quote

You have a missing abstraction.
(note emphasis)
Was This Post Helpful? 0
  • +
  • -

#11 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:20 PM

ammaar12:

Even though an Order class may be appropriate, but for the sake of making this simple, you really don't need it. Instead, you need to correct your existing classes.

  • Refactor the Department class to store a collection of items, not the items in every department in the store. Expose methods to add new items.

    class Department
    {
    	private List<Item> items;
    	public boolean addItem(Item itm){}
    	.
    	.
    	.
    }
    
    


  • Before, opening the UI, you need to create Department instances and add the food items. Ideally, this information would come from an external source (e.g. file, DB, network)

    main()
    {
    	Department meatDept = new Department();
    	meatDept.addItem(...); 	meatDept.addItem(...); 	meatDept.addItem(...); // ....
    	Department bakeDept = new Department();
    	bakeDept.addItem(...);	bakeDept.addItem(...);	bakeDept.addItem(...); // ....
    	.
    	.
    	.
    	Cart cart = new Cart();
    	startUI(cart);
    	double total = 0;
    	for(Item itm : cart.getItems())
    		total += (itm.getPrice() * itm.getQuantity());
    	System.out.println("Total price: $"+String.format("%.2f", total));
    }
    
    


  • Your cart should simply contains items, nothing else.

    class Cart
    {
    	private List<Item> items;
    	public boolean addItem(Item itm){}
    	public List<Item> getItems(){}
    	.
    	.
    	.
    }	
    
    

Was This Post Helpful? 2
  • +
  • -

#12 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:33 PM

blackcompe

This was very very helpful, Thank You So much, I just kept on complicating things with this program..!! I'm gonna do this now, and let you know! By the way, professor suggested to do Polymorphism and Abstraction for this, how am I gonna do that? Do I make subclasses for departments as for each separate departments and handle them?

Thanks again..
Was This Post Helpful? 0
  • +
  • -

#13 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:35 PM

Quote

This was very very helpful, Thank You So much, I just kept on complicating things with this program..!! I'm gonna do this now, and let you know! By the way, professor suggested to do Polymorphism and Abstraction for this, how am I gonna do that? Do I make subclasses for departments as for each separate departments and handle them?


That would be a good way to demonstrate subclassing. Put anything common to all departments in the base class. There's your polymorphism. "Abstraction" doesn't necessarily necessitate the use of an abstract class. It could mean use a base class. Your requirements aren't complex enough to require the use of abstract classes. Once you start defining department specific implementations of methods, then you can use abstract classes.

Edit:

If you're going to create classes for each department, you might as well just make them singletons and also create the items in an initializer block. E.g.

class Department
{
	protected List<Item> items = new ArrayList<Item>;
	protected Department();
	public List<Item> addItem(Item itm);
	public Item findItem(String name);
	.
	.
	.
}

class BakeryDepartment extends Department
{
	{
		addItem(...);
		addItem(...);
		addItem(...);
	}
}

class Store
{
	private final static Department bakery = new BakeryDepartment();
	private final static Department meats = new MeatDepartment();
	.
	.
	.

	public static Department getDepartment(String name)
	{
		switch(name.toLowerCase())
		{
			case "bakery":
				return bakery;
			case "meats":
				return meats;
			.
			.
			.
		}
	}
}


This post has been edited by blackcompe: 23 November 2013 - 12:58 PM

Was This Post Helpful? 1
  • +
  • -

#14 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:44 PM

View Postblackcompe, on 23 November 2013 - 12:20 PM, said:


	public boolean addItem(Item itm){}
	.
	.
	.
}



And yeah why is the datatype for the method 'addItem' boolean? :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: I've a problem in displaying the attribute of object from list...

Posted 23 November 2013 - 12:46 PM

Sorry - but there's no relation between a department and the number of items a customer has purchased at one time
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »