I'm very new to java and I know I'm learning this in a very bad way. So, I've understood a bit like how to print and stuff (I'm so sorry I'm totally new to java). Anyways, I'm creating a POS (Point of Sale) system.
So in my current code when you run this in cmd/console
You will see 2 choices (1 & 0) but it has actually 3 choices,(1,2 & 0).
1- Add Item to List
2- Total of everything (need to add items first [1])
0- Exit
After adding a product/item to the list, it would loop back to the transaction part (1,2 & 0) part. Everything was ok until I inputted "2" which then placed me with an error of:
'Exception in thread "main" java.lang.NullPointException at Pos.main(Pos.java:69)'
while my friend who I was chatting with at Facebook used this online compiler and it says this:
'java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Pos.main(Pos.java:47)'
(the main reason why I placed so many imports)
please help a rookie out :--(
I've been searching around stackoverflow and google but nothing seems to work or maybe my knowledge level is just too low to understand those.
anyways, my current goal is for when i press '2' it will get the total of all totals from the items at the transaction area that i've added. So basically pressing '1' at the transaction area means you're ordering something. And when I press '2' then there goes your bill (total) or something like that.
import java.*;
import java.util.*;
import java.util.Scanner;
import java.lang.*;
import java.io.*;
class Product {
private String prodName;
private double prodPrice;
private int quantity;
public double prodTotal;
// constructor
public Product(String prodName, double prodPrice,int quantity,double prodTotal) {
this.prodName = prodName;
this.prodPrice = prodPrice;
this.quantity = quantity;
this.prodTotal = prodTotal;
}
// getter
public String getprodName() { return prodName; }
public double getprodPrice() { return prodPrice; }
public int getquantity() { return quantity; }
public double getprodTotal() { return prodTotal; }
// setter
public void setprodName(String prodName) { this.prodName = prodName; }
public void setprodPrice(double prodPrice) { this.prodPrice = prodPrice; }
public void setquantity(int quantity) {this.quantity = quantity; }
public void setprodTotal(double prodTotal) {this.prodTotal = prodTotal; }
}
public class Pos{
public static void main (String args[])
{
String pname;
double price;
int quant,choice,j,i=0;
double ptotal, total=0;
Product[] prod = new Product[100];
do{
System.out.println("\n1 - Add Item to List\n0 - Exit\n");
Scanner inputReader= new Scanner( System.in );
choice=inputReader.nextInt();
switch(choice){
case 1:
System.out.println("Enter product name: ");
pname = inputReader.next();
System.out.println("Enter price: ");
price = inputReader.nextDouble();
System.out.println("Enter quantity: ");
quant = inputReader.nextInt();
ptotal = price*quant;
ptotal = Math.round(ptotal * 100.0) / 100.0;
System.out.println("\nProduct: "+pname+"\tPrice: P"+price+"\tQuantity: "+quant+"\tTotal: P"+ptotal);
prod[i] = new Product (pname,price,quant,ptotal);
i++;
break;
case 2:
j=i;
while(j>0){
total=total+prod[j].prodTotal;
j--;
}
System.out.println("Overall Total: "+total);
break;
case 0:
break;
}
}while(choice!=0);
//System.out.println("\nProduct: "+prodName+"\tPrice: "+prodPrice+"\tQuantity: "+quantity);
}
}