Discounting program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • civicjai
    New Member
    • Sep 2007
    • 4

    Discounting program

    Can anyone teach me how to wirte this
    Many Thanks

    Supermarkets sometimes have discounts on multiple purchases. For example, the unitprice for “mango” is $1.89, but buying two of the product will cost $2.50. The discount information is stored with each item in the items array.

    For those items that offer a discount for multiple purchases, the discounted price is shown when it applies.

    An example is shown below:
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):
    ==========Bill= =========
    mango/$1.89
    mango/discounted (2@2.5) $0.61
    mango/$1.89
    mango/discounted (2@2.5) $0.61
    Amount due: $5
    Thanks for shopping with us!


    --------------------------------------------------------------------------------
    For the code i write so far the Bill i only can print the bill like this

    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):1002
    mango/$1.89
    Type item code (press enter to finish):
    ==========Bill= =========
    mango/$1.89
    mango/$1.89
    mango/$1.89
    mango/$1.89
    Amount due: $5
    Thanks for shopping with us!

    Please Help me. And please give me more detail, cause this is my first year to learn java, i want to learn more and make me more understand~ A lot of thanks


    Code:
    .CheckoutProgram.java.
    
    import java.io.*;
    import java.text.DecimalFormat;
    
    public class CheckoutProgram {
    	
    	public void start() {
    		
    		SalesItem[] items = getStock();
    						
    		System.out.print("Type item code (press enter to finish):");
     		String wordIn = Keyboard.readInput();
     		
     		SalesItem[] goods = new SalesItem[1000];
    		
    		int count = 0;	
     		while (wordIn.length()>=4 && wordIn.length()<=4){
    	 	for (int i=0;i<items.length;i++) {
    		if (items[i] != null && wordIn.equals(items[i].getItemCode())){
    		System.out.println(items[i]);
    			goods[count] = items[i];
    		}
    				
    		}
     		System.out.print("Type item code (press enter to finish):");
     		wordIn = Keyboard.readInput();
    		count++;
    } 		
     		
    		System.out.println();
    		System.out.println("==========Bill==========");
    				
    		double amountDue = 0.0;
    		
    		for (int i=0; i<count; i++){
    					
    				System.out.println(goods[i]);
    			amountDue = amountDue + goods[i].getUnitPrice();
    	}
    		
    	
    			
    			
    		System.out.println();
    		System.out.println("Amount due: $" + new DecimalFormat().format(amountDue));
    		System.out.println("Thanks for shopping with us!");
    
    }
    
    	// method to read in "stock.txt" and store the items for sale in an array of type SalesItem
    	private SalesItem[] getStock(){
    		SalesItem[] items = new SalesItem[1000];
    		try {
    		    BufferedReader br = new BufferedReader(new FileReader("stock.txt"));
    		    String theLine;
    		    int count = 0;
    		    while ((theLine = br.readLine()) != null) {
    		         String[] parts = theLine.split(",");
    		         items[count] = new SalesItem(parts[0],parts[1],Double.parseDouble(parts[2]));
    		         if (parts.length==4){
    		        	 String discount = parts[3];
    		        	 String numPurchases = discount.substring(0, discount.indexOf("@"));
    		        	 String price = discount.substring(discount.indexOf("@")+1);
    		        	 items[count].setNumPurchases(Integer.parseInt(numPurchases));
    		        	 items[count].setDiscountedPrice(Double.parseDouble(price));
    				 }
    		         count++;		         
    		    }
    		} 
    		catch (IOException e) {
    		    System.err.println("Error: " + e);
    		}
    		return items;
    	}	
    
    	
    
    
    
    
    	
    	
    	
    	
    	
    	
    	
    	
    	
    }
    
    
    ..


    Code:
    ..SalesItem.java
    
    import java.text.DecimalFormat;
    
    public class SalesItem {
    	private String itemCode; //the item code
    	private String description; // the item description
    	private double unitPrice; // the item unit price
    	
    	// An item may offer a discount for multiple purchases 
    	private int numPurchases; //the number of purchases required for receiving the discount
    	private double discountedPrice; // the discounted price of multiple purchases
    		
    	// the constructor of the SalesItem class
    	public SalesItem (String itemCode, String description, double unitPrice){
    		this.itemCode = itemCode;
    		this.description = description;
    		this.unitPrice = unitPrice;
    	}
    	
    	// accessor and mutator methods
    	
    	public String getItemCode(){
    		return itemCode;
    	}
    	
    	public void setItemCode(String itemCode){
    		this.itemCode = itemCode;
    	}
    	
    	public String getDescription(){
    		return description;
    	}
    	
    	public void setDescription(String description){
    		this.description = description;
    	}
    	
    	public double getUnitPrice(){
    		return unitPrice;
    	}
    	
    	public void setUnitPrice(double unitPrice){
    		this.unitPrice = unitPrice;
    	}
    	
    	public int getNumPurchases(){
    		return numPurchases;
    	}
    	
    	public void setNumPurchases(int numPurchases){
    		this.numPurchases = numPurchases;
    	}
    	
    	public double getDiscountedPrice(){
    		return discountedPrice;
    	}
    	
    	public void setDiscountedPrice(double discountedPrice){
    		this.discountedPrice = discountedPrice;
    	}
    	
    	// the string representation of a SalesItem object
    	public String toString(){
    		return description + "/$" + new DecimalFormat().format(unitPrice);
    	}
    }
    
    ..

    Code:
    ..stock.txt
    1000,low-fat milk (1 litre),2.15
    1001,good-morning cereal,5.60
    1002,mango,1.89,2@2.50
    1003,Coca-Cola (300 ml),2.5
    ..
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Use code Tags while you do Post.

    And point out the specific fragment of Code.

    Kind regards,
    Dmjpro.

    Comment

    • forse
      New Member
      • Sep 2007
      • 7

      #3
      I read your question twice and still cannot understand what you want.
      Be more precise on what you want..

      I can only guess that you are using an ARRAY and you have problems dealing with two dimensional arrays.

      If that is the case then you may read a Tutorial on Multidimensiona l or arrays of arrays in Java

      Comment

      • civicjai
        New Member
        • Sep 2007
        • 4

        #4
        Part one: simple checkout
        The checkout program records purchases. An item code is to be entered by the user. If it
        is an existing item code that matches an item in the items array (as given in
        CheckoutProgram .java), a description of the item with its unit price is displayed.
        For example, if the user types the item code “1000” and then presses the Enter key,
        “low-fat milk (1 litre)/$2.15” is displayed on the screen, followed by
        another prompt for typing the next item code.
        Type item code (press enter to finish):1000
        low-fat milk (1 litre)/$2.15
        Type item code (press enter to finish):
        If the entered item code cannot be matched to an item in the items array, the program
        should continue with another prompt for typing an item code. For example, if the user
        continues using the program by typing the item code “6666” and then presses the Enter
        key, another prompt for typing an item code is shown.
        Type item code (press enter to finish):1000
        low-fat milk (1 litre)/$2.15
        Type item code (press enter to finish):6666
        Type item code (press enter to finish):
        If the user presses the Enter key at the prompt without first typing an item code, the
        program finishes executing by displaying a bill, which lists all the items bought (item
        description and unit price), and the total amount due. An example is shown below:
        Type item code (press enter to finish):1000
        low-fat milk (1 litre)/$2.15
        Type item code (press enter to finish):1001
        good-morning cereal/$5.6
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):1003
        Coca-Cola (300 ml)/$2.5
        Type item code (press enter to finish):


        ==========Bill= =========
        low-fat milk (1 litre)/$2.15
        good-morning cereal/$5.6
        mango/$1.89
        Coca-Cola (300 ml)/$2.5
        Amount due: $12.14
        Thanks for shopping with us!
        Hint:
        • To implement this program, you need to store the purchases in an array. The size
        of the array should be declared as a big number, say 1000. You also need to use
        an integer counter to count the actual number of purchases.
        • You MUST break your program into methods. For example, you may have:
        - a method to display a purchased item on the screen,
        - a method to record the purchased items,
        - a method to calculate the amount due.
        • Null values in the array need to be handled well so that no
        NullPointerExce ptions are generated during the execution of the program.
        • To format a double variable e.g. amountDue, as money, use
        “$" + new DecimalFormat() .format(amountD ue)
        Part two: multiple purchase discounts
        Supermarkets sometimes have discounts on multiple purchases. For example, the unit
        price for “mango” is $1.89, but buying two of the product will cost $2.50. The
        discount information is stored with each item in the items array.
        The checkout program displays a bill similar to Part One, but for those items that offer a
        discount for multiple purchases, the discounted price is shown when it applies.
        An example is shown below:
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):
        ==========Bill= =========
        mango/$1.89
        mango/discounted (2@2.5) $0.61
        mango/$1.89
        mango/discounted (2@2.5) $0.61
        Amount due: $5
        Thanks for shopping with us!

        In the above example, four of the “mango” are purchased. The multiple purchase
        discount for “mango” (2@2.50) applies to every two “mango” items. So the program
        gives every second “mango” a discounted price ($0.61) which makes two purchases
        $2.50 (every first “mango” is still priced using its unit price $1.89). Another
        example is shown below:
        Type item code (press enter to finish):1000
        low-fat milk (1 litre)/$2.15
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):1001
        good-morning cereal/$5.6
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):1003
        Coca-Cola (300 ml)/$2.5
        Type item code (press enter to finish):1002
        mango/$1.89
        Type item code (press enter to finish):
        ==========Bill= =========
        low-fat milk (1 litre)/$2.15
        mango/$1.89
        good-morning cereal/$5.6
        mango/discounted (2@2.5) $0.61
        Coca-Cola (300 ml)/$2.5
        mango/$1.89
        Amount due: $14.64
        Thanks for shopping with us!

        To work out whether the price is to be a discounted price, you need to count the
        number of previous purchases for items with the same item code.

        -----------------------------------------------------------------------------------------------------
        i already done the part1 ,but i am not sure i done it correct or not~
        but i can't do part 2, i have no idea how to do part2 ~ and i haven't learn two dimensional arrays, so i think i can't use it for this assignment. can i use another way to slove part 2

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Arrays are sooo Fortranesque; Java is a little object oriented language. I know this
          is homework, but aren't you allowed to use simple classes? I can imagine a
          DiscountInfo class associated with an item; both stored in some sort of Map.
          The Map is controlled by a simple DiscountManager you can consult given an
          item and the amount of items bought ...

          kind regards,

          Jos

          Comment

          Working...