忙活一下午的成果.
本帖最后由 meilon 于 2017-4-19 22:48 编辑学习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;
}
}
package MyBook;
import java.text.*;
import java.util.*;
public class MyBook {
Book[] book = new Book;
Scanner x = new Scanner(System.in);
SimpleDateFormat dates = new SimpleDateFormat("yyyy-MM-dd");
MyBook(){
initialize();
}
void initialize(){
book = new Book();
book.setName("聊斋志异");
book.setDate("2017-01-18");
book.setStatus(true);
book.setCount(22);
book = new Book();
book.setName("西游记");
book.setDate("无 ");
book.setStatus(false);
book.setCount(15);
book = new Book();
book.setName("东周列国志");
book.setDate("无 ");
book.setStatus(false);
book.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.getName().equals(name)) {
return i;
}
}
System.out.println("无此图书信息");
return -1;
}
//查询已有图书信息数量(获得Book对象实际长度)
int length(){
int i = 0;
for (;i < book.length; i++) {
if (book == 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.getStatus().matches(status) ) { //根据借出状态打印信息
System.out.print(i + 1 + "\t");
book.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 = new Book();
book.setName(name);
book.setStatus(false);
book.setDate("无 ");
System.out.println("新增《"+book.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.isStatus()){ //执行借出状态
System.out.println("《"+ name +"》为借出状态,不能删除!");
continue;
}else if(!book.isStatus()){ //执行未借状态
for (;i < length(); i++) {
book = book;
}
book = 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.isStatus()) {
System.out.println("对不起,该图书已借出!");
continue;
}else if(!book.isStatus()){
long times = System.currentTimeMillis();
Date date = new Date(times);
book.setDate(dates.format(date));
book.setCount(book.getCount() + 1);
book.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.getCount() < book.getCount()) {
index = j;
}
}
Book temp = book;
book = book;
book = 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.isStatus()) { //执行未借状态
System.out.println("此图书未借出!");
continue;
}else if (book.isStatus()) { //执行已借状态
long times = System.currentTimeMillis();
Date now = new Date(times);
Date d1 = dates.parse(book.getDate());
long d2 = now.getTime() - d1.getTime();
int days = (int)(d2 / 1000 / 60 / 60 / 24 + 1);
book.setStatus(false);
book.setDate("无 ");
System.out.println("共借出" + days + "天," + "租金" + days * 1.5 + "元.");
}
if (--num <= 0) {
System.out.println("\n所有图书都已归还!");
}
}
go();
}
}
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;
}
}
}
}
{:1_921:},加油!! 已经很不错了 加油小伙子 已经很不错了 加油小伙子 看起来很厉害,可我不懂 棒棒!!
这个能做网页自动答题的程序么? 路漫漫其修远兮 ACCP7.0 {:1_900:} 爱学习 爱吾爱