Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,866 members, 7,802,785 topics. Date: Friday, 19 April 2024 at 09:31 PM

Help A Java Newbie With This Simple Java Program - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help A Java Newbie With This Simple Java Program (1189 Views)

Simple Java Calculator Code. / Simple Java Challenge / Java Program To Solve Quadratic Equation-dealwap (2) (3) (4)

(1) (Reply) (Go Down)

Help A Java Newbie With This Simple Java Program by halmat(m): 12:00am On Sep 30, 2015
Hi everyone,

I'm a newbie to programming trying to build some skills around Java. I encountered an exercise in Dietel's Java how to program book. It's requesting to make an invoice program for a hardware store with constructors, get and set methods, and also calculate the total of the purchase on the invoice with quantity * price.

I tried my way around it and come up with these two classes. I'm now stock at an error saying partQuantity: an instance of type int cannot be returned by a method 'Java.lang.String'

Please help me out with this, I really don't know what next to do.

Here are the two classes for the program.


public class NewInvoicevoice
{
private String partName;
private String partDescription;
private int partQuantity;
private int partPrice;

public NewInvoice
(
String name,
String itemDescription,
int itemQuantity,
int itemPrice
) // ENDS CONSTRUCTOR ARGUMENTS
{
partName = name;
partDescription = itemDescription;
partQuantity = itemQuantity;
partPrice = itemPrice;
}

public void setName( String name )
{
partName = name;
}

public void setDescription( String itemDescription )
{
partDescription = itemDescription;
}

public void setQuantity(int itemQuantity)
{
partQuantity = itemQuantity;
}
public void setPrice( int itemPrice )
{
partPrice = itemPrice;
}

public String getName()
{
System.out.println ("ENTER THE NAME OF THE COMPONENT: " );
return partName;
}

public String getDescription()
{
System.out.println ("ENTER THE DESCRIPTION OF THE COMPONENT: " );
return partDescription;
}

public String getQuantity()
{
System.out.println ("ENTER THE QUANTITY ORDERED: " );
return partQuantity;
}
public int getPrice()
{
System.out.println ("ENTER THE PRICE OF THE COMPONENT: " );
return partPrice;
}

public void displayMessage( String nameOfStore)
{
System.out.printf ( "Welcome to %s HRDWARE STORE.", nameOfStore);
System.out.println ( "\n" ) ;
}

public void displayTotal( int itemTotal )
{
System.out.printf ( "THE TOTAL COST OF ALL YOUR PURCHASE(S) IS: ", + itemTotal + "\n" );
}

}



And here is its Test class



import java.util.Scanner;


public class NewInvoiceTest {


public static void main(String[] args) {



Scanner invoiceNm = new Scanner( System.in );
Scanner invoiceDsct = new Scanner( System.in );
Scanner invoiceQtty = new Scanner( System.in );
Scanner invoicePrc = new Scanner( System.in );
Scanner storeNm = new Scanner( System.in );


String invoiceName = invoiceNm.nextLine();
String invoiceDescription = invoiceDsct.nextLine();
int invoiceQuantity = invoiceQtty.nextInt();
int invoicePrice = invoicePrc.nextInt();
String storeNme = storeNm .nextLine();

String storeName = "DengrDEV";
int totalPurchase = invoicePrice * invoiceQuantity;

NewInvoice newInvoicea = new NewInvoice(invoiceName, invoiceDescription, invoiceQuantity, invoicePrice);

newInvoicea.displayMessage ( storeNme );
System.out.println ("" + newInvoicea.getName() + "\n" + newInvoicea.getDescription() + "\n" + newInvoicea.getQuantity() + "\n" + newInvoicea.getPrice() + "\n" + "" );
newInvoicea.displayTotal ( totalPurchase );

}

}

Thanks in anticipation.
Re: Help A Java Newbie With This Simple Java Program by FincoApps(m): 2:26am On Sep 30, 2015
halmat:
Hi everyone,
public class NewInvoicevoice
{
private String partName;
private String partDescription;
private int partQuantity;
private int partPrice;

public NewInvoice
(
String name,
String itemDescription,
int itemQuantity,
int itemPrice
) // ENDS CONSTRUCTOR ARGUMENTS

First of all, your class and constructor name must be the same. Your class name is NewInvoicevoice and the constructor is NewInvoice(). So rename the class to NewInvoice()



halmat:
Hi everyone,
public String getQuantity()
{
System.out.println ("ENTER THE QUANTITY ORDERED: " );
return partQuantity;
}

Secondly, the getQuantity() method has a return type of String, however you are returning partQuantity which has a datatype of int. So to fix this, replace "return partQuantity" with "return String.valueOf(partQuantity)"
Re: Help A Java Newbie With This Simple Java Program by yungcheda(m): 7:16am On Sep 30, 2015
1. Your constructor name and class names have to match

2. Why are you using constructors to set instance variables and still using set method again?? They playing the same roles in a way...

3. Your partQuantity is supposed to return an int type but you put the return type as String. Change the String to int. This exact one is what is causing the error

Goodluck
Re: Help A Java Newbie With This Simple Java Program by fattbabakay(m): 11:14am On Sep 30, 2015
yungcheda:
1. Your constructor name and class names have to match

2. Why are you using constructors to set instance variables and still using set method again?? They playing the same roles in a way...

3. Your partQuantity is supposed to return an int type but you put the return type as String. Change the String to int. This exact one is what is causing the error

Goodluck
Osheyyyy baddest...
Re: Help A Java Newbie With This Simple Java Program by halmat(m): 1:48pm On Sep 30, 2015
Thank you all, I really appreciate your efforts. I now got the compiler error out. Although the program is yet to function as I wish, I'm currently battling with it and will post the final code that receive input and print the entered value back to the user.
Re: Help A Java Newbie With This Simple Java Program by Nobody: 3:52pm On Sep 30, 2015
use quick fix
Re: Help A Java Newbie With This Simple Java Program by halmat(m): 7:40pm On Sep 30, 2015
Thanks everyone, and here is my final code:


public class NewInvoice
{
private String partName;
private String partDescription;
private double partQuantity;
private double partPrice;


public void setName( String name )
{
partName = name;
}

public void setDescription( String itemDescription )
{
partDescription = itemDescription;
}

public void setQuantity(double itemQuantity)
{
partQuantity = itemQuantity;
}
public void setPrice( double itemPrice )
{
partPrice = itemPrice;
}

public String getName()
{
return partName;
}

public String getDescription()
{
return partDescription;
}

public double getQuantity()
{
return (partQuantity);
}
public double getPrice()
{
return partPrice;
}

public void displayMessage( String nameOfStore)
{
System.out.print("\n\n*************************************************" );
System.out.printf ( "\nWELCOME TO %s HARDWARE STORE.", nameOfStore);
System.out.println ( "\n" );
}

public void displayTotal( double itemTotal)
{
System.out.println( "" );
System.out.printf ( "THE TOTAL COST OF ALL YOUR PURCHASE(S) IS: \n$%.2f", itemTotal );
}

}

And its Test class




import java.util.Scanner;


public class NewInvoiceTest {


public static void main(String[] args) {



Scanner invoiceNm = new Scanner( System.in );
Scanner invoiceDsct = new Scanner( System.in );
Scanner invoiceQtty = new Scanner( System.in );
Scanner invoicePrc = new Scanner( System.in );
Scanner storeNm = new Scanner( System.in );

System.out.print( "Enter the store name: \n" );
String storeNme = storeNm .nextLine();

System.out.print( "Enter the item Name: \n" );
String invoiceName = invoiceNm.nextLine();

System.out.print( "Enter the item Description: \n" );
String invoiceDescription = invoiceDsct.nextLine();

System.out.print( "Enter the item Quantity: \n" );
double invoiceQuantity = invoiceQtty.nextDouble();

System.out.print( "Enter the item price ($): \n" );
double invoicePrice = invoicePrc.nextDouble();







NewInvoice newInvoicea = new NewInvoice();


newInvoicea.setName( invoiceName );



newInvoicea.setDescription( invoiceDescription );


newInvoicea.setQuantity( invoiceQuantity );


newInvoicea.setPrice( invoicePrice );

newInvoicea.displayMessage ( storeNme );

System.out.println ("NAME OF THE COMPONENT: \n" + newInvoicea.getName() +
"\n\nTHE DESCRIPTION OF THE COMPONENT: \n" + newInvoicea.getDescription() +
"\n\nQUANTITY ORDERED: \n" + newInvoicea.getQuantity() +
"\n\nPRICE OF THE COMPONENT: \n$" + newInvoicea.getPrice() + "" );
double invoiceTotal = newInvoicea.getQuantity() * newInvoicea.getPrice( );
newInvoicea.displayTotal( invoiceTotal );



}


}


1 Like

Re: Help A Java Newbie With This Simple Java Program by FincoApps(m): 7:42pm On Sep 30, 2015
Congratulations smiley
halmat:
Thanks everyone, and here is my final code:

And its Test class

Re: Help A Java Newbie With This Simple Java Program by halmat(m): 7:55pm On Sep 30, 2015
FincoApps:
Congratulations smiley

Please elaborate more on quick fix
Re: Help A Java Newbie With This Simple Java Program by halmat(m): 7:56pm On Sep 30, 2015
FincoApps:
Congratulations smiley

Thanks sir.

1 Like

Re: Help A Java Newbie With This Simple Java Program by halmat(m): 8:56pm On Sep 30, 2015
FincoApps:
Congratulations smiley

Thanks sir

1 Like

(1) (Reply)

Urgent: I Need A Software To Build A Network Marketing Platform / Is Java Still Worth Learning With Php / Job Opportunity - Competent Programmers Only

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 29
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.