is an existing item code that matches an item in the items array , 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.
And also,If the entered item code cannot be matched to an item in the items array, the programshould continue with another prompt for typing an item code. For example, if the usercontinues using the program by typing the item code “6666” and then presses the Enterkey, another prompt for typing an item code is shown.
A lot of thanks =)
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import java.text.DecimalFormat;
- public class CheckoutProgram {
- public void start() {
- SalesItem[] items = getStock();
- }
- 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;
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.text.DecimalFormat;
- public class SalesItem {
- private String itemCode;
- private String description;
- private double unitPrice;
- private int numPurchases;
- private double discountedPrice;
- public SalesItem (String itemCode, String description, double unitPrice){
- this.itemCode = itemCode;
- this.description = description;
- this.unitPrice = unitPrice;
- }
- 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]
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
/code]