473,394 Members | 1,749 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,394 software developers and data experts.

Need Urgent Help With Arrays

Can anyone teach me how to 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 , 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
  1. import java.io.*;
  2. import java.text.DecimalFormat;
  3.  
  4. public class CheckoutProgram {
  5.  
  6.     public void start() {
  7.         SalesItem[] items = getStock();
  8.  
  9.     }
  10.  
  11.     array of type SalesItem
  12.     private SalesItem[] getStock(){
  13.         SalesItem[] items = new SalesItem[1000];
  14.         try {
  15. BufferedReader br = new BufferedReader(new FileReader("stock.txt"));
  16.             String theLine;
  17.             int count = 0;
  18.             while ((theLine = br.readLine()) != null) {
  19.                  String[] parts = theLine.split(",");
  20.                  items[count] = new SalesItem(parts[0],parts[1],Double.parseDouble(parts[2]));
  21.                  if (parts.length==4){
  22. String discount = parts[3];
  23. String numPurchases = discount.substring(0, discount.indexOf("@"));
  24. String price = discount.substring(discount.indexOf("@")+1);
  25.  items[count].setNumPurchases(Integer.parseInt(numPurchases));
  26.  items[count].setDiscountedPrice(Double.parseDouble(price));
  27.                  }
  28.                  count++;                 
  29.             }
  30.         } 
  31.         catch (IOException e) {
  32.             System.err.println("Error: " + e);
  33.         }
  34.         return items;
  35.     }    
  36. }
  37.  
SalesItem.java

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

[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]
Sep 18 '07 #1
4 1220
Nepomuk
3,112 Expert 2GB
Hi Civicjai! Welcome to TSDN!

When posting code, please use the CODE tags (the # symbol in the Message editor).

For user input, have a look at the Scanner class.

For finding the correct item for a given input, there are several methods. If you want to use an array, you'll have to go through the items and check, if the item you're looking at has that specific number. Not very efficient, is it?

You can however use a TreeMap, which will be much more sufficient for your problem. Have a look at the API and try to use it.

Greetings,
Nepomuk
Sep 18 '07 #2
Hi Nepomuk
OKAY~ THANKS
Sep 18 '07 #3
JosAH
11,448 Expert 8TB
Hi Nepomuk
OKAY~ THANKS
I deleted your double post and put in some code tags in this one for readability.

kind regards,

Jos
Sep 18 '07 #4
kreagan
153 100+
Can anyone teach me how to 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 , a description of the item with its unit price is displayed.
[/code]
Use the Scanner class to get the code input. Scanner

As for the rest, pretend this is real life and not a program. If I give you that list and told you to look up code XXXX, what would you do?
Sep 18 '07 #5

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

Similar topics

3
by: avy31 | last post by:
Hy, I have a problem at which I can not find the solution: I have this: Declare Sub VRB_IO_StdInstel Lib "TWVRB408.DLL" Alias "_VRB_IO_StdInstel@12" (ByVal Mode As Integer, ByVal szIniFile As...
7
by: kalikoi | last post by:
I have two arrays as follows array1=("45101010","45101010","45103020","45103020","45103020","45201020","45201020", "45201020","45201020","45202010") ...
1
by: irfi | last post by:
Ok can anyone help me with this problem I have to create a program in NetBeans 4.0 and this is the requirement:- I have to create a base class which consists of EMPLOYEE NAME, CODE and...
7
by: thegreatest21 | last post by:
Right, I am making a Tax Calculator and need an array to store the data that has been typed in when the user is prompted. However, being completely new to Java I am having some difficulty getting to...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.