java - Issue with if statment -
im trying make if statement when r1 , num equal prints r1 wont print. asks product number , when enter nothing prints. main way @ end. please show me im doing wrong?
import java.io.*; import java.util.*; import java.lang.*; import java.text.*; public class producttype implements comparable<producttype> { private string partnum; private double price; private int stock; public producttype (string partnum, double price , int stock ){ this.partnum = partnum; this.price = price ; this.stock = stock ; } public producttype(){ partnum = "" ; price = 0; stock = 0; } public void setnum(string partnum) {this.partnum = partnum;} public void setprice(double price) {this.price = price ;} public void setstock(int stock) {this.stock = stock ;} public string getnum() {return partnum;} public double getprice() {return price;} public int getstock() {return stock;} public int compareto(producttype othertype) { int temp = (this.getnum().compareto(othertype.getnum())); return temp; } //object1.compareto(object2); //(function).compareto.function2; public string tostring() { string result = "" + partnum + " price" + price + "stock" + stock; return result; } public static void main(string[] args) { producttype r1 = new producttype("1422", 1.00, 2); producttype r2 = new producttype("8535", 2.00, 3); scanner in = new scanner(system.in); system.out.print("enter product number: "); string num = in.nextline(); if ( r1.equals(num)) system.out.print(r1); } }
r1 instance of producttype , num string. never equal.
you check r1 instance's num value num string.
if ( r1.getnum().equals(num)) system.out.print(r1);