So I have been trying to figure out how to do this for a week now. I am behind in the class due to an accident at work so I have not been participating. I started a chemical inventory program using net-beans IDE and have several errors which I don't understand? The teacher is no help and I've read and viewed video on the subject to no avail. Can some one look at my code and tell me what is wrong please. Below is my code now be gentle as I am VERY new to this. Any help would be appreciated. And this is my first post. Below the code is the requirement for week 4. Thanks in advance!
package chemical.inventory;
import java.util.Scanner;
/**
*
* #author Chris
*/
public class ChemicalInventory {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
Chemical[] chemical;
chemical = new Chemical[4];
chemical[0] = new Chemical("Sodium Hypochlorite","1084","1.29","5000");
chemical[1] = new Chemical("Caustic Soda","2010","1.25","5000");
chemical[2] = new Chemical("Sulfuric Acid","1098","2.10","3000");
chemical[3] = new Chemical("Lime","1010","0.93","2500");
double chemicalValue = 0.0;
System.out.printf("\nInventory value is: $%,2f\n",chemicalValue);
}
private static class Chemical {
private String sodium_Hypochlorite;
public Chemical() {
}
private Chemical(String sodium_Hypochlorite, String string, String string0, String string1) {
this.sodium_Hypochlorite = sodium_Hypochlorite;
}
}
}
class Chemical.Inventory
{
protected String chemicalName;
protected double chemicalPrice;
protected double chemicalStock;
protected double chemicalitemNumber;
public ChemicalInventory(String name, double price, double stock, double itemNumber);
static {
String name;
this.chemicalTitle = title;
double price = 0;
this.chemicalPrice = price;
double stock = 0;
this.chemicalStock = stock;
double itemNumber = 0;
this.chemicalitemNumber = itemNumber;
}
Chemical(String string, String string0, String string1, String string2) {
}
public void setChemicalName(String name)
{
this.chemicalName = name;
}
public String getChemicalName()
{
return chemicalName;
}
public void setChemicalPrice(double price)
{
this.chemicalPrice = price;
}
public double getChemicalPrice()
{
return chemicalPrice;
}
public void setChemicalStock(double stock)
{
this.chemicalStock = stock;
}
public double getChemicalStock()
{
return chemicalStock;
}
public void setChemicalitemNumber(double itemNumber)
{
this.chemicalitemNumber = itemNumber;
}
public double getChemicalitemNumber()
{
return chemicalitemNumber;
}
public double getValue()
{
return this.chemicalStock * this.chemicalPrice;
}
static {
System.out.println();
System.out.println( "Chemical Name:" + chemical.getChemicalTitle());
System.out.println("Chemical Price:" + chemical.getChemicalPrice());
System.out.println("Chemical gallons in stock:" + chemical.getChemicalStock());
}
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
Add a company logo to the GUI using Java™ graphics classes.
What is wrong with your code, a lot:
You defined Scanner input = new Scanner (System.in); and you never use it
Naming convention, field String sodium_Hypochlorite; i believe should be named name
class Chemical.Inventory dot is invalid character for names in java. If you want define your class to be inner class of other class, place it there, if you want it to be on some path, use packages.
constructor defined in class class Chemical.Inventory first does not have a body
Both constructors defined in class Chemical.Inventory has not matching names with class.
your first static initializer in class Chemical.Inventory is using this and some undefined variables. Logic from that block should be declared in constructor.
your second static initializer in class Chemical.Inventory is using variable chemical which wasnt defined anywhere.
Related
Excuse me, I'm new to this.
I am currently using BlueJ and going through YouTube tutorials on Eclipse, but I need to use BlueJ for my assignment
I simply have to make a GUI called GadgetShop (which I have done), and it has buttons which add info from my Mobile and MP3 class. There is also a class called Gadget, which is the super-class.
So the issue I am having is with ArrayLists and gathering the info from the classes to display it in the Text Boxes I have made. I made an array list but I'm not sure what's wrong, because it says constructor Gadget in class Gadget cannot be applied to given types;
Here's the code needed in the GadgetShop:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class GadgetShop implements ActionListener
{
private JTextField model, price, weight, size, credit, memory, phoneNo, duration, download, displayNumber;
private JButton addMobile, addMP3, clear, displayAll;
//These JTextField's are for the labels
private JTextField model2, price2, weight2, size2, credit2, memory2, phoneNo2, duration2, download2, displayNumber2;
private JFrame frame;
private ArrayList<Gadget> gadgetDetails;
public GadgetShop()
{
makeFrame();
}
public static void main (String args[]){
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget Object = new Gadget();
GadgetList.add(Object.Gadget(model, price, weight, size));
}
public void addGadget(Gadget newGadget)
{
gadgetDetails = new ArrayList<Gadget>();
gadgetDetails.add(newGadget);
model.setText("s6");
My Gadget is this:
/**
* Gadget that is a super class for the Mobile Phone and MP3.
* Needs input for the price, weight, model and size.
*/
public class Gadget
{
// Variables
public double price;
public int weight;
public String model;
public String size;
/**
* Constructor for objects of class Gadget
*/
public Gadget(double ThePrice, int TheWeight, String TheModel, String TheSize)
{
// initialise instance variables
price = ThePrice;
weight = TheWeight;
model = TheModel;
size = TheSize;
}
public double price()
{
return price;
}
public int weight()
{
return weight;
}
public String model()
{
return model;
}
public String size()
{
return size;
}
public void print()
{
System.out.println("The price of the gadget is " + price + " pounds");
System.out.println("The weight is " + weight + " grams");
System.out.println("The model is " + weight);
System.out.println("The size is " + size);
}
}
What does it actually mean so I can fix this, and what's the recommended way to gather information from my classes to be entered into my text boxes when clicking the button? (I know how to activate button on clicks and add the information, but just don't know the best way to retrieve it)
Thank you for reading, I want to learn so this would help me extremely.
Your constructor for Gadget takes arguments:
public Gadget(double ThePrice, int TheWeight, String TheModel, String TheSize)
{
// initialise instance variables
price = ThePrice;
weight = TheWeight;
model = TheModel;
size = TheSize;
}
You cannot simply call new Gadget() because the constructor needs four arguments. You have two options: firstly, you can provide the arguments in your main method:
public static void main (String args[]){
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget object = new Gadget(1.00,20,"a model", "big");
GadgetList.add(object);
}
Substitute whatever values make sense. The other solution is too create another constructor which doesn't take arguments in your Gadget class:
public Gadget() {
//initialize values to default values
price = 1.00
weight = 21
// etc.
}
You still have to fix the main method a little bit:
public static void main (String args[]){
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget object = new Gadget();
GadgetList.add(object);
}
I need help with a specific problem in my class project. The goal of the project is to create a program in which you can register how much shares you own. Information that's required is the company name, how many shares you own and their respective value. I created a GUI class and a class where the information is transferred to. The input comes from a private void. I'm having trouble finding a way to transfer the input from the private void to a an arraylist in a class outside it.
Here is how I initialized the arraylist in the GUI class.
public class GUISharePortfolio_1 extends javax.swing.JFrame {
ArrayList<SharePackage.Share> Package = new ArrayList<SharePackage.Share>();
Next is how I get the company name, number of shares and their value from the GUI. Since it is a private void I have to transfer that information to the SharePackage class.
private void CreatePortfolioButtonActionPerformed(java.awt.event.ActionEvent evt) {
String name;
double number;
double value;
name = CompanyNameField.getText();
number = Double.parseDouble(NumberOfSharesField.getText());
value = Double.parseDouble(ValueOfShareField.getText());
Package.CompanyName(name);
Package.NumberOfShares(number);
Package.ValueOfShare(value);
}
I'm getting an error saying "cannot find symbol" under the CompanyName, NumberOfShares and ValueOfShare.
The public class to which the info should be transferred is this:
package shareportfolio;
import java.util.ArrayList;
public class SharePackage
{
private ArrayList<Share> Package = new ArrayList<Share>();
public class Share
{
private String companyname;
private double numberofshares;
private double valueofshare;
Share(String companyname, double numberofshares, double valueofshare)
{
this.companyname = companyname;
this.numberofshares = numberofshares;
this.valueofshare = valueofshare;
}
public void setCompanyName(String name)
{
companyname = name;
}
public String getCompanyName()
{
return(companyname);
}
public void setNumberOfShares(double number)
{
numberofshares = number;
}
public double getNumberOfShares()
{
return(numberofshares);
}
public void setValueOfShare(double value)
{
valueofshare = value;
}
public double getValueOfShare()
{
return(valueofshare);
}
}
}
I would appreciate any help very much.
You have a field named Package, who's type is ArrayList. ArrayList doesn't have a method called CompanyName. What you're probably trying to do is something like:
Package.add(new SharePackage.Share(companyname, numberofshares, valueofshares));
You have two such fields named 'Package', so not sure which one you're trying to add to. Maybe you're under the impression the fields are somehow the same one. They are not.
BTW: Definitely learn Java coding style before submitting this to anyone. You are naming fields with UpperCamelCase which makes it very difficult for a java programmer to read your code.
user1207705 that was the answer. I modified it to:
String name;
double number;
double value;
name = CompanyNameField.getText();
number = Double.parseDouble(NumberOfSharesField.getText());
value = Double.parseDouble(ValueOfShareField.getText());
Package.add(new SharePackage.Share(name, number, value));
Thank you for your help and I will work on Java coding style.
In this project the user must enter 1 or 2 hospitals but not 3 or more. So the program starts and I display a menu. If the user presses 1 he must enter a hospital(name and department). After this the program displays the menu again and the user can choose to insert another hospital.
But after that, if I choose to insert another one (which is not permitted) the program accepts it. It seems that every time InsertHospitals() is called from the main class, the value of numberofhospitals (which is a counter counting how many hospitals I entered) equals 0.
public class Hospital {
private String Name, Departments;
private char flag;
private int numberofhospitals;
private Hospital[] ListOfHospitals;
//private Patient[] ListOfPatiens;
//private Doctor[] ListOfDoctors;
//private Examination[] ListOfExaminations;
//private Folder[] ListOfFolders;
public Hospital(String Name, String Departments)
{
this.Name=Name;
this.Departments=Departments;
}
public Hospital()
{
ListOfHospitals = new Hospital[2];
//ListOfPatiens = new Patient[100];
//ListOfDoctors = new Doctor[100];
//ListOfExaminations = new Examination[100];
//ListOfFolders = new Folder[100];
}
public String getName()
{
return Name;
}
public void setname(String Name)
{
this.Name=Name;
}
public String getDepartments()
{
return Departments;
}
public void setdepartments(String Departments)
{
this.Departments=Departments;
}
public void InsertHospitals()
{
if(numberofhospitals==2)
{
System.out.println("You can give only two hospitals!");
}
else
{
String temp = sir.readString("Hospital's Name:");
Name=temp;
String temp1 = sir.readString("Hospital's departments:");
Departments=temp1;
Hospital hospital = new Hospital(Name, Departments);
ListOfHospitals[numberofhospitals]=hospital;
numberofhospitals=numberofhospitals+1;
}
}
}
Your misunderstanding something, the list of hospitals (as mentioned) should not be inside your hospital class. You have to consider your hospital class as a blueprint you are using in your application.
Which means that you need to have a list of hospitals, as a list inside your other application class (which runs the application) and the InsertHospitals method should not be in your hospital class either obviously.
As you add a new hospital in your program, you create a new hospital object and add it to the list of hospitals (fx an arraylist) your keeping as a field value.
Also posssibly make a new constructor with parameters in the hospital class so you can insert the values outside of the class.
Something like this fx.
public class MainApp {
private ArrayList<Hospital> hospitalList;
public static void main(String[] args) {
// Initialize or load it from a file or whatever here.
hospitalList = new ArrayList<Hospital>();
// your code here...
}
public void insertHospital(<insert parameters here to create a hospital>) {
Hospital newHospital = new Hospital(<insert params with new constructor>);
hospitalList.add(newHospital);
}
}
Whatever your problem, your program completely wrong. In insertHospital() your changing Name and Departments fields, and creating new Hospital with those values. When you print Hospital information all hospitals will have the same value.
I have this class which will be used from my main class and will be creating objects of class Sale:
import acm.program.*;
public class Sale {
int size = 0;
public int scode = 0 ; // sell code
public String cname ; // client name
public int cphone ; // client phone
public String sdate ; // selling date
public int cost ; // final cost
public int aItems[] // sold product
public Sale (Item aItems[],String cname, String cphone, String sdate) {
this.aItems = aItems ;
this.cname= cname;
this.sphone= sphone;
this.sdate= sdate;
}
public void setsItems(Items aItems) {
this.aItems= aItems;
}
public void setCname (String name) {
this.name= name;
}
public void setCphone(String cphone) {
this.cphone= cphone;
}
public void setSDate(String sdate) {
this.sdate= sdate;
}
The project says that in my main class I must have a way to review any sale made by entering the unique id number of the sale which will be created in this class. My problem is that I don't know how to set my class so that every time it's called from my main it will generate a new id number, starting from 1 and increasing by 1 every time.
Any ideas?
Use a static member variable to store the ID number of the last sale. A static member is a variable that is part of the class, not the object. This value can get incremented like you want.
Make a new method to access that variable that also increments the variable by one each time you generate a new ID.
In your main, call the Sale.generateNewID() method, then pass that new ID into the constructor of your Sale class.
public class Sale {
public static int idCount = 0;
public static int generateNewID() {
return ++idCount;
}
// this is a new member variable to store the id of the sale
private int id;
// note: added id parameter to constructor
public Sale (int theId, Item aItems[],String cname, String cphone, String sdate) {
this.id = theId;
// other constructor assignments that you had go here.
}
... // rest of your code
}
I have asked this question before and followed the feedback as best as I could but I am still having one problem with storing the info that the user enters into the array.
Here is the first attempt:
OOP Java: Creating a stock inventory program
So I need to have in total three classes(That's required). The Stock, stock inventory and then the user interface. The purpose of this program is to ask the user to input the company's name, stock rating, price and the number of shares. Of course, I then have to do other things. I think I am okay with the rest, the problem is the stockInterface, the last bit of code that I post below.
public class Stock {
private String companyName;
private String stockRating;
private int price;
private int numberOfShares;
public String getCompanyName() {
return companyName;
}
public int getStockRating() {
return stockRating;
}
public String getPrice() {
return price;
}
public int getNumberOfShares() {
return numberOfShares;
}
public Stock(String companyName, String stockRating, int price, int numberOfShares) {
super();
this.companyName = companyName;
this.stockRating = stockRating;
this.price = price;
this.numberOfShares = numberOfShares;
}
import java.util.*;
public class StockInvetory {
private static final int INVENTORY_SIZE = 12;
private Stock [] stocks;
public StockInvetory() {
stocks = new Stock [INVENTORY_SIZE];
}
public class StockInterface() {
private static StockInventory stockPortfolio;
public static void main (String [] args){
System.out.println ("Stock's name:");
String stockName = console.next();
System.out.println ("Stock's rating");
String stockRating= console.next();
System.out.println ("Stock's price:");
int stockPrice = console.nextInt();
System.out.println ("Numbers of shares: ");
int numberShares= console.nextInt();
stockPortfolio [0]= new Stock(stockName, stockRatings, stockPrice, numberShares);
}
This piece of code doesn't work.
stockPortfolio [0]= new Stock(stockName, stockRatings, stockPrice, numberShares)
Can somebody please show me the proper way to store the info into the array? Thank you very much.
Lots of compile errors...
You have defined stockRating as a String but yet return it as an int:
public int getStockRating() {
return stockRating;
}
The same is true for price.
You have extra parenthesis here:
public class StockInterface() {
^
Also in StockInventory, there are multiple statements in the class block They belong in a method.
console is not instantiated.
stockPortfolio is assigned as an array entry, yet it is a single object, and assigned to the Stock which is not a matching type.
So you've declared the stockPortfolio as an instance of StockInventory. StockInventory is a class not an array, so you can't use stockPortfolio [0] = ... because stockPortfolio is an instance of the class. You have a private member in StockInventory that is an array of Stock class instances. What you need is an accessor method to be able to manipulate it. So change StockInventory as follows:
public class StockInvetory {
/*
All the code you have now ...
*/
public Stock [] getStocks(){
return stocks;
}
public setStocks(Stock [] value){
//maybe some checking here ...
stocks = value;
}
}
Now just a slight change in using the class. You need to use the accessor methods as follows:
public class StockInterface {
/*
What you have just the following line changes ...
*/
stockPortfolio.getStocks()[0] = new Stock(stockName, stockRatings, stockPrice, numberShares);
}
I am assuming you are happy with the way you are initializing the array and that you have decided arrays are better than more dynamic data structures in collections for your specific project. If this is not true have a look at Java Collections they may bring you more joy.