本帖最后由 meilon 于 2017-4-19 22:48 编辑
学习java一个月了....总算能写出点像样的东西了...
大神莫笑话, 让我这新手美一会儿....
放上代码.....
[Java] 纯文本查看 复制代码 package MyBook;
public class Book {
//图书信息
private boolean status; //借出状态
private String name; //名称
private String date; //日期
private int count; //借出次数
void print(){
System.out.print("《" + getName() + "》 \t\t");
System.out.print(getStatus() + "\t\t");
System.out.print(getDate() + "\t");
System.out.println(getCount() + "次");
}
public String getStatus() {
if(status){
return "已借";
}else{
return "未借";
}
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getDate() {
return date;
}
public void setDate(String dates) {
this.date = dates;
}
}
[Java] 纯文本查看 复制代码 package MyBook;
import java.text.*;
import java.util.*;
public class MyBook {
Book[] book = new Book[99];
Scanner x = new Scanner(System.in);
SimpleDateFormat dates = new SimpleDateFormat("yyyy-MM-dd");
MyBook(){
initialize();
}
void initialize(){
book[0] = new Book();
book[0].setName("聊斋志异");
book[0].setDate("2017-01-18");
book[0].setStatus(true);
book[0].setCount(22);
book[1] = new Book();
book[1].setName("西游记");
book[1].setDate("无 ");
book[1].setStatus(false);
book[1].setCount(15);
book[2] = new Book();
book[2].setName("东周列国志");
book[2].setDate("无 ");
book[2].setStatus(false);
book[2].setCount(70);
}
//流程控制
int flow(){
int flow;
do{
System.out.print("请选择:");
flow = x.nextInt();
if(flow < 1 || flow > 6){
System.out.print("序号错误!");
}
}while(flow < 1 || flow > 6);
return flow;
}
void go(){
System.out.print("输入任意键返回...");
x.next();
}
//查询图书信息(返回下标)
int search(String name){
int index = length();
for (int i = 0;i < index; i++) {
if (book[i].getName().equals(name)) {
return i;
}
}
System.out.println("无此图书信息");
return -1;
}
//查询已有图书信息数量(获得Book对象实际长度)
int length(){
int i = 0;
for (;i < book.length; i++) {
if (book[i] == null) {
break;
}
}
return i;
}
//打印信息(返回打印的数量)
int print(String status){
int j = 0;
System.out.println("序号\t名称 \t\t状态\t\t借出日期 \t共借阅");
for (int i = 0;i < length(); i++) {
if (book[i].getStatus().matches(status) ) { //根据借出状态打印信息
System.out.print(i + 1 + "\t");
book[i].print();
j++;
}
}
System.out.println("\n查询到" + j +"条信息");
return j;
}
void case0(){
System.out.println("\n欢迎使用图书借阅管理器");
System.out.println("-----------------------------------");
System.out.println("\t1.新增图书");
System.out.println("\t2.查看图书");
System.out.println("\t3.删除图书");
System.out.println("\t4.借出图书");
System.out.println("\t5.归还图书");
System.out.println("\t6.退出系统");
System.out.println("-----------------------------------");
}
void case1(){
System.out.println("*****************新增图书*********************");
int i = length();
for (; i < book.length; i++) {
System.out.print("请输入图书名称(输入0结束):");
String name = x.next();
if("0".equals(name)){
return;
}else{
book[i] = new Book();
book[i].setName(name);
book[i].setStatus(false);
book[i].setDate("无 ");
System.out.println("新增《"+book[i].getName()+"》成功!");
}
}
if (i == book.length) {
System.out.println("仓库已满!");
}
go();
}
void case2(){
System.out.println("*****************查看图书*********************");
print("\\D借");
go();
}
void case3(){
System.out.println("*****************删除图书********************");
int num = print("未借"); //打印所有状态为未借的图书信息
while (num > 0) {
System.out.print("\n输入要删除的图书名称(输入0退出):");
String name = x.next();
if ("0".equals(name)) {
return;
}
int i = search(name);
if (i < 0) {
continue;
}else if(book[i].isStatus()){ //执行借出状态
System.out.println("《"+ name +"》为借出状态,不能删除!");
continue;
}else if(!book[i].isStatus()){ //执行未借状态
for (;i < length(); i++) {
book[i] = book[i+1];
}
book[i] = null;
System.out.println("成功删除《"+ name +"》图书信息!");
if (--num <= 0) {
System.out.println("\n仓库已空!");
}
}
}
go();
}
void case4(){
System.out.println("*****************借出图书*********************");
int num = print("未借");
while (num > 0) {
System.out.print("\n输入要借出的图书名称(输入0退出):");
String name = x.next();
int index = search(name);
if("0".equals(name)){
return;
}else if (index < 0) {
continue;
}else if (book[index].isStatus()) {
System.out.println("对不起,该图书已借出!");
continue;
}else if(!book[index].isStatus()){
long times = System.currentTimeMillis();
Date date = new Date(times);
book[index].setDate(dates.format(date));
book[index].setCount(book[index].getCount() + 1);
book[index].setStatus(true);
System.out.println("借出《" + name + "》成功!");
}
//按借出次数排序
int length = length();
for (int i = 1;i < length; i++) {
index = 0;
for (int j = 1;j <= length - i; j++) {
if (book[j].getCount() < book[index].getCount()) {
index = j;
}
}
Book temp = book[length - i];
book[length - i] = book[index];
book[index] = temp;
}
if (--num <= 0) {
System.out.println("\n所有图书都已借出!");
}
}
go();
}
void case5() throws ParseException{
System.out.println("******************归还图书********************");
int num = print("已借"); //打印所有状态为已借的图书信息
while (num > 0) {
System.out.print("输入要归还的图书名称(输入0退出):");
String name = x.next();
if("0".equals(name)){
return;
}
int i = search(name);
if (i < 0) {
continue;
}else if (!book[i].isStatus()) { //执行未借状态
System.out.println("此图书未借出!");
continue;
}else if (book[i].isStatus()) { //执行已借状态
long times = System.currentTimeMillis();
Date now = new Date(times);
Date d1 = dates.parse(book[i].getDate());
long d2 = now.getTime() - d1.getTime();
int days = (int)(d2 / 1000 / 60 / 60 / 24 + 1);
book[i].setStatus(false);
book[i].setDate("无 ");
System.out.println("共借出" + days + "天," + "租金" + days * 1.5 + "元.");
}
if (--num <= 0) {
System.out.println("\n所有图书都已归还!");
}
}
go();
}
}
[Java] 纯文本查看 复制代码 package MyBook;
import java.text.ParseException;
public class Text {
public static void main(String[] args) throws ParseException{
MyBook dvd = new MyBook();
while(true){
dvd.case0();
switch(dvd.flow()){
case 1:
dvd.case1();
break;
case 2:
dvd.case2();
break;
case 3:
dvd.case3();
break;
case 4:
dvd.case4();
break;
case 5:
dvd.case5();
break;
case 6:
System.out.print("退出程序...");
return;
}
}
}
}
|