By GokiSoft.com|
19:41 06/07/2022|
Java Basic
[Source Code] Quản lý phòng tập GYM - Java basic - C2108L
Quản lý phòng tập GYM - Java basic
#Ve.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Ve implements IInputOutput{
String customerName, buyDate, expiredDate;
public Ve() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten KH: ");
customerName = scan.nextLine();
System.out.println("Nhap ngay mua: ");
buyDate = scan.nextLine();
System.out.println("Nhap ngay het han: ");
expiredDate = scan.nextLine();
}
@Override
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "customerName=" + customerName + ", buyDate=" + buyDate + ", expiredDate=" + expiredDate;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getBuyDate() {
return buyDate;
}
public void setBuyDate(String buyDate) {
this.buyDate = buyDate;
}
public String getExpiredDate() {
return expiredDate;
}
public void setExpiredDate(String expiredDate) {
this.expiredDate = expiredDate;
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
DataMgr dataMgr = new DataMgr();
dataMgr.input();
dataMgr.display();
}
}
#IInputOutput.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
/**
*
* @author Diep.Tran
*/
public interface IInputOutput {
void input();
void display();
}
#DungCu.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class DungCu implements IInputOutput{
String name, status, type;
public DungCu() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap trang thai: ");
status = scan.nextLine();
System.out.println("Nhap loai: ");
type = scan.nextLine();
}
@Override
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "name=" + name + ", status=" + status + ", type=" + type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
#DoUong.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class DoUong implements IInputOutput{
String name;
float price;
public DoUong() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap gia: ");
price = Float.parseFloat(scan.nextLine());
}
@Override
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "name=" + name + ", price=" + price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
#DataMgr.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class DataMgr implements IInputOutput{
ArrayList<IInputOutput> dataList;
public DataMgr() {
dataList = new ArrayList<>();
}
public ArrayList<IInputOutput> getDataList() {
return dataList;
}
public void setDataList(ArrayList<IInputOutput> dataList) {
this.dataList = dataList;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so doi tuong can them: ");
int N = Integer.parseInt(scan.nextLine());
Random random = new Random();
for (int i = 0; i < N; i++) {
int rad = random.nextInt(2);
IInputOutput io;
switch(rad) {
case 0:
System.out.println("Nhap thong tin dung cu: ");
io = new DungCu();
break;
case 1:
System.out.println("Nhap thong tin do uong: ");
io = new DoUong();
break;
default:
System.out.println("Nhap thong tin ve: ");
io = new Ve();
break;
}
io.input();
dataList.add(io);
}
}
@Override
public void display() {
System.out.println("Thong tin dung cu: ");
for (IInputOutput io : dataList) {
io.display();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2022-07-06 12:56:03
#Ve.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Ve implements IInputOutput{
String customerName, buyDate, expiredDate;
public Ve() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten KH: ");
customerName = scan.nextLine();
System.out.println("Nhap ngay mua: ");
buyDate = scan.nextLine();
System.out.println("Nhap ngay het han: ");
expiredDate = scan.nextLine();
}
@Override
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "customerName=" + customerName + ", buyDate=" + buyDate + ", expiredDate=" + expiredDate;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getBuyDate() {
return buyDate;
}
public void setBuyDate(String buyDate) {
this.buyDate = buyDate;
}
public String getExpiredDate() {
return expiredDate;
}
public void setExpiredDate(String expiredDate) {
this.expiredDate = expiredDate;
}
}
#Test.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
// DataMgr d1 = new DataMgr();
DataMgr d1 = DataMgr.getInstance();
d1.getDataList().add(new DungCu());
DataMgr.getInstance().getDataList().add(new Ve());
// DataMgr d2 = new DataMgr();
DataMgr d2 = DataMgr.getInstance();
d2.getDataList().add(new Ve());
d2.getDataList().add(new DoUong());
System.out.println("size1 = " + d1.getDataList().size());
System.out.println("size2 = " + d2.getDataList().size());
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
// DataMgr dataMgr = new DataMgr();
// dataMgr.input();
DataMgr.getInstance().input();
// dataMgr.display();
DataMgr.getInstance().display();
}
}
#IInputOutput.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
/**
*
* @author Diep.Tran
*/
public interface IInputOutput {
void input();
void display();
}
#DungCu.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class DungCu implements IInputOutput{
String name, status, type;
public DungCu() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap trang thai: ");
status = scan.nextLine();
System.out.println("Nhap loai: ");
type = scan.nextLine();
}
@Override
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "name=" + name + ", status=" + status + ", type=" + type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
#DoUong.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class DoUong implements IInputOutput{
String name;
float price;
public DoUong() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap gia: ");
price = Float.parseFloat(scan.nextLine());
}
@Override
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "name=" + name + ", price=" + price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
#DataMgr.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt1167;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class DataMgr implements IInputOutput{
ArrayList<IInputOutput> dataList;
//singleton pattern
private static DataMgr instance = null;
private DataMgr() {
dataList = new ArrayList<>();
}
public static DataMgr getInstance() {
if(instance == null) {
instance = new DataMgr();
}
return instance;
}
public ArrayList<IInputOutput> getDataList() {
return dataList;
}
public void setDataList(ArrayList<IInputOutput> dataList) {
this.dataList = dataList;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so doi tuong can them: ");
int N = Integer.parseInt(scan.nextLine());
Random random = new Random();
for (int i = 0; i < N; i++) {
int rad = random.nextInt(2);
IInputOutput io;
switch(rad) {
case 0:
System.out.println("Nhap thong tin dung cu: ");
io = new DungCu();
break;
case 1:
System.out.println("Nhap thong tin do uong: ");
io = new DoUong();
break;
default:
System.out.println("Nhap thong tin ve: ");
io = new Ve();
break;
}
io.input();
dataList.add(io);
}
}
@Override
public void display() {
System.out.println("Thong tin dung cu: ");
for (IInputOutput io : dataList) {
io.display();
}
}
}