Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help with java shop project

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with java shop project

    My assignment

    You will be creating a program for a shop which sells some products by the pound (weight) and others by the dozen. Items such as bananas, coke cans and cookies are sold by the dozen; apples, rice, vegetables are sold by the pound.

    Your test class code MUST create an array of Shop objects (using polymorphism) to hold as many products the customer can buy (make the size 30). Then ask the user to enter the items purchased by customer. The user should enter name of the item, its price, followed the number of particular items or weight in pounds depending on type of item. Use the word “end” to terminate the list of items purchased by customer.

    Maintain a counter while entering the items as customer can buy any number of products.

    Display the products in ascending order by price with their total cost and at the end display the total purchase or grand total for the customer’s purchase.


    Heres my code:


    Superclass

    ackage Shop;
     
     
    public class Shop {
     
     
    		public String name;
    		public double price;
    		public double total;
    		//private double quantity;
     
     
     
    		public String getNames() {
    			return name;
    		}
    		public void setName(String name) {
    			this.name = name;
    		}
     
    		public double getPrice() {
    			return price;
    		}
    		public void setPrice(double price) {
    			this.price = price;
    		}
     
     
    		public double getTotal() {
    			return total;
    		}
    		public void setTotal(double total) {
    			this.total = total;
    		}
     
     
     
    		public String toString()
    		{
    			return "this is a " + name + getPrice();
    		}
    		public  double getCalculateAmount() {
    			return total;
     
     
    		}
     
     
     
     
     
    		}

    sublcass called Poundshop

    package Shop;
     
    public class PoundShop extends Shop {
    	private int weight;
     
    	public PoundShop() {
     
    	}
     
    	public int getWeight() {
    		return weight;
    	}
     
    	public void setWeight(int weight) {
    		this.weight = weight;
    	}
     
     
    	public double calculateAmount() {
     
    		return weight  * getPrice();
     
     
     
    	    }
     
    }

    subclass called DozenShop
    package Shop;
     
    public class DozenShop extends Shop{
     
    	public   int quantity;
     
     
     
    	public int getQuantity() {
    		return quantity;
    	}
     
     
    	public void setQuantity(int quantity) {
    		this.quantity = quantity;
    	}
     
     
    	public double getCalculateAmount() {
     
    		return quantity/12 * getPrice();
     
     
    	}
     
     
     
     
    	    }

    Tester

    import java.util.Scanner;
     
     
    import Shop.*;
     
    public class ShopTester {
     
    	 static Scanner input = new Scanner(System.in);
     
    	 public static void main(String[] args) {   
     
     
    		 Shop[] shops = new Shop[30];
    		 shops[0] = new DozenShop();
    		 shops[1] = new PoundShop();
     
    		 for(int i = 0; i < 30; i++) {
    			    int a = 0;
    				Shop s = shops[a];
    			    System.out.println("Please enter name, price and quantity");
    				String data = input.nextLine();
    				String splitData[] = data.split(" ");
     
     
    	 }
     
     
    	 }
     
    }



    I need to get input from the user and store it into a string and then when they enter quantity, If its double its pound, If its int its dozens


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with java shop project

    Good first post, but your question is a bit vague. Do you know how to use Scanner to get input from the keyboard? If so, store that in a String variable. After that, it could be hard to tell if the user entered a double value or an int value, unless you can assume a double value will always have a decimal component, but I don't think that's a good assumption. How were you thinking you'd do it? Or, why can't you just ask the user if the value is pounds or dozens?

    Edit: Rereading, number or weight depends on the type of the item being purchased. It's like when you scan a produce item at those self-service check-out machines, the machine will either weigh the item and determine total price based on the items price/pound or ask the user to input how many items are being purchased. So your items should indicate whether their price is based on quantity or weight.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java shop project

    This is what the output should look like.

    I'm Trying to firgure out how to separate the pound and dozens

    Edit: image looked to small

    http://i.imgur.com/FoeASee.png
    Attached Images Attached Images

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with java shop project

    So there are items, and you should have a class for the items. Some of the items are sold by quantity (or dozen, but that's a goofy way to sell a banana), and other items are sold by the pound. Maybe you could have a super class called "Item" with subclasses "PoundItem" and "DozenItem." The item being purchased (the object representing that item) will then indicate to the 'machine' calculating the total for that item if it is being purchased by the pound or by the dozen, and the item will either be weighed or the quantity for that item requested.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java shop project

    can you write it in code I'm not sure what your trying to explain

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with java shop project

    No, I can't write it for you, but I'll try to explain it better.

    What you have in the code you posted is not that far off. I wouldn't use those names (DozenShop and PoundShop), because they don't describe accurately what they are. But that's okay, let's work with those for a bit. When instances of those objects, let's just pick one, say a banana, which has to be measured by the dozen, is presented to be purchased, a method could be called. That method would specify the units required to buy that item. Let's say the method is called calculatePurchasePrice(). There would be a method in both DozenShop and PoundShop of the same name.

    When the bananas are presented for purchase, the method would be called:

    banana.calculatePurchasePrice();

    And the method would ask the user,

    "How many do you have?" (bananas may or may not be specified)

    After the user inputs how many, the numbers of bananas would be converted to dozens, and the price would be calculated.

    A method of the same name, calculatePurchasePrice(), in PoundShop might ask:

    "What does the item weigh?"

    The method calculatePurchasePrice() might be specified in the super class Shop but left empty for each subclass to override and define.

    Hope this helps. Good luck!

Similar Threads

  1. Shop
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 13th, 2012, 06:46 PM