473,401 Members | 2,146 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,401 software developers and data experts.

I need to know how to enter items into array and display them correctly. my try.

//~--- JDK imports ------------------------------------------------------------

import java.text.*;
import java.io.Console;
import java.util.regex.*;
import java.util.Arrays;

public class A3P2 {
public static void main(String[] args) {
final int MAX = 25;
String userInput,
str2 = "",
str3 = "",
moreItems = "";
String[] itemNum = new String[MAX];
String[] itemDescription = new String[MAX];
int[] qtyOnHand = new int[MAX];
double[] itemPrice = new double[MAX];
int totalValue = 0,
count = 0, rowNum,
rows = 0,
index = 0,
qoh = 0;
double totalCost, priceIn;
NumberFormat nfc = NumberFormat.getCurrencyInstance(); // To format outputs to currency
boolean blnEntryIsValid = true, blnCodeFound = true;
String error1 = "**ERROR01** - Item number must be five characters\n\n";
String error2 = "\n\n**ERROR02** - Department code does not exist\n\n";
String error3 = "**ERROR03** - Item number must be numeric in last 3 positions";
String error4 = "**ERROR04** - Item description is required and must be max 20 chars";
String error5 = "**ERROR05** - Unit price must be between 0 and 5,000.00";
String error6 = "**ERROR06** - Quantity on hand must be between 0 and 500";

// two-dimm array...row ... col
String[][] strDeptInfo = {
{ "AU", "Automotive" },
{ "CO", "Cosmetics" },
{ "CW", "Childrens Wear" },
{ "EL", "Electronics" },
{ "FD", "Food" },
{ "FR", "Furniture" },
{ "HW", "Hardware" },
{ "JE", "Jewelry" },
{ "KB", "Kitchen & Bath" },
{ "MW", "Mens Wear" },
{ "PH", "Pharmacy" },
{ "TO", "Toys" },
{ "WW", "Womens Wear" }
};

TextIO.putln("JavaMart Inventory System");
TextIO.putln("Programmed by Cal Schroeder");
TextIO.putln("for: Stephen Lawrence");
TextIO.putln();
TextIO.putln();
{
do {
count++;
index++;
TextIO.put("\nEnter Item Number: ");
userInput = TextIO.getlnString().toUpperCase();
TextIO.putln(userInput);
str2 = userInput.substring(0, 2);
str3 = userInput.substring(2, 5);
TextIO.putln("str3 = " + str3);
TextIO.putln("str2 = " + str2);
if ((userInput.length() != 5)) {
blnEntryIsValid = false; // If entry is not 5 characters
TextIO.put(error1 + "\n");
}
{
// Check if first two characters match list
rows = strDeptInfo.length; // Number of rows
blnEntryIsValid = false;
for (rowNum = 0; rowNum < rows; rowNum++)
{
if (strDeptInfo[rowNum][0].compareToIgnoreCase(str2) == 0)
{
blnCodeFound = false;
rowNum = rows - 1; // Allows a quick exit when found
TextIO.putln("Code Found " + str2);
TextIO.putln();
}
}
if (blnCodeFound)
do {
TextIO.putln("**ERROR02** - " + "Department code does not exist");
TextIO.putln("Choose one of the following...");
System.out.println();

for (rowNum = 0; rowNum < rows; rowNum++)
{
TextIO.putln(strDeptInfo[rowNum][0] + " - " + strDeptInfo[rowNum][1]);
}

System.out.println();
}while (blnEntryIsValid);
else
{
//last 3 characters, should be numbers
if (!Character.isDigit(str3.charAt(0)) || !Character.isDigit(str3.charAt(1)) || !Character.isDigit(str3.charAt(2)))
{
TextIO.putln("**ERROR03** - " + "Item number must be numeric in last 3 positions");
blnEntryIsValid = true;
System.out.println();
}

{
//If all is valid then it gets to this point
itemNum[index] = userInput;
}
}

do {
//Enter Description
System.out.println("Enter item Description (1 ~ 20 char): ");
userInput = TextIO.getlnString();

//Inventory item description – must contain a value and be
//less than or equal to 20 characters in length.

if (userInput == null || userInput.length() > 20)
{
blnEntryIsValid = false;
TextIO.putln(error4);

}else
{
// Read values into the array

itemDescription[index] = userInput;
TextIO.putln("Item Desription is " + itemDescription[index] + "\n");
blnEntryIsValid = true;

}

}while (blnEntryIsValid==false);
}do
{
//Unit price
TextIO.putln("Enter unit price: ");
priceIn = TextIO.getDouble();
//Unit price – between 0 and 5,000.00 inclusive.

if (priceIn > 0 || priceIn > 5000)
{
itemPrice[index] = priceIn;

if (priceIn < 0 || priceIn > 5000)
{
blnEntryIsValid = false;
TextIO.putln(error5);

}
else
{
TextIO.putln("\n\nItem Price is " + itemPrice[index] + "\n");
blnEntryIsValid = true;
}do
{
//Quantity on hand
TextIO.putln( "Enter Quantity On Hand" );
qoh = TextIO.getlnInt();
//Quantity-on-hand – between 0 and 500 inclusive.
if (qoh < 0 || qoh > 500)
{
blnEntryIsValid = false;
System.out.println(error6);
}
else
{
qtyOnHand[0] = qoh;
blnEntryIsValid=true;


{
for (rowNum = 0; rowNum < qtyOnHand.length; rowNum++)
showArray(qtyOnHand[rowNum]);
}
TextIO.putln("Any More Items y/n");
moreItems = TextIO.getlnString();

}

}while (!blnEntryIsValid);

}

}while (!blnEntryIsValid);

}while (moreItems.equalsIgnoreCase("y"));
}TextIO.putln("you have entered " + count);
}
}
Apr 20 '10 #1
0 1190

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

Similar topics

9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
2
by: JackM | last post by:
Let me attempt to explain my problem. I have a crude php script that takes a text list of songs that was generated by an mp3 list program and translates each entry into the form where they can be...
4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
1
by: Benton2862 | last post by:
I need help with and array for a program that acts like a cash register. The user will input an items price and then there payment. It will tell you the change and then tell you what kinda of change...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
2
by: HarisHohkl | last post by:
Hi, I've this function in a class to update the total value.but when i try to remove the these row highlight in Bold it crash, what should i do???? void display_total_value() { double...
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
0
by: jac130 | last post by:
i need to populate an array with grades. there is an 'add students & grades' button. user is prompted by input box to enter a students name, then another input box to enter their grade. the student's...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.