-1

I am attempting to run a bit of code for a school program. In this I have to create a class used to store the item number and price in order to find a discount.

I am using repl.it, an internet compiler for various languages. I have read through here and seen many posts regarding class path when running in terminal. Since everything is local to the repl.it program I am not sure what is causing these errors since I would assume all paths would be available to the programs. I have posted the code below. Thank you for any and all help.

public class Inventory
{
  private String itemNumber;
  private double origPrice;

  public Inventory()
  {
    itemNumber = " ";
    origPrice = 0.0;
  }// end default constructor

  public Inventory (String newItemNumber, double newOrigPrice)
  {
    itemNumber = newItemNumber;
    origPrice = newOrigPrice;
  }// end overloaded constructor

  //Mutators
  public void setItemNumber(String newItemNumber)
  {
    this.itemNumber = newItemNumber;
  }// end setItemNumber

  public void setOrigPrice(double newOrigPrice)
  {
    this.origPrice = newOrigPrice;
  }// end setOrigPrice

  //Accessors
  public String getItemNumber()
  {
    return this.itemNumber;
  }// end getItemNumber

  public double getOrigPrice()
  {
    return this.origPrice;
  }// end getOrigPrice

  public void printSalesData() 
  {
    int days = 0;
    double discount = .1;

    while (days <= 7)
    {
      origPrice = origPrice * discount;
      System.out.println("The sales price after " + days + " day(s)     is: " + origPrice);      
    }// end while 
  }// end printSalesData   

}

For my main the code is:

import java.util.Scanner;
class UseInventory
{
  public static void main (String [] args)
  {

    String input = " ";

    Inventory inv1 = new Inventory();


    Scanner user = new Scanner(System.in);

    while (input.equals("y"))//fix this line
    {
      System.out.println("Please enter the item number: ");

      inv1.setItemNumber(user.nextLine());

      System.out.println("Please enter the times original price: ");

      inv1.setOrigPrice(user.nextDouble());

      System.out.println("Do you have any other items to enter? y or n.";

      input = user.nextLine();

    }//end while

  }//end main

}//end class
  • 1
    Sidenote: that's not how you compare strings, see [here](http://stackoverflow.com/q/513832/5647260) – Andrew Li Nov 27 '16 at 19:05
  • Also, you are not updating the value of `input` inside the while loop. I'm guessing you want the user to enter `y` or `n` – denvercoder9 Nov 27 '16 at 19:07
  • Thank you Andrew, I was in a haste to get this in here and didnt spot check. Will edit and fix – Jack Roudebush Nov 27 '16 at 19:08
  • And Rafiduzzaman, it is meant to continue until the user specifies they have no other items to enter, so yes you are correct. Right now, the section of code is unfinished as it will display do you have anything else to enter, hence the y or n – Jack Roudebush Nov 27 '16 at 19:09
  • @JackRoudebush But you are not taking that input from the user. You should read a string for y or n in the while loop after reading the prices. – denvercoder9 Nov 27 '16 at 19:12
  • I see what you are saying @RafiduzzamanSonnet I have edited – Jack Roudebush Nov 27 '16 at 19:18
  • But you are still comparing strings the wrong way. Did you see the link Andrew provided? – denvercoder9 Nov 27 '16 at 19:26
  • Yes, i am still working on it. New to coding and am reading the post he sent over so I can understand it – Jack Roudebush Nov 27 '16 at 19:29
  • @JackRoudebush take a look at [this](http://www.javatpoint.com/string-comparison-in-java). What you are looking for is `equals()` method – denvercoder9 Nov 27 '16 at 19:59
  • I edited the code above, do you think I should create an alternate variable for n as String quit = "n" and then input.equals(input) continue to loop and input.equals(quit) to stop it? – Jack Roudebush Nov 27 '16 at 20:04

1 Answers1

1

I rewrote the program with the edits provided by Andrew and Rafiduzzaman and put them in an actually IDE and it ran perfectly. Thank you for the help.

  • 1
    Yeah, repl.it is lame because they sorta require you to name your class "Main" and you also can't mark it public. Annoying. – djangofan Jun 24 '17 at 17:09