473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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


Expand|Select|Wrap|Line Numbers
  1. .CheckoutProgram.java.
  2.  
  3. import java.io.*;
  4. import java.text.DecimalFormat;
  5.  
  6. public class CheckoutProgram {
  7.  
  8.     public void start() {
  9.  
  10.         SalesItem[] items = getStock();
  11.  
  12.         System.out.print("Type item code (press enter to finish):");
  13.          String wordIn = Keyboard.readInput();
  14.  
  15.          SalesItem[] goods = new SalesItem[1000];
  16.  
  17.         int count = 0;    
  18.          while (wordIn.length()>=4 && wordIn.length()<=4){
  19.          for (int i=0;i<items.length;i++) {
  20.         if (items[i] != null && wordIn.equals(items[i].getItemCode())){
  21.         System.out.println(items[i]);
  22.             goods[count] = items[i];
  23.         }
  24.  
  25.         }
  26.          System.out.print("Type item code (press enter to finish):");
  27.          wordIn = Keyboard.readInput();
  28.         count++;
  29. }         
  30.  
  31.         System.out.println();
  32.         System.out.println("==========Bill==========");
  33.  
  34.         double amountDue = 0.0;
  35.  
  36.         for (int i=0; i<count; i++){
  37.  
  38.                 System.out.println(goods[i]);
  39.             amountDue = amountDue + goods[i].getUnitPrice();
  40.     }
  41.  
  42.  
  43.  
  44.  
  45.         System.out.println();
  46.         System.out.println("Amount due: $" + new DecimalFormat().format(amountDue));
  47.         System.out.println("Thanks for shopping with us!");
  48.  
  49. }
  50.  
  51.     // method to read in "stock.txt" and store the items for sale in an array of type SalesItem
  52.     private SalesItem[] getStock(){
  53.         SalesItem[] items = new SalesItem[1000];
  54.         try {
  55.             BufferedReader br = new BufferedReader(new FileReader("stock.txt"));
  56.             String theLine;
  57.             int count = 0;
  58.             while ((theLine = br.readLine()) != null) {
  59.                  String[] parts = theLine.split(",");
  60.                  items[count] = new SalesItem(parts[0],parts[1],Double.parseDouble(parts[2]));
  61.                  if (parts.length==4){
  62.                      String discount = parts[3];
  63.                      String numPurchases = discount.substring(0, discount.indexOf("@"));
  64.                      String price = discount.substring(discount.indexOf("@")+1);
  65.                      items[count].setNumPurchases(Integer.parseInt(numPurchases));
  66.                      items[count].setDiscountedPrice(Double.parseDouble(price));
  67.                  }
  68.                  count++;                 
  69.             }
  70.         } 
  71.         catch (IOException e) {
  72.             System.err.println("Error: " + e);
  73.         }
  74.         return items;
  75.     }    
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. }
  92.  
  93.  
  94. ..


Expand|Select|Wrap|Line Numbers
  1. ..SalesItem.java
  2.  
  3. import java.text.DecimalFormat;
  4.  
  5. public class SalesItem {
  6.     private String itemCode; //the item code
  7.     private String description; // the item description
  8.     private double unitPrice; // the item unit price
  9.  
  10.     // An item may offer a discount for multiple purchases 
  11.     private int numPurchases; //the number of purchases required for receiving the discount
  12.     private double discountedPrice; // the discounted price of multiple purchases
  13.  
  14.     // the constructor of the SalesItem class
  15.     public SalesItem (String itemCode, String description, double unitPrice){
  16.         this.itemCode = itemCode;
  17.         this.description = description;
  18.         this.unitPrice = unitPrice;
  19.     }
  20.  
  21.     // accessor and mutator methods
  22.  
  23.     public String getItemCode(){
  24.         return itemCode;
  25.     }
  26.  
  27.     public void setItemCode(String itemCode){
  28.         this.itemCode = itemCode;
  29.     }
  30.  
  31.     public String getDescription(){
  32.         return description;
  33.     }
  34.  
  35.     public void setDescription(String description){
  36.         this.description = description;
  37.     }
  38.  
  39.     public double getUnitPrice(){
  40.         return unitPrice;
  41.     }
  42.  
  43.     public void setUnitPrice(double unitPrice){
  44.         this.unitPrice = unitPrice;
  45.     }
  46.  
  47.     public int getNumPurchases(){
  48.         return numPurchases;
  49.     }
  50.  
  51.     public void setNumPurchases(int numPurchases){
  52.         this.numPurchases = numPurchases;
  53.     }
  54.  
  55.     public double getDiscountedPrice(){
  56.         return discountedPrice;
  57.     }
  58.  
  59.     public void setDiscountedPrice(double discountedPrice){
  60.         this.discountedPrice = discountedPrice;
  61.     }
  62.  
  63.     // the string representation of a SalesItem object
  64.     public String toString(){
  65.         return description + "/$" + new DecimalFormat().format(unitPrice);
  66.     }
  67. }
  68.  
  69. ..

Expand|Select|Wrap|Line Numbers
  1. ..stock.txt
  2. 1000,low-fat milk (1 litre),2.15
  3. 1001,good-morning cereal,5.60
  4. 1002,mango,1.89,2@2.50
  5. 1003,Coca-Cola (300 ml),2.5
  6. ..
Sep 25 '07 #1
4 1983
dmjpro
2,476 2GB
Use code Tags while you do Post.

And point out the specific fragment of Code.

Kind regards,
Dmjpro.
Sep 25 '07 #2
forse
7
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 Multidimensional or arrays of arrays in Java
Sep 25 '07 #3
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
NullPointerExceptions are generated during the execution of the program.
• To format a double variable e.g. amountDue, as money, use
“$" + new DecimalFormat().format(amountDue)
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
Sep 25 '07 #4
JosAH
11,448 Expert 8TB
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
Sep 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.