₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,324,995 members, 8,419,850 topics. Date: Thursday, 04 June 2026 at 02:49 AM

Toggle theme

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

Nairaland ForumScience/TechnologyProgrammingHelp A Java Newbie With This Simple Java Program (1564 Views)

1 Reply (Go Down)

Help A Java Newbie With This Simple Java Program by halmat(op):
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(op): 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(op): 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 );



}


}
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(op):
FincoApps:
Congratulations smiley
Please elaborate more on quick fix
Re: Help A Java Newbie With This Simple Java Program by halmat(op): 7:56pm On Sep 30, 2015
FincoApps:
Congratulations smiley
Thanks sir.
Re: Help A Java Newbie With This Simple Java Program by halmat(op): 8:56pm On Sep 30, 2015
FincoApps:
Congratulations smiley
Thanks sir
Re: Help A Java Newbie With This Simple Java Program by FredSteel: 1:00pm On Nov 07, 2024
When deciding between .net vs java, .NET offers excellent performance and security, especially for enterprise applications. Java, however, provides flexibility with its cross-platform capabilities, making it ideal for global applications. Both technologies allow companies to build scalable, secure, and efficient software solutions tailored to their needs.
1 Reply

Simple Java Calculator Code.Simple Java ChallengeJava Program To Solve Quadratic Equation-dealwap234

Help!!! I Formatted My HDD Without Backup, How May I Recover Formatted Files?19 Tips For Programmers/software Engineers In 2019Comprehensive Study Guide For Data Science : Learn In 9 Months For Free