By GokiSoft.com| 13:42 15/04/2020|
Java Basic

Share Code - Hướng dẫn chữa bài tập quản lý phòng tập GYM BT1178


Bài Tập

Quản lý phòng tập GYM - Java basic


/*
 * 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 lession12.gym;

/**
 *
 * @author Diep.Tran
 */
public interface IInputOutput {
    void input();
    
    void display();
}



/*
 * 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 lession12.gym;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Instruction implements IInputOutput{
    enum STATUS {
        NEW, OLD
    };
    
    enum TYPE {
        BUY, BORROW
    }
    
    String name;
    float weight, width, height, length;
    STATUS status;
    TYPE type;

    public Instruction() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public float getLength() {
        return length;
    }

    public void setLength(float length) {
        this.length = length;
    }

    public STATUS getStatus() {
        return status;
    }

    public void setStatus(STATUS status) {
        this.status = status;
    }

    public TYPE getType() {
        return type;
    }

    public void setType(TYPE type) {
        this.type = type;
    }
    
    @Override
    public void input() {
        Scanner scan =  new Scanner(System.in);
        System.out.println("Enter name: ");
        name = scan.nextLine();
        
        System.out.println("Enter weight: ");
        weight = Float.parseFloat(scan.nextLine());
        System.out.println("Enter width: ");
        width = Float.parseFloat(scan.nextLine());
        System.out.println("Enter height: ");
        height = Float.parseFloat(scan.nextLine());
        System.out.println("Enter length: ");
        length = Float.parseFloat(scan.nextLine());
        
        System.out.println("1. Status >> NEW");
        System.out.println("2. Status >> OLD");
        System.out.println("Choose: ");
        int choose = Integer.parseInt(scan.nextLine());
        if(choose == 1) {
            status = STATUS.NEW;
        } else {
            status = STATUS.OLD;
        }
        
        System.out.println("1. Type >> BUY");
        System.out.println("2. Type >> BORROW");
        System.out.println("Choose: ");
        choose = Integer.parseInt(scan.nextLine());
        if(choose == 1) {
            type = TYPE.BUY;
        } else {
            type = TYPE.BORROW;
        }
    }

    @Override
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Instruction{" + "name=" + name + ", weight=" + weight + ", width=" + width + ", height=" + height + ", length=" + length + ", status=" + getStatusString() + ", type=" + getTypeString() + '}';
    }
    
    public String getStatusString() {
        if(status == STATUS.NEW) {
            return "new";
        }
        return "old";
    }
    
    public String getTypeString() {
        if(type == TYPE.BUY) {
            return "buy";
        }
        return "borrow";
    }
}



/*
 * 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 lession12.gym;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Drings implements IInputOutput{
    String name;
    int price;

    public Drings() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter name: ");
        name = scan.nextLine();
        
        System.out.println("Enter price: ");
        price = Integer.parseInt(scan.nextLine());
    }

    @Override
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Drings{" + "name=" + name + ", price=" + price + '}';
    }
}



/*
 * 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 lession12.gym;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Ticket implements IInputOutput{
    String customerName, buyDate, expireDate;

    public Ticket() {
    }

    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 getExpireDate() {
        return expireDate;
    }

    public void setExpireDate(String expireDate) {
        this.expireDate = expireDate;
    }
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter customer name: ");
        customerName = scan.nextLine();
        
        System.out.println("Enter buy date (dd/mm/yyyy): ");
        buyDate = scan.nextLine();
        
        System.out.println("Enter expire date (dd/mm/yyyy): ");
        expireDate = scan.nextLine();
    }

    @Override
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Ticket{" + "customerName=" + customerName + ", buyDate=" + buyDate + ", expireDate=" + expireDate + '}';
    }
    
    
}



/*
 * 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 lession12.gym;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class DataMgr {
    List<IInputOutput> ioList;
    
    private static DataMgr instance = null;
    
    private DataMgr(){
        ioList = new ArrayList<>();
    }
    
    public synchronized static DataMgr getInstance() {
        if(instance == null) {
            instance = new DataMgr();
        }
        return instance;
    }

    public List<IInputOutput> getIoList() {
        return ioList;
    }

    public void setIoList(List<IInputOutput> ioList) {
        this.ioList = ioList;
    }
}



/*
 * 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 lession12.gym;

import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter N: ");
        int N = scan.nextInt();
        Random random = new Random();
        
        for (int i = 0; i < N; i++) {
            int rad = random.nextInt(3);
            
            IInputOutput io;
            
            switch(rad) {
                case 0:
                    io = new Instruction();
                    break;
                case 1:
                    io = new Drings();
                    break;
                default:
                    io = new Ticket();
                    break;
            }
            
            io.input();
            
            DataMgr.getInstance().getIoList().add(io);
        }
        
        DataMgr.getInstance().getIoList().forEach((io) -> {
            io.display();
        });
    }
}


Liên kết rút gọn:

https://gokisoft.com/1178

Bình luận