본문 바로가기
java

HashMap/Calender

by 헤르쯔44 2021. 10. 20.
728x90
반응형

1.도서정보를 갖는 클래스를 만들고 추가,검색,수정,삭제,전체보기 기능을
만들어 보세요.HashMap에 저장(도서번호가 key로 저장)
도서정보:도서번호,도서명,가격

 

 

package hw;


import java.util.Scanner;
import java.util.HashMap;
import java.util.Collection;
import java.util.ArrayList;
class BookInfo {
	private String bNum;
	private String bName;
	private int price;
	public BookInfo(String bNum, String bName, int price){
		this.bNum = bNum;
		this.bName = bName;
		this.price = price;
	}

	public String getbNum() {
		return bNum;
	}
	public String getbName() {
		return bName;
	}
	public int getPrice() {
		return price;
	}
	public String toString(){
		return "도서번호:"+bNum+"\n"+"도서명:"+bName+"\n"+"가격:"+price+"\n"+"-----------------------------------------------";
	}
}
class BookManager{
	private HashMap<String,BookInfo> map;
	public BookManager(){
		map = new HashMap<String,BookInfo>();
	}
	public void run(){
		Scanner sc = new Scanner(System.in);
		boolean run = true;
		while(run){
			System.out.println("1.추가 2.검색 3.수정 4.삭제 5.전체보기 6.종료");
			System.out.println("-----------------------------------------------");
			int function = sc.nextInt();
			switch (function){
			case 1:
				addBook();
				break;
			case 2:
				seachBook();
				break;
			case 3:
				modifyBook();
				break;
			case 4:
				removeBook();
				break;
			case 5:				
				printAllBook();
				break;
			case 6:
				run = false;
				break;
			}
		}
		
	}
	public void addBook(){
		Scanner sc = new Scanner(System.in);
		System.out.println("도서번호를 입력하세요");
		String bNum=sc.next();
		System.out.println("도서명을 입력하세요");
		String bName=sc.next();
		System.out.println("가격을 입력하세요");
		int price =sc.nextInt();
		BookInfo bi = new BookInfo(bNum,bName,price);
		map.put(bNum,bi);
	}
	public boolean seachBook(){
		Scanner sc = new Scanner(System.in);
		System.out.println("도서번호를 입력하세요");
		String bNum=sc.next();		
		BookInfo bi = map.get(bNum);
		if(bi==null){
			System.out.println("존재하지 않는 도서번호입니다.");
			return false;
		}
		else{
			System.out.println("[찾은 도서 정보]");
			System.out.println(bi);
			return true;
		}
	}
	public boolean removeBook(){
		Scanner sc = new Scanner(System.in);
		System.out.println("도서번호를 입력하세요");
		String bNum=sc.next();		
		BookInfo bi = map.remove(bNum);
		if(bi==null){
			System.out.println("존재하지 않는 도서번호입니다.");
			return false;
		}
		else{
			System.out.println("[삭제한 도서 정보]");
			System.out.println(bi);
			return true;
		}
	}
	public boolean modifyBook(){
		Scanner sc = new Scanner(System.in);
		System.out.println("도서번호를 입력하세요");
		String bNum=sc.next();		
		BookInfo bi = map.remove(bNum);
		if(bi==null){
			System.out.println("존재하지 않는 도서번호입니다.");
			return false;
		}
		else{
			System.out.println("[찾은 도서 정보]");
			System.out.println(bi);
			System.out.println("도서명을 입력하세요");
			String name = sc.next();
			System.out.println("가격을 입력하세요");
			int price = sc.nextInt();
			BookInfo temp = new BookInfo(bNum,name,price);
			map.put(bNum,temp);
			return true;
		}
	}
	public void printAllBook(){
		Collection<BookInfo> col = map.values();
		ArrayList<BookInfo> list = new ArrayList<BookInfo>(col);
		for(BookInfo n : list){
			System.out.println(n);
		}
	}

}
class HW1 {
	public static void main(String[] args) {
		BookManager bm = new BookManager();
		bm.run();

	}
}

2. 년도와 월을 입력받아 달력출력하기
예) 
년도 : 2010
월 : 11

---------------------------
   2010년 11월
---------------------------
  일 월 화 수 목 금 토
      1  2 3  4  5  6
 ..

 

package hw;


import java.util.Calendar;
import java.util.Scanner;

public class HW2{
	public static void main(String[] args) {
		final String[] weekStr = {"일", "월", "화", "수", "목", "금", "토"};

		Scanner sc = new Scanner(System.in);
		Calendar cd = Calendar.getInstance();

		while(true) {
			System.out.println("[달력 보기]");
			System.out.print("년도 : ");
			int year = sc.nextInt(); 
			System.out.print("월 : ");
			int mon = sc.nextInt();


			cd.set(Calendar.YEAR, year);
			cd.set(Calendar.MONTH, mon - 1);
			
			int startWeek = cd.get(Calendar.DAY_OF_WEEK);

			int maxDay = cd.getActualMaximum(Calendar.DAY_OF_MONTH);

			System.out.println("\t\t" + year + "년 " + mon + "월");
			System.out.println("==================================================");

			//요일 출력
			for(int i = 0; i < weekStr.length; i++){ 
				System.out.print(weekStr[i] + "\t");
			}

			System.out.println();

			int nLine = 1;
			int nDay = 1;
			for(int i = 1; i < startWeek; i++ ) {
				System.out.print("\t");
				nLine++;
			}

			for(int i = 0; i < maxDay; i++) {
				System.out.print(nDay + "\t");

				if(nLine % 7 == 0)
					System.out.println();
				nLine++;
				nDay++;
			}

			System.out.println();
		}
	}
}
728x90
반응형

'java' 카테고리의 다른 글

PrintWriter/InputStreamReader  (0) 2021.10.20
FileReader/FileWriter  (0) 2021.10.20
회원관리 기능  (0) 2021.10.20
[9/17] class ,생성자,메소드  (0) 2021.10.20
[JAVA-문법] 1. 출력,main메소드  (0) 2021.09.13