So this is my main class
public class Main_Page {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader (isr);
String type;
String ticnum;
int number;
int i=1;
int choice =0;
double distance;
double total=0;
double payment=0;
public static void main(String[] args) throws Exception{
Main_Page m=new Main_Page();
m.doMenu();
m.doBus();
m.doTrain();
}
public void doMenu() throws Exception {
do
{
System.out.println("***WELCOME***");
System.out.println("What ticket do you want to purchase?");
System.out.println("(1) Bus " + "\n(2) Train" + "\n(3) Exit");
System.out.println("Please enter your choice:");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1: doBus();
break;
case 2: doTrain();
break;
case 3:
Receipt re=new Receipt();
ReceiptMain rm=new ReceiptMain();
rm.setReceipt(re);
rm.getReceipt();
rm.getReceipt().print();
default: System.out.println("Invalid choice. Please try again");
}
}while(choice<=2);
}
public void doBus()throws Exception {
type="Bus";
System.out.println("Please enter distance(km):");
distance=Integer.parseInt(br.readLine());
total=distance*0.5;
payment+=total;
number+=i;
System.out.println("Total payment is:RM" +payment);
ticnum="B";
}
public void doTrain() throws Exception{
type="Train";
System.out.println("Please enter distance(km):");
distance=Integer.parseInt(br.readLine());
total=distance*0.8;
payment+=total;
number+=i;
ticnum="T";
}
}
And this is my other class
public class Receipt extends Main_Page {
public void print(){
Main_Page mp=new Main_Page();
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
System.out.println("******RECEIPT******");
System.out.println(d);
System.out.println("[Type] [Number] [Price]");
System.out.println(mp.type + " " + mp.number + " "
+ " " + mp.payment);
}
}
Right now, when i run the code, when it displays my receipt, i have null values for “mp.type, mp.number and mp.payment”. How do i use the values from my Main class to the Receipt class?