A small program use java object oriented program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
interface Medicin{ | |
public void addingMedicine(int addM); | |
public void sellingMedicine(int buy); | |
} | |
class Medicine implements Medicin { | |
public String m_name; | |
public int m_num; | |
public Medicine(){ | |
m_name = new String(); | |
m_num = 0; | |
} | |
public Medicine(String name, int num){ | |
m_name = new String(name); | |
m_num = num; | |
} | |
public void addingMedicine(int addM){ | |
m_num = m_num+addM; | |
} | |
public void sellingMedicine(int buy){ | |
m_num = m_num - buy; | |
} | |
public void disp(){ | |
System.out.println(" Name of Medicine: "+m_name+" \n At Present Number of Medicine "+m_num); | |
} | |
} | |
class MedicineShop { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Scanner sc = new Scanner(System.in); | |
Scanner s = new Scanner(System.in); | |
String mName[] = new String[50]; | |
int mNum[] = new int[50]; | |
Medicine[] n = new Medicine[4]; | |
System.out.println("Enter total num of medicine . you will add: "); | |
int num = sc.nextInt(); | |
for(int i = 0; i<num; i++){ | |
System.out.println("Enter the name of medicine"+(i+1)+":"); | |
mName[i] = s.nextLine(); | |
System.out.println("Enter the amount of medicine"+(i+1)+":"); | |
mNum[i] = sc.nextInt(); | |
} | |
for(int j = 0; j< num; j++){ | |
n[j] = new Medicine(mName[j], mNum[j]); | |
n[j].disp(); | |
} | |
//adding new medicine | |
System.out.println("Adding Medicine: select name of medicine: "); | |
for(int j = 0; j< num; j++){ | |
System.out.print((j+1)+": "+n[j].m_name+"\n"); | |
} | |
int choice = sc.nextInt(); | |
System.out.println("adding onumber of medicine: "); | |
int nu = sc.nextInt(); | |
n[choice-1].addingMedicine(nu); | |
n[choice-1].disp(); | |
//selling sum tablet | |
System.out.println("Selling : select name of medicine:"); | |
for( int k = 0; k< num; k++){ | |
System.out.print((k+1)+": "+n[k].m_name+"\n"); | |
} | |
int choiceSell = sc.nextInt(); | |
System.out.println("Enter onumber of medicine selling: "); | |
int sell = sc.nextInt(); | |
n[choiceSell-1].sellingMedicine(sell); | |
n[choiceSell-1].disp(); | |
} | |
} |