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