CyberCafe.java
Printing.javaCode:public abstract class CyberCafe{ String cust_name; int pc_no; public CyberCafe(String cn, int pn){ cust_name = cn; pc_no = pn; } public String getCustName(){ return cust_name; } public int getPC(){ return pc_no; } public abstract double calcTotCharge(); }
Internet.javaCode:public class Printing extends CyberCafe{ int no_of_print; boolean colour; public Printing(String cn, int pn, int p, boolean c){ super(cn, pn); no_of_print = p; colour = c; } public int getNoOfPrint(){ return no_of_print; } public boolean getColour(){ return colour; } public double calcTotCharge(){ double charge; if(colour){ charge = 0.7 * no_of_print; } else{ charge = 0.3 * no_of_print; } return charge; } }
myCyberCafeAPP.javaCode:public class Internet extends CyberCafe{ String start_time; String end_time; int no_of_files_downloaded; public Internet(String cn, int pn, String s, String e, int d){ super(cn, pn); start_time = s; end_time = e; no_of_files_downloaded = d; } public String getStartTime(){ return start_time; } public String getendTime(){ return end_time; } public int getDownloadedFiles(){ return no_of_files_downloaded; } public double calcTotCharge(){ double charge = 0.0; int min = 0; int startH = Integer.parseInt(start_time.substring(0, 2)); int startM = Integer.parseInt(start_time.substring(2)); int endH = Integer.parseInt(end_time.substring(0, 2)); int endM = Integer.parseInt(end_time.substring(2)); //calculate minutes if(startH == endH){ min = endM - startM; } else if(startH < endH){ min = ((endH - startH -1) * 60) + endM + (60 - startM); } else{ min = (((24 - startH) + endH - 1) * 60) + endM + (60 - startM); } //calculate charge if(min < 120){ charge = 2.5; } else{ charge = 2.5 + ((((min - 120) / 30) + 1) * 0.5); } //calculate total charges charge = charge + (1.5 * no_of_files_downloaded); return charge; } }
i have subclass "Printing" and "Internet"..Code:import java.util.Scanner; public class myCyberCafeAPP{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter no of customer: "); int cust = input.nextInt(); CyberCafe[] myCafePrint= new CyberCafe[cust]; CyberCafe[] myCafeInternet= new CyberCafe[cust]; System.out.print("\n"); for(int i=0; i<cust; i++){ System.out.print("Customer Name: "); String name = input.next(); System.out.print("Phone No: "); int phone = input.nextInt(); System.out.print("No of Print: "); int print = input.nextInt(); System.out.print("Colour (Yes/No): "); String colour = input.next(); boolean c; if(colour.equalsIgnoreCase("Yes")){ c = true; } else{ c = false; } System.out.print("Start Time (eg: 1420): "); String start = input.next(); System.out.print("End Time (eg: 1430): "); String end = input.next(); System.out.print("No of Files Downloaded: "); int download = input.nextInt(); myCafePrint[i] = new Printing(name, phone, print, c); myCafeInternet[i] = new Internet(name, phone, start, end, download); } double total = 0.0; for(int j=0; j<cust; j++){ total = total + myCafePrint[j].calcTotCharge() + myCafeInternet[j].calcTotCharge(); } System.out.print("Total Charge: RM " + total); } }
how can i store both "Printing" and "Internet" data in the same array object...
for example:
array[1] = {customer name, phone no, no of print, color, start time, end time, no of download}
should i use interface..i read a lot about it but i don't understand...
can anyone teach me how to solve it with appropriate example...
thanks in advance...




Reply With Quote