November 1st, 2012, 06:23 PM
-
Sales Associate Project
Hello,
for my Java course as a project we were to design a program that a sales associate would use to enter customer information, lookup prices and display the prices of items to the screen. well we were given a second project which builds upon this program. basiclly we were supposed to design a main menu for the sales associate and we were to put the main menu in a loop to where it repeated until the program was exited. my problem is I have a secondary menu for the sales associate to add items that a customer purchased, however I always get a run time error that the string index is out of bounds. I will post a copy of the code so you can get an idea.
import java.util.Scanner;
import java.io.*;
/*
* Created by Ryan Moore
* This Program will present customer information for a sales associate
*/
public class Moore_PP2
{
public static void main(String[] args) throws IOException
{
//constant
final double taxRate = 0.08;
//variables
double shoes;
double tShirts;
double shorts;
double caps;
double jackets;
double prodAmt; // holds the cost without tax
double cost; // holds the cost
double tax; // holds the tax of the product
double totalCost; // holds the total cost of the product
String cust;
String custName;
String custName2;
String custAdd; // holds the customers street address
String custCity; // holds the customers city, state, and zip
String custEmail; // holds the customers email
String input; // holds the input from option 3
char answer; // reads the first character from input on option 3
int quantity; // holds the quantity of the product purchased
int number; // holds the number entered on main screen
int price; // holds the number entered for price lookup
shoes = 50.00; // tells the price of shoes
tShirts = 30.00; // tells the price of tshirts
shorts = 75.00; // tells the price of shorts
caps = 15.00; // tells the price of caps
jackets = 100.00; // tells the price of jackets
// sets the accumulator to 0
totalCost = 0.0;
// used to read input from the keyboard
Scanner keyboard = new Scanner(System.in);
// displays the main menu
System.out.print("Welcome to the main menu. " + "Enter a number to continue.\n"
+ "Enter Customer information = 1\n" + "Price Lookup = 2\n"
+ "Display Total Bill = 3\n" + "Quit = 4\n");
number = keyboard.nextInt();
/* used to determine which number is entered
* the switch statement was used from chapter 3 in section 3.9 page 154
*/
switch (number)
{
case 1:
// open the file for writing
PrintWriter outputFile = new PrintWriter("Customer.txt");
// asks for the customers name
System.out.println("Enter Customers name");
keyboard.nextLine(); //used to consume a remaining newline
custName = keyboard.nextLine();
// writes the name to the file
outputFile.println(custName);
// asks for the customers address
System.out.println("Enter Customers Street Address");
System.out.println("For example: 123 Fake Street");
custAdd = keyboard.nextLine();
// writes the customers address to the file
outputFile.println(custAdd);
// asks for the customers ciry, state, and zip
System.out.println("Enter Customers City, State, and Zip Code.");
System.out.println("For Example: Someplace, Tx. 77327");
custCity = keyboard.nextLine();
// writes the customers city, state, and zip to the file
outputFile.println(custCity);
// asks for the customers email
System.out.println("Enter Customers Email");
custEmail = keyboard.nextLine();
// writes the customers email address to the file
outputFile.println(custEmail);
// closes the file
outputFile.close();
//used to print the input to the screen
System.out.println("Customer: " + custName);
System.out.println("Address: " + custAdd);
System.out.println("City, State, and Zip: " + custCity);
System.out.println("Email: " + custEmail);
System.out.println("has been added to the database.");
break;
case 2:
System.out.println("What Item would you like to look up?");
System.out.println("Please enter a number to continue.\n");
System.out.print("(1) Shoes.\n" + "(2) T-Shirts.\n" +
"(3) Shorts.\n" + "(4) Caps.\n" + "(5) Jackets.\n");
price = keyboard.nextInt();
// switch statement for price look up menu
switch (price)
{
case 1:
// displays the price for a pair of shoes
System.out.println("The price for a pair of shoes"
+ " is: $" +shoes);
break;
case 2:
// displays the price for a t-shirt
System.out.println("The price for a T-Shirt is: $" +
tShirts);
break;
case 3:
// displays the price for a pair of shorts
System.out.println("The price for a pair of shorts " +
"is: $" + shorts);
break;
case 4:
// displays the price for a cap
System.out.println("The price for a cap is: $" + caps);
break;
case 5:
// displays the price for a jacket
System.out.println("The price for a Jacket is: $"+
jackets);
break;
default:
System.out.println("Invalid Entry!");
System.out.println("Please enter a 1, 2, 3, 4, or 5.");
}
break;
case 3:
System.out.println("Enter Customers name");
keyboard.nextLine();
custName2 = keyboard.nextLine();
do
{
System.out.println("What did the Customer purchase?");
System.out.println("Type the word shoes, shirt, shorts, cap");
System.out.println("or jacket");
cust = keyboard.nextLine();
if (cust.equalsIgnoreCase("shoes"))
{
// asks the associate how many pairs the customer purchased
System.out.println("How many pairs?");
quantity = keyboard.nextInt();
prodAmt = shoes * quantity;
tax = prodAmt * taxRate;
cost = prodAmt + tax;
totalCost += cost;
}
System.out.println("Did the customer purchase anything else?");
System.out.println("Please Enter (1) for yes or (2) for no.");
input = keyboard.nextLine();
answer = input.charAt(0);
} while (answer == 'y' || answer == 'Y');
// opens the external file
File file = new File("Customer.txt");
Scanner inputFile = new Scanner(file);
// reads the first line of the file
String line = inputFile.nextLine();
// reads the second line of the file
String line2 = inputFile.nextLine();
// reads the third line of the file
String line3 = inputFile.nextLine();
// reads the fourth line of the file
String line4 = inputFile.nextLine();
// diplays the output of the customer info to the screen
// this part of the code isn't finished yet
System.out.println(line);
System.out.println(line2);
System.out.println(line3);
System.out.println(line4);
System.out.println("Product Purchased\t Quantity\t Total Cost");
break;
case 4:
// exits the program
System.exit(0);
break;
default:
System.out.println("Your input needs to be a 1, 2, 3, or 4.");
}
}
}
/** the switch statements come from chapter 3 section 3.9
* the println methods came from chapter 2 section 2.2
* the scanner class was used from chapter 2 section 2.13
* the various math calculations came from chapter 2
*/
the problem is with the if statement, I've tried with a switch statement and it has the same problem however if I omit the if statement the loop works just fine. any ideas on what my problem is?
November 5th, 2012, 08:19 AM
-
Please edit your post and wrap the code with code tags.
get a run time error that the string index is out of bounds.
Please post the full text of the error message that shows where the error happens and what the value of the index was.