0% found this document useful (0 votes)
4 views8 pages

Lập trình HDT

The document contains Java code for three classes: ArrayList for managing integer arrays, SinhVien for managing student records, and PhieuXuat for managing product shipment records. Each class includes methods for adding, retrieving, searching, and deleting items, as well as displaying and processing data. The main methods in each class provide a menu-driven interface for user interaction to perform various operations on the data.

Uploaded by

Huỳnh Trâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Lập trình HDT

The document contains Java code for three classes: ArrayList for managing integer arrays, SinhVien for managing student records, and PhieuXuat for managing product shipment records. Each class includes methods for adding, retrieving, searching, and deleting items, as well as displaying and processing data. The main methods in each class provide a menu-driven interface for user interaction to perform various operations on the data.

Uploaded by

Huỳnh Trâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Array TMDT

package com.mycompany.arraytmdt;
import java.util.Scanner;
class ArrayList {
private int[] a;
private int n;
public ArrayList(int size) {
a = new int[size];
n = 0;
}
public void add(int x) {
a[n++] = x;
}
public int get(int i) {
return a[i];
}
public int nItem() {
return n;
}
public int search(int x) {
int i = 0;
while (i<n && x!=a[i])
i++;
return i;
}
public void delete(int i) {
n--;
for (int j=i; j<n; j++)
a[j] = a[j+1];
}
}
public class ArrayTMDT {
static Scanner sc = new Scanner(System.in);
static void nhap(ArrayList a) {
int x;
do {
x = sc.nextInt();
if (x>0)
a.add(x);
} while (x>0);
}
static void xuat(ArrayList a) {
for (int i=0; i<a.nItem(); i++)
System.out.print(" "+a.get(i));
}
static int tong(ArrayList a) {
int sum = 0;
for (int i=0; i<a.nItem(); i++)
sum = sum+a.get(i);
return sum;
}
public static void main(String[] args) {
ArrayList a = new ArrayList(100);
int chon;
do {
System.out.print("\n1. Nhap ");
System.out.print("\n2. Xuat");
System.out.print("\n3. Tong");
System.out.print("\n4. Tim");
System.out.print("\n5. Xoa vi tri");
System.out.print("\n6. Xoa gia tri");
System.out.print("\nChon:");
chon = sc.nextInt();
switch (chon) {
case 1:
System.out.print("Nhap day (0:Ket thuc):");
nhap(a);
break;
case 2:
System.out.print("Day : ");
xuat(a);
break;
case 3:
System.out.print("Tong : "+tong(a));
break;
case 4:
System.out.print("Tim :");
int x = sc.nextInt();
if (a.search(x)<a.nItem())
System.out.println("Tim thay");
else
System.out.println("Khong tim thay");
break;
case 5:
System.out.print("Xoa phan tu thu :");
int i = sc.nextInt();
a.delete(i);
break;
case 6:
System.out.print("Xoa gia tri: ");
x = sc.nextInt();
i = a.search(x);
while (i <a.nItem()) {
a.delete(i);
i = a.search(x);
}
break;
}
} while (chon>0);
}
}
Array Sinhvien
package com.mycompany.arraylisrtsvtmdt;

import java.util.Scanner;

class SinhVien {
private String MSSV, ho, ten;
private int diem;
public SinhVien(String m, String h, String t, int d) {
MSSV = m;
ho = h;
ten = t;
diem = d;
}
public String getMSSV() {
return MSSV;
}
public String getHo() {
return ho;
}
public String getTen() {
return ten;
}
public int getDiem() {
return diem;
}
}
class ArrayList {
private SinhVien[] a;
private int n;
public ArrayList(int size) {
a = new SinhVien[size];
n = 0;
}
public void add(SinhVien x) {
a[n++] = x;
}
public SinhVien get(int i) {
return a[i];
}
public int nItem() {
return n;
}
public int search(String m) {
int i = 0;
while (i<n && m.compareTo(a[i].getMSSV())!=0)
i++;
return i;
}
public void delete(int i) {
n--;
for (int j=i; j<n; j++)
a[j] = a[j+1];
}
}
public class ArrayLisrtSVTMDT {
static Scanner sc = new Scanner(System.in);
static void nhap(ArrayList a) {
String m, h, t;
int d;
do {
sc.nextLine();
System.out.print("Ma so:");
m = sc.nextLine();
if (m.length()>0) {
System.out.print("Ho :");
h = sc.nextLine();
System.out.print("Ten :");
t = sc.nextLine();
System.out.print("Diem :");
d = sc.nextInt();
a.add(new SinhVien(m, h, t, d));
}
} while (m.length()>0);
}
static void xuat(ArrayList a) {
for (int i=0; i<a.nItem(); i++) {
SinhVien x = a.get(i);
System.out.printf("\n%5s %-20s %-8s %4d", x.getMSSV(),
x.getHo(), x.getTen(), x.getDiem());
}
}
public static void main(String[] args) {
ArrayList a = new ArrayList(100);
String m;
int i, chon;
do {
System.out.print("\n1. Nhap ");
System.out.print("\n2. Xuat");
System.out.print("\n3. Tim");
System.out.print("\n4. Xoa MSSV");
System.out.print("\nChon:");
chon = sc.nextInt();
switch (chon) {
case 1:
nhap(a);
break;
case 2:
xuat(a);
break;
case 3:
sc.nextLine();
System.out.print("Tim :");
m = sc.nextLine();
i = a.search(m);
if (i<a.nItem()) {
SinhVien x = a.get(i);
System.out.printf("\n%5s %-20s %-8s %4d",
x.getMSSV(),
x.getHo(), x.getTen(),
x.getDiem());
}
else
System.out.println("Khong tim thay");
break;
case 4:
sc.nextLine();
System.out.print("Xoa theo ma so: ");
m = sc.nextLine();
i = a.search(m);
if (i <a.nItem())
a.delete(i);
break;
}
} while (chon>0);
}
}

Array Hanghoa
package com.mycompany.arraylisthhtmdt;
import java.util.Scanner;

class PhieuXuat {
private String soPX, MSHH;
private int SL;
public PhieuXuat(String spx, String mh, int SL) {
soPX = spx;
MSHH = mh;
this.SL = SL;
}
public String getSoPX() {
return soPX;
}
public String getMSHH() {
return MSHH;
}
public int getSL() {
return SL;
}
}
class ArrayList {
private PhieuXuat[] a;
private int n;
public ArrayList(int size) {
a = new PhieuXuat[size];
n = 0;
}
public void add(PhieuXuat x) {
a[n++] = x;
}
public PhieuXuat get(int i) {
return a[i];
}
public int nItem() {
return n;
}
public int search(String mh) {
int i = 0;
while (i<n && mh.compareTo(a[i].getMSHH())!=0)
i++;
return i;
}
public void delete(int i) {
n--;
for (int j=i; j<n; j++)
a[j] = a[j+1];
}
public void sapttMSHH() {
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
if (a[i].getMSHH().compareTo(a[j].getMSHH())>0) {
PhieuXuat tam = a[i];
a[i] = a[j];
a[j] = tam;
}
}
}
public class ArrayListHHTMDT {
static Scanner sc = new Scanner(System.in);
static void nhap(ArrayList a) {
String sopx, mh;
int sl;
do {
sc.nextLine();
System.out.print("So PX :");
sopx = sc.nextLine();
if (sopx.length()>0) {
System.out.print("Ma hang :");
mh = sc.nextLine();
System.out.print("So luong:");
sl = sc.nextInt();
a.add(new PhieuXuat(sopx, mh, sl));
}
} while (sopx.length()>0);
}
static void xuat(ArrayList a) {
for (int i=0; i<a.nItem(); i++) {
PhieuXuat x = a.get(i);
System.out.printf("\n%5s %-8s %4d", x.getSoPX(),
x.getMSHH(), x.getSL());
}
}
static void timMSHH(ArrayList a, String mshh) {
for (int i=0; i<a.nItem(); i++) {
PhieuXuat x = a.get(i);
if (x.getMSHH().compareTo(mshh)==0)
System.out.printf("\n%5s %-8s %4d", x.getSoPX(),
x.getMSHH(), x.getSL());
}
}
static void timPX(ArrayList a, String sopx) {
for (int i=0; i<a.nItem(); i++) {
PhieuXuat x = a.get(i);
if (x.getSoPX().compareTo(sopx)==0)
System.out.printf("\n%5s %-8s %4d", x.getSoPX(),
x.getMSHH(), x.getSL());
}
}
static void tongHopHH(ArrayList a) {
a.sapttMSHH();
int i=0;
while (i<a.nItem()) {
PhieuXuat x = a.get(i);
String mh = x.getMSHH();
int soLuong = 0;
while (i<a.nItem() && mh.compareTo(a.get(i).getMSHH())==0) {
soLuong = soLuong+a.get(i).getSL();
i++;
}
System.out.printf("\n%5s %d", mh, soLuong);
}
}
public static void main(String[] args) {
ArrayList a = new ArrayList(100);
String mshh;
int i, chon;
do {
System.out.print("\n1. Nhap ");
System.out.print("\n2. Xuat");
System.out.print("\n3. Tim MSHH");
System.out.print("\n4. Tim PX");
System.out.print("\n5. Tong hop theo MSHH");
System.out.print("\nChon:");
chon = sc.nextInt();
switch (chon) {
case 1:
nhap(a);
break;
case 2:
xuat(a);
break;
case 3:
sc.nextLine();
System.out.print("Tim HH:");
mshh = sc.nextLine();
timMSHH(a, mshh);
break;
case 4:
sc.nextLine();
System.out.print("Tim PX:");
String sopx = sc.nextLine();
timPX(a, sopx);
break;
case 5:
tongHopHH(a);
break;
}
} while (chon>0);
}
}

You might also like